Vega_Ska
Vega_Ska

Reputation: 21

Declaring points in array for drawing polygon in VB 2019

Is there a way to declare a constant array where i can declare the points for drawing a polygon?

Something along the lines like:

Dim myPoints As Point() = {{10,10}, {12,12}, {13,13}, {14,14}, {15,15}} 
...
...
...
myGraphics.DrawPolygon(myPen, myPoints)

The idea is hardcoding the points using the least posible code

Upvotes: 0

Views: 1536

Answers (2)

djv
djv

Reputation: 15774

You can initialize an array of Point like this

Dim myPoints = {
    New Point(10, 10),
    New Point(12, 12),
    New Point(13, 13),
    New Point(14, 14),
    New Point(15, 15)
}

or put it all on one line, which looks just like your multidimensional array version.

Dim myPoints = {New Point(10, 10), New Point(12, 12), New Point(13, 13), New Point(14, 14), New Point(15, 15)}

We can also instead use a jagged array which LINQ can operate on to select new Points

Dim myPoints =
    {New Integer() {10, 11}, New Integer() {12, 13}, New Integer() {14, 15}}.
    Select(Function(p) New Point(p(0), p(1))).ToArray()

Upvotes: 2

G3nt_M3caj
G3nt_M3caj

Reputation: 2685

Do you mean something like this?

        Dim NewPoint As Func(Of Integer, Integer, Point) = Function(left As Integer, top As Integer)
                                                               Return New Point(left, top)
                                                           End Function
        Dim myPoints As List(Of Point) = New List(Of Point) From {NewPoint(50, 50), NewPoint(80, 50), NewPoint(110, 80), NewPoint(80, 110), NewPoint(50, 110), NewPoint(20, 80)}
        myGraphics.DrawPolygon(Pens.Red, myPoints.ToArray)

Upvotes: 0

Related Questions