Michael Ahlers suggested using the Law of Cosines to find the angle between two line segments. The Law of Cosines states for a triangle that:
c^2 = a^2 + b^2 - 2 * a * b * Cos(C)
Where a, b, and c are the lengths of the sides of the triangle and A, B, and C are the angles opposite the corresponding sides. For example, A is the angle opposite side a.
Solving for C gives:
C = Arccosine((c^2 - a^2 - b^2) / -2 * a * b)
Microsoft's Derived Math Formula Web page gives this formula for Arccosine:
Arccosine(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
Putting all this together lets us find the angle between two line segments. The GetAngle function calculates the triangle side lengths. It uses the formula above and the Acos function to calculate the angle.
|