I have two lines: Line1 and Line2. Each line is defined by two points (P1L1(x1, y1), P2L1(x2, y2)
and P1L1(x1, y1), P2L3(x2, y3))
. I want to know the inner angle defined by these two lines.
For do it I calculate the angle of each line with the abscissa:
double theta1 = atan(m1) * (180.0 / PI);
double theta2 = atan(m2) * (180.0 / PI);
After to know the angle I calculate the following:
double angle = abs(theta2 - theta1);
The problem or doubt that I have is: sometimes I get the correct angle but sometimes I get the complementary angle (for me outer). How can I know when subtract 180º
to know the inner angle? There is any algorithm better to do that? Because I tried some methods: dot product,
following formula:
result = (m1 - m2) / (1.0 + (m1 * m2));
But always I have the same problem; I never known when I have the outer angle or the inner angle!
The whole point is much easier than the given answers:
When you use atan(slope) you lose (literally) one bit of information, that is there are exactly two angles (theta) and (theta+PI) in the range (0..2*PI), which give the same value for the function tan().
Just use atan2(deltax, deltay) and you get the right angle. For instance
Then subtract, take absolute value, and if greater than PI subtract from 2*PI.
I think what you're looking for is the inner product (you may also want to look over the dot product entry) of the two angles. In your case, that's given by:
Answer is in radians.
EDIT: Here's a complete implementation. Substitute the problematic values in p1, p2, and p3 and let me know what you get. The point p1 is the vertex where the two lines intersect, in accordance with your definition of the two lines.
The code above yields: