Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15587

Point rotation with reference to zero?

$
0
0
Hi all, hope you had a good weekend :). I should be able to do this but for some reason not wrapping my mind around it. The following code rotates a given point around an origin, usually the center. Proven, straightforward math.

Code:

Private Function RotatePoint(ByRef pointToRotate As POINTF, ByVal angle As Single, ByRef Origin As POINTF) As POINTF
       
        Const pi As Single = 3.141593
       
        Dim RadAngle As Double
        Dim X As Double
        Dim Y As Double
        Dim StartAngle As Double
        Dim EndAngle As Double
        Dim Radius As Double
        Dim NewX As Single
        Dim NewY As Single
                 
        '- Convert degrees to Radians
        RadAngle = 2 * pi * angle / 360
        '- Find angle between origin and point to rotate
        X = pointToRotate.X - (Origin.X)
        Y = pointToRotate.Y - (Origin.Y)
        If X = 0 Then X = 0.00001
        StartAngle = Atn(Y / X)
        '- Add the desired angle with which to rotate
        EndAngle = StartAngle + RadAngle
        '- Calculate Radius
        Radius = X / Cos(StartAngle)
        '- Find location of point after rotation
        NewX = CSng(Radius * Cos(EndAngle))
        NewY = CSng(Radius * Sin(EndAngle))
        '- Return new point position
        RotatePoint.X = NewX + Origin.X
        RotatePoint.Y = NewY + Origin.Y
     

    End Function


Works as it should but with one caveat for certain applications - the desired rotation angle is always added to the previous angle, so that each time this is called and a new angle(0-360 degrees) passed, it will keep adding the angles together and go nuts if you want to, say, use it for rotation with a slider control.

Is it possible to modify this so as to always reference rotation from zero? Then one could simply set the desired angle of rotation to a number from 0 to 360, every time.

I've been experimenting for the last few days but not getting my desired result :(. I'm fairly proficient with basic trig functions but this one has me baffled! :). any help much appreciated.

Viewing all articles
Browse latest Browse all 15587

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>