none
Collection Initializer for a collection of points RRS feed

  • Question

  • Hi,

    I'm attempting to initialize a number of "PointCollection's" in an indexed array.  A variable will then be used to select a point collection which will be used to draw a polygon using Polygon.Points. Note the point collections are fixed and will not be changed at run time. As I am not a professional code writer I'm not sure if it's possible or weather I have not discovered the correct syntax. The code below does not work but hopefully demonstrates what I'm trying to achieve. If anyone has done something like this before please advise - Many Thanks!

    Dim n

    Dim ArrayofShapes()(,) As PointCollection = {

    New Point(,) {Point(100,0), Point(200,100) .....},

    New Point(,) {Point(200,0), Point(200,200) ....}

    Polygon.Points = ArrayofShapes(n)

    I also tried using  a collection initializer but still can't get it to work e.g.

    Dim ArrayofShapes(n) = New PointCollection (Of Point) From {Point(100,0), Point(200,100) ....}

    Saturday, December 2, 2017 11:04 AM

Answers

  • Here is a collection of polygons but I used a List(of T).

    Public Class Form6
        Private MyShapes As New List(Of PointF())
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'create the star shape polygon
            Dim thePolygon() As PointF =
                {New PointF(5, 9),
                New PointF(6, 6),
                New PointF(9, 6),
                New PointF(7, 4),
                New PointF(8, 1),
                New PointF(5, 3),
                New PointF(2, 1),
                New PointF(3, 4),
                New PointF(1, 6),
                New PointF(4, 6),
                New PointF(5, 9)}
    
    
            'create the square shape polygon
            Dim thePolygon2() As PointF =
                {New PointF(0, 0),
                New PointF(6, 0),
                New PointF(6, 6),
                New PointF(0, 6)}
    
    
            MyShapes.Add(thePolygon)
            MyShapes.Add(thePolygon2)
    
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .ResetTransform()
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                .ScaleTransform(20, 20)         'create scale of 20 units across the width of the drawing surface
    
                .TranslateTransform(2, 2)
                Dim path2 As New Drawing2D.GraphicsPath
                path2.AddLines(MyShapes(1))
                path2.CloseFigure()
                .DrawPath(Pens.Blue, path2)
    
    
                .TranslateTransform(10, 10)       'translate origin to center star in drawing surface more or less
                .RotateTransform(180)           'rotate start for upright points
                Dim path1 As New Drawing2D.GraphicsPath
                path1.AddLines(MyShapes(0))
                .DrawPath(Pens.Red, path1)
    
                path1.Dispose()
                path2.Dispose()
            End With
        End Sub
    End Class

    • Marked as answer by LH63 Sunday, December 3, 2017 12:12 PM
    Saturday, December 2, 2017 12:45 PM

All replies


  • Here is an array of points.

    I am not sure what you want the collection for. Do you want to make an array of arrays of points?

            'create the star shape polygon
            Dim thePolygon() As PointF =
                {New PointF(5, 9),
                New PointF(6, 6),
                New PointF(9, 6),
                New PointF(7, 4),
                New PointF(8, 1),
                New PointF(5, 3),
                New PointF(2, 1),
                New PointF(3, 4),
                New PointF(1, 6),
                New PointF(4, 6),
                New PointF(5, 9)}
    
    

    If you tell us more about exactly what you want to do we may have suggestions. Like List(of T) instead.

    Saturday, December 2, 2017 12:05 PM
  • Here is a collection of polygons but I used a List(of T).

    Public Class Form6
        Private MyShapes As New List(Of PointF())
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'create the star shape polygon
            Dim thePolygon() As PointF =
                {New PointF(5, 9),
                New PointF(6, 6),
                New PointF(9, 6),
                New PointF(7, 4),
                New PointF(8, 1),
                New PointF(5, 3),
                New PointF(2, 1),
                New PointF(3, 4),
                New PointF(1, 6),
                New PointF(4, 6),
                New PointF(5, 9)}
    
    
            'create the square shape polygon
            Dim thePolygon2() As PointF =
                {New PointF(0, 0),
                New PointF(6, 0),
                New PointF(6, 6),
                New PointF(0, 6)}
    
    
            MyShapes.Add(thePolygon)
            MyShapes.Add(thePolygon2)
    
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .ResetTransform()
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                .ScaleTransform(20, 20)         'create scale of 20 units across the width of the drawing surface
    
                .TranslateTransform(2, 2)
                Dim path2 As New Drawing2D.GraphicsPath
                path2.AddLines(MyShapes(1))
                path2.CloseFigure()
                .DrawPath(Pens.Blue, path2)
    
    
                .TranslateTransform(10, 10)       'translate origin to center star in drawing surface more or less
                .RotateTransform(180)           'rotate start for upright points
                Dim path1 As New Drawing2D.GraphicsPath
                path1.AddLines(MyShapes(0))
                .DrawPath(Pens.Red, path1)
    
                path1.Dispose()
                path2.Dispose()
            End With
        End Sub
    End Class

    • Marked as answer by LH63 Sunday, December 3, 2017 12:12 PM
    Saturday, December 2, 2017 12:45 PM
  • If you want to initialize a PointCollection, you can do :

    Dim collection As PointCollection = New PointCollection({New System.Windows.Point(100, 0), New System.Windows.Point(200, 100), New System.Windows.Point(0, 100)})

    Saturday, December 2, 2017 1:59 PM
  • Got it working by using case statement to check the variable's value then based on variables value created a collection of points using:

    Dim PolyPoints as New PointCollection

    Case "n"

    Dim Point1 as New Point(100,0)

    Dim Point2 as New Point(200,100)

    etc

    PolyPoints.Add(Point1)

    PolyPoints.Add(Point2)

    etc

    Your suggestion also contained another useful method which I'll investigate further.

    Many Thanks!

    Sunday, December 3, 2017 12:12 PM