Rotate polygon

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
Hi all,

I want to rotate a polygon around one point of that polygon.
The point of rotation is p2(pivotr).
I use the folowing code:
VB.NET:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint

        Dim myPenF As New Pen(Color.OldLace, 5)
        myPenF.Alignment = PenAlignment.Center
        myPenF.LineJoin = LineJoin.Round
        Dim mybrushF As New SolidBrush(Color.OrangeRed)
        Dim p1 As New Point
        p1 = endr
        Dim p2 As New Point
        p2 = pivotr
        Dim p3 As New Point(pivotr.X + 10, pivotr.Y + 20)
        Dim p4 As New Point(endr.X, endr.Y + 10)
        Dim polygonR As Point() = {p1, p2, p3, p4}

        e.Graphics.FillPolygon(mybrushF, polygonR)
        e.Graphics.DrawPolygon(myPenF, polygonR)
    End Sub

 Private Sub flipperRightMoveUp()
      
        For fl = -20 To 40

            Dim flipperRightAngle As Double = fl

            endr = New Point(pivotr.X - ((Math.Cos(flipperRightAngle * (pi / 180)) * 80)), pivotr.Y - (Math.Sin(flipperRightAngle * (pi / 180)) * 80))
            
            Dim rect As New Rectangle(200, 400, 600, 500)
            Invalidate(New Rectangle(200, 400, 600, 500))


        Next


    End Sub

This works, but the polygon gets deformed because i don't know how to calculate the other two points. I am not such a math wizz.

Can someone help me with my calculation, or is there a better way, like RotateTransform, but I have no clue how to use it.

Thanks in advance,

Groover.
 
You can call TranslateTransform to shift the coordinate system to be centred on the point of rotation. For instance, if the point of rotation is at (50, 100) then you would translate like this:
VB.NET:
e.Graphics.TranslateTransform(50, 100)
You would then translate all the points the other way, i.e. subtract 50 from the X value and 100 from the Y value. That means your point of rotation becomes (0, 0), i.e. the origin. Finally, you call RotateTransform and specify the angle of rotation. Everything you now draw will be rotated by that angle around the point of rotation.
 
Hi all,

I recoded with TranslateTransform and RotateTransform. After a lot of trial and error, I can not get it working.
The problem is that after rotation the drawing is not visable.

I assume that my methode of invalidating the drawn region is not right.
I can not use the me.invalidate and me. update because it messes with my other animations.

Here is the code that I use:
VB.NET:
     Public rectf As New Rectangle(162, 654, 217, 100) 'region to invalidate in Class Form1


    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Right And flipperRightMoveU = False Then
            flipperRightMoveU = True 'makes  Call flipperRightMoveUp() called once.
            flipperRightMoveD = False


            Call flipperRightMoveUp()
            TextBox1.BackColor = Color.Red 'just to check code, remove if OK

        End If

        If e.KeyCode = Keys.Left Then
            TextBox1.BackColor = Color.Red 'just to check code, remove if OK
        End If

    End Sub


    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Right And flipperRightMoveD = False Then

            flipperRightMoveD = True 'makes  Call flipperRightMoveDown() called once.
            flipperRightMoveU = False
            Call flipperRightMoveDown()

            flipperRightMoveU = False
            TextBox1.BackColor = Color.Blue 'just to check code, remove if OK

        End If
        If e.KeyCode = Keys.Left Then


            TextBox1.BackColor = Color.Blue 'just to check code, remove if OK
        End If

    End Sub


        'draw right flipper in Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint

        Dim myPenF As New Pen(Color.OldLace, 5)

        Dim mybrushF As New SolidBrush(Color.OrangeRed)

        Dim p1 As New Point
        p1 = endr
        Dim p2 As New Point
        p2 = pivotr
        Dim p3 As New Point(pivotr.X + 10, pivotr.Y + 20)
        Dim p4 As New Point(endr.X, endr.Y + 10)
        Dim polygonR As Point() = {p1, p2, p3, p4}


        If flipperRightMoveU = False Then 'makes polygonR drawn at form load for fist time
            e.Graphics.TranslateTransform(360, 718)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            myPenF.LineJoin = LineJoin.Round

            e.Graphics.FillPolygon(mybrushF, polygonR)
            e.Graphics.DrawPolygon(myPenF, polygonR)
        End If
    End Sub


    Public Sub flipperRightMoveUp()

        Dim gfx As Graphics = Me.CreateGraphics
        Dim myPenF As New Pen(Color.OldLace, 5)

        Dim mybrushF As New SolidBrush(Color.OrangeRed)
        Dim p1 As New Point
        p1 = endr
        Dim p2 As New Point
        p2 = pivotr
        Dim p3 As New Point(pivotr.X + 10, pivotr.Y + 20)
        Dim p4 As New Point(endr.X, endr.Y + 10)
        Dim polygonR As Point() = {p1, p2, p3, p4}

        Dim fl As Double = 40 'angle of rotation

        myPenF.LineJoin = Drawing2D.LineJoin.Round
        gfx.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

        gfx.TranslateTransform(360, 718) 'rotation point
        gfx.RotateTransform(fl)

        gfx.DrawPolygon(myPenF, polygonR)
        gfx.FillPolygon(mybrushF, polygonR)

        Invalidate(rectf) ' erase an redraw !!!!!IS NOT WORKING AS I EXPECT!!!!
    End Sub


    Public Sub flipperRightMoveDown()
        Dim gfx As Graphics = Me.CreateGraphics

        Dim myPenF As New Pen(Color.OldLace, 5)
        Dim mybrushF As New SolidBrush(Color.OrangeRed)

        Dim p1 As New Point
        p1 = endr
        Dim p2 As New Point
        p2 = pivotr
        Dim p3 As New Point(pivotr.X + 10, pivotr.Y + 20)
        Dim p4 As New Point(endr.X, endr.Y + 10)
        Dim polygonR As Point() = {p1, p2, p3, p4}


        gfx.ResetTransform() 'Undo rotation

        gfx.TranslateTransform(360, 718) 'make poligonR to be drawn at the right point..
        myPenF.LineJoin = Drawing2D.LineJoin.Round
        gfx.SmoothingMode = Drawing2D.SmoothingMode.HighQuality


        gfx.FillPolygon(mybrushF, polygonR)
        gfx.DrawPolygon(myPenF, polygonR)

        Invalidate(rectf) 'erase and redraw


    End Sub

If I press keyRight then the polygon is somtimes there for a fraction of a second.
If I release keyRight then the polygon is perfectly redrawn at the beginpoint before rotation.

How can I solve this?

Greetz

Groover1
 

Latest posts

Back
Top