Results 1 to 9 of 9

Thread: [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,256

    Resolved [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

    I have an array of points that draws a polygon in a vertical position (All of the X and Y values are positive)...

    Code:
    Dim points21x22() As Point = New Point() {New Point(10, 0), New Point(15, 3), New Point(18, 5), New Point(20, 8), _
                                                 New Point(20, 39), New Point(17, 42), New Point(3, 42), New Point(0, 39), _
                                                 New Point(0, 8), New Point(2, 5), New Point(5, 3), New Point(10, 0)}
    I can rotate this array 90 degrees clockwise...

    Code:
    Dim newPoints() As Point = Array.ConvertAll(points21x22, Function(pt) New Point(-pt.Y, pt.X))
    But this results in negative values in the X coordinate which messes with my offsetting.

    I can fix my offsetting...

    Code:
    Dim o As Integer = newPoints.Min(Function(pt2) pt2.X)
    newPoints = Array.ConvertAll(newPoints, Function(pt) New Point(pt.X - o, pt.Y))
    But this still gives an array with some negative X values.
    Can someone explain how to rotate my polygons so that all of the point values are positive?, and also if this code...

    Code:
    Dim newPoints() As Point = Array.ConvertAll(points21x22, Function(pt) New Point(pt.Y, -pt.X))
    Will rotate 90 degrees anticlockwise? And also, will this rotate by 180 degrees?...

    Code:
    Dim newPoints() As Point = Array.ConvertAll(points21x22, Function(pt) New Point(pt.Y, pt.X))

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,639

    Re: Rotating an array of points clockwise 90, 180, and 270 degrees

    What are you actually trying to rotate around? I would think that the thing to do would be to determine your point of rotation, convert each point to polar coordinates relative to that, increase or decrease the angle as appropriate and then convert back to Cartesian coordinates.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,256

    Re: Rotating an array of points clockwise 90, 180, and 270 degrees

    That makes some sense. The array starts at 10, 0. If I subtract 10 from each of the X values, rotate using the methods I mentioned, then add 10 to each of the X values, is that right? Iím not at my computer right now.

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,256

    Re: Rotating an array of points clockwise 90, 180, and 270 degrees

    Iíll post a diagram showing what it starts at, and what it is supposed to be transformed toÖ

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,256

    Re: [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

    Problem resolved... I managed to get myself tied up in point arrays. Didn't realise i already had the answer

    Code:
    Public Shared Function PlusNinety(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(-pt.Y, pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.X)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X - o, pt.Y))
    End Function
    
    Public Shared Function MinusNinety(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(pt.Y, -pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.Y)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X, pt.Y - o))
    End Function
    
    Public Shared Function PlusOneEighty(ByVal p() As Point) As Point()
        Dim o As Integer = p.Max(Function(pt2) pt2.Y)
        Return Array.ConvertAll(p, Function(pt) New Point(pt.X, o - pt.Y))
    End Function

  6. #6
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    351

    Re: [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

    Quote Originally Posted by .paul. View Post
    Problem resolved... I managed to get myself tied up in point arrays. Didn't realise i already had the answer

    Code:
    Public Shared Function PlusNinety(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(-pt.Y, pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.X)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X - o, pt.Y))
    End Function
    
    Public Shared Function MinusNinety(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(pt.Y, -pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.Y)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X, pt.Y - o))
    End Function
    
    Public Shared Function PlusOneEighty(ByVal p() As Point) As Point()
        Dim o As Integer = p.Max(Function(pt2) pt2.Y)
        Return Array.ConvertAll(p, Function(pt) New Point(pt.X, o - pt.Y))
    End Function
    Do you think making these extension methods will make the code more clean? (sorry to rename your function names but for me digits are more readable )

    VB.NET Code:
    1. p1.Rotate180()
    2. p2.RotateMinus90()
    3. p3.RotatePlus90()

    Code:
    Imports System.Runtime.CompilerServices
    
    Module PointsArrayExtensions
    
    <Extension()>
    Public Function RotatePlus90(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(-pt.Y, pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.X)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X - o, pt.Y))
    End Function
    
    <Extension()>
    Public Shared Function RotateMinus90(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(pt.Y, -pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.Y)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X, pt.Y - o))
    End Function
    
    <Extension()>
    Public Function Rotate180(ByVal p() As Point) As Point()
        Dim o As Integer = p.Max(Function(pt2) pt2.Y)
        Return Array.ConvertAll(p, Function(pt) New Point(pt.X, o - pt.Y))
    End Function
    
    End Module

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,256

    Re: [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

    Quote Originally Posted by peterst View Post
    Do you think making these extension methods will make the code more clean? (sorry to rename your function names but for me digits are more readable )

    VB.NET Code:
    1. p1.Rotate180()
    2. p2.RotateMinus90()
    3. p3.RotatePlus90()

    Code:
    Imports System.Runtime.CompilerServices
    
    Module PointsArrayExtensions
    
    <Extension()>
    Public Function RotatePlus90(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(-pt.Y, pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.X)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X - o, pt.Y))
    End Function
    
    <Extension()>
    Public Shared Function RotateMinus90(ByVal p() As Point) As Point()
        Dim newPoints() As Point = Array.ConvertAll(p, Function(pt) New Point(pt.Y, -pt.X))
        Dim o As Integer = newPoints.Min(Function(pt2) pt2.Y)
        Return Array.ConvertAll(newPoints, Function(pt) New Point(pt.X, pt.Y - o))
    End Function
    
    <Extension()>
    Public Function Rotate180(ByVal p() As Point) As Point()
        Dim o As Integer = p.Max(Function(pt2) pt2.Y)
        Return Array.ConvertAll(p, Function(pt) New Point(pt.X, o - pt.Y))
    End Function
    
    End Module
    I considered making them extension methods. At this point in my project, i'm focusing on GUI functionality issues. I might come back to these functions later...

  8. #8
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    351

    Re: [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

    Quote Originally Posted by .paul. View Post
    I considered making them extension methods. At this point in my project, i'm focusing on GUI functionality issues. I might come back to these functions later...
    I am thinking of methods naming as most of time bad naming makes my code unclear of the real intent. Probably RotateLeft/RotateRight or RotateCW/RotateCCW (may include 90 as degrees) are more appropriate names. I am really interested what you think and what you will use as names as all these sound ok but in code they may look weird when time passes and you read what you did. I had few db fields and procedures renaming few hours ago to suit better the global purpose of my app but right now I found it is still not perfect for future readability and easy understanding what is done (event sourced systems are pain to get naming right).

  9. #9

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,256

    Re: [RESOLVED] Rotating an array of points clockwise 90, 180, and 270 degrees

    I should point out, my plusOneEighty method isnít actually a rotation of 180 degrees. Itís a flip horizontal mirror of the original points. This works ok for me, as my points are symmetricalÖ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width