Search code examples
vb.netgraphicspolygonrotatetransform

Rotate a polygon drawn with GraphicsPath


I'd like to rotate a polygon by -90 degrees at the upper right corner.

Here's my code to draw the polygon :

Private Sub Form1_Click(sender As Object, e As EventArgs) Handles MyBase.Click
    Dim g As Graphics = Me.CreateGraphics()
    Dim p As New Drawing2D.GraphicsPath()

    Dim startPoint As Point = PointToClient(Cursor.Position)

    Dim littleAngle As Integer = 4
    Dim anglesDifference As Integer = 2
    Dim bigAngle As Integer = littleAngle + anglesDifference
    Dim bigSegment As Integer = 50

    Dim p1, p2, p3, p4, p5 As Point

    p1 = New Point(startPoint.X + littleAngle, startPoint.Y - littleAngle)
    p2 = New Point(startPoint.X + littleAngle + bigSegment, startPoint.Y - littleAngle)
    p3 = New Point(startPoint.X + littleAngle + bigSegment + littleAngle, startPoint.Y)
    p4 = New Point(startPoint.X + littleAngle - anglesDifference + bigSegment, startPoint.Y + bigAngle)
    p5 = New Point(startPoint.X + bigAngle, startPoint.Y + bigAngle)

    p.AddLines({startPoint, p1, p2, p3, p4, p5})

    Dim pen As New Pen(Color.Red, 1)
    g.FillPath(New SolidBrush(Color.Red), p)
    g.DrawPath(pen, p)

    Dim translateMatrix As New Matrix
    translateMatrix.RotateAt(-90, New PointF(p.GetBounds.Width, 0))
    p.Transform(translateMatrix)

    g.FillPath(New SolidBrush(Color.Red), p)
    g.DrawPath(pen, p)
End Sub

Here's what is drawn using the code above :

the result I've

And here's an example of what I'd like to have :

enter image description here


Solution

  • To get what you want, just replace translateMatrix.RotateAt(-90, New PointF(p.GetBounds.Width, 0)) with:

    translateMatrix.RotateAt(-90, p3)