[Math] Calculate Angle between Two Intersecting Line Segments

trigonometry

Need some help/direction, haven't had trig in several decades.

On a 2 dimensional grid, I have two line segments.

The first line Segment always starts at the origin $(0,0)$, and extends to $(1,0)$ along the $X$-axis.

The second line Segment intersects the first at the origin, and can extend to potentially anywhere within $(-1,-1)$ to $(1,1)$.

I need to always calculate the angle to the right of the first segment…

If this is already addressed in another post, please comment me the link.

UPDATE

I will have a single input of $(x,y)$ for the end of the 2nd segment…

so segment $A$ would be $(0,0)$$ (1,0)$ and segment $B$ would be $(0,0)$$(x,y)$ where $(x,y)$ can be anywhere inside $(-1,-1)$ and $(1,1)$ assuming that the scale is $0.1$.

Let me know If I can provide any additional information that will help.

UPDATE

OK… assuming that the first segment is running along the $Y$-axis… $A(0,0)$ and $B(0,1)$

And the second segment is running from $A(0,0)$ to $C(.4,.4)$ with a scale of .2….

$$\theta= \tan^{-1}{\dfrac{.4}{.4}}= 45$$

If I change C to $C(.4,-.4)$ I get.

$$\theta= \tan^{-1}{\dfrac{.4}{-.4}}= -45$$

Do I have to manually compensate for the quadrant because this seems to calculate based specifically on the axis… I would expect the 2nd one to come up as 135 degrees from the positive Y Axis…

What am I missing?

Just for posterity…

If I had $C(-0.4,-0.1)$ I would expect the result for the angle from the positive Y axis to this line segment to be roughly 255 degrees…

$$\theta= \tan^{-1}{\dfrac{.4}{-.1}}= 75.9637$$

Plus 180 from starting at the positive Y axis….

Best Answer

What you are looking for is an angle $\theta$ such that \begin{align} x &= r\cos\theta \\ y &= r\sin\theta \end{align} where $r = \sqrt{x^2+y^2}$ is the length of segment $B.$

If you are doing this on a computer, you may be in luck, because many programming environments offer a function (usually named $\mathrm{atan2}$) which is defined so that $$ \mathrm{atan2}(y,x) $$ gives the exact angle you want, in radians, except that the result of $\mathrm{atan2}$ is always in the range $-\pi < \mathrm{atan2} \leq \pi,$ whereas you want $0 \leq \theta < 2\pi$ (in radians). So if $\mathrm{atan2} < 0$ you will want to add $2\pi.$ Multiply by $180/\pi$ to get the answer in degrees.

The function $\tan^{-1}$ can only give an angle $\theta$ such that $-\frac\pi2 < \theta < \frac\pi2.$ So this will be the correct angle only if $x > 0.$ But if $x < 0$ you can use the fact that $$ \tan\frac yx = \tan\frac{-y}{-x} $$ and the fact that $(-x,-y)$ is what you get when you rotate $(x,y)$ by $180$ degrees in either direction around $(0,0).$ In short, if $x<0$ then you need to add $180$ degrees to your result; if $x > 0$ but $y < 0$ you will need to add $360$ degrees to compensate for the fact that $\tan^{-1}\frac yx < 0$; and if $x=0$ you need to look at the sign of $y$ to decide whether the angle is $90$ degrees or $270$ degrees.