Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I have an array of points that makes a polygon. how can i rotate polygon around its center.

What I have tried:

Public Function RotatePoints(_Points() As Point, _CenterPoint As Point, _Degree As Double) As Point()
    Dim Output() As Point = _Points.Clone()

    Dim Angle As Double = (_Degree / 180.0) * Math.PI
    For Index As Integer = 0 To Output.Length - 1
        Output(Index) = New Point((Math.Cos(Angle) * (Output(Index).X - _CenterPoint.X)) + _CenterPoint.X, (Math.Sin(Angle) * (Output(Index).Y - _CenterPoint.Y)) + _CenterPoint.Y)
    Next

    Return Output
End Function
Posted
Updated 18-Jan-18 0:35am
Comments
Kornfeld Eliyahu Peter 18-Jan-18 5:50am    
point-by-point?
mashali 18-Jan-18 6:45am    
thanks. problem solved, my algorithm was wrong.

Simple: your algorithm is wrong.
See here: Geometric Operations - Rotate[^]

I would suggest that you "break out" each coordinate into a separate set of lines, get X working - obviously, use the debugger to check - and then repeat for Y. When both work, amalgamate the separate stages of X and Y into single lines and create your new point.
 
Share this answer
 
Comments
mashali 18-Jan-18 6:32am    
Thanks OriginalGriff
its work :)
OriginalGriff 18-Jan-18 6:39am    
You're welcome!
Special thanks to OriginalGriff

Public Function RotatePoints2(_Points() As Point, _CenterPoint As Point, _Degree As Double) As Point()
    Dim Output() As Point = _Points.Clone()

    Dim Angle As Double = (_Degree / 180.0) * Math.PI
    For Index As Integer = 0 To Output.Length - 1
        Dim Dx As Integer = (Output(Index).X - _CenterPoint.X)
        Dim Dy As Integer = (Output(Index).Y - _CenterPoint.Y)
        Output(Index).X = (Math.Cos(Angle) * Dx) - (Math.Sin(Angle) * Dy) + _CenterPoint.X
        Output(Index).Y = (Math.Sin(Angle) * Dx) + (Math.Cos(Angle) * Dy) + _CenterPoint.Y
    Next

    Return Output
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900