Link to home
Start Free TrialLog in
Avatar of CyrexCore2k
CyrexCore2kFlag for United States of America

asked on

Rotatating line... Linear Algebra question.

Okay so here's a question. I need an algorithm for rotating a point (p2) about an axis (p1). Here's the spaghetti code I have so far.

Radius = SquareRoot((p2.y - p1.y) ^ 2 + (p2.x - p1.x) ^ 2)
Radians = ArcCos((p1.y - p2.y) / radius) ' Angle between Vector(p1,p2) and unit vector (p1, {0, 1}). Rotating counter clockwise.
Radians = Radians + (2 * ((2 * PI) / 360)) 'Add two degrees to current angle of line
p2.y = (Cos(Radians) * Radius) + p1.y
p2.x = (Cos(Radians) * Radius) + p1.x

The code works fine for angles < 180 degrees. But as soon as I get to say 182 the line moves to an angle of 2 degrees counter clockwise from the unit vector... anyone know a better algorithm for this or how to fix this one to be able to run a full rotation? I need to be able to "add" angles to lines.
Avatar of ozo
ozo
Flag of United States of America image

Radians = atan2(p2.y - p1.y,p1.x - p2.x);
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CyrexCore2k

ASKER

YES! Thank you!