How to calculate both positive and negative angle between two lines?

Posted by Jaanus on Stack Overflow See other posts from Stack Overflow or by Jaanus
Published on 2010-04-18T19:16:14Z Indexed on 2010/04/18 19:33 UTC
Read the original article Hit count: 384

Filed under:
|
|

There is a very handy set of 2d geometry utilities here.

The angleBetweenLines has a problem, though. The result is always positive. I need to detect both positive and negative angles, so if one line is 15 degrees "above" or "below" the other line, the shape obviously looks different.

The configuration I have is that one line remains stationary, while the other line rotates, and I need to understand what direction it is rotating in, by comparing it with the stationary line.

EDIT: in response to swestrup's comment below, the situation is actually that I have a single line, and I record its starting position. The line then rotates from its starting position, and I need to calculate the angle from its starting position to current position. E.g if it has rotated clockwise, it is positive rotation; if counterclockwise, then negative. (Or vice versa.)

How to improve the algorithm so it returns the angle as both positive or negative depending on how the lines are positioned?

© Stack Overflow or respective owner

Related posts about geometry

Related posts about 2d