4

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....

Patrick
  • 141

3 Answers3

1

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.

David K
  • 98,388
0

In fact, you want to know the angle between the $x$ axis and any line going through the origin $(0,0)$ and a point $A (x_a,y_a)$, with $-1 \leq x_a,y_a \leq 1?$

This is fairly easy. Since there are two angles between two lines, you said you wanted the "right angle", that is, the one to the right hand side of the $x$ axis.

If $x_a \times y_a<0$, $\theta= \tan^{-1}{\dfrac{y_a}{x_a}}$

If $x_a=0$, $\theta=\dfrac{\pi}{2}$

If $x_a\times y_a>0$, $\theta= \tan^{-1}{\dfrac{y_a}{x_a}}-\pi$

Martigan
  • 5,844
  • Yes... you appear to know what I want more so than I do... Let me verify everything and I'll select answer. – Patrick Jan 09 '15 at 15:33
  • You can also use $\theta = cos^{-1}\left(\frac{x}{\sqrt{x^2+y^2}} \right)$ for example, as it will also work when $x = 0$ – servabat Jan 09 '15 at 15:42
  • I ran (-.4, -.4) through the bottom formula and got -64.9 Degrees... that doesn't seem right to me... if the first part is running along the X axis.... I would expect a positive answer between 90 and 180 degrees... what am I missing? – Patrick Jan 09 '15 at 15:52
  • @Patrick First, angle are negative when counting them down... (that is, clockwise). Second, did you substract $\pi$ or $180°$? – Martigan Jan 09 '15 at 15:54
  • OK... I can handle the negative... I subtracted by \Pi – Patrick Jan 09 '15 at 15:56
  • Then, $\tan^{-1}$ is the inverse function of $tan$, $\tan^{-1}1=45°=\frac{\pi}{4}$ – Martigan Jan 09 '15 at 15:58
  • OK... I'm still not seeing what I would expect to see though... $\theta= \tan^{-1}{\dfrac{-0.4}{-0.4}}-\pi$ still only returns -64.9... and with - 180 instead of $\Pi$ returns -89.6... Going from between the two quadrants on the right to the bottom left quadrant I would expect something near 135... – Patrick Jan 09 '15 at 16:06
  • Would it be easier/different if the first segment ran along the Y axis from $A(0,0)$ to $B(0,1)$? – Patrick Jan 09 '15 at 16:08
  • @Patrick This is exactly what I got: $-\frac{3\pi}{4}$... I don't know what is going on. – Martigan Jan 09 '15 at 16:11
0

Not sure EXACTLY what you are asking, but I will answer this to the best of my ability. If you could include a visual that would greatly help me.

When we have two intersecting line segments like this

enter image description here

finding any single value (a, c, b, d) will reveal all other values. For example, if we have the value of a, then c = a, and we have (in degrees) b = d = 180-a.

Therefore, I can equip with the tools to find the angle between two vectors, as you have given. For example, we can treat the first vector as you have said as $[1, 0]$ and the second vector as $[1, 1]$. We take the dot product between them, which just means that we multiply the corresponding values and sum them up, or $<a, b> = \sum\limits_{i = 1}^{n}{a_ib_i}$ where $n$ is the number of elements in the vector. We use the geometric fact that $<a, b> = |a||b|cos(\theta_{a, b})$ where $|a|$ means its norm or magnitude (I only deal with standard inner product and norm here). This gives us that the dot product, which is $1$, is equal to $\sqrt{2}cos(\theta)$, which means $cos(\theta) = \frac{1}{\sqrt{2}}$

This in turn gives us that the angle between these two is 45 degrees, and we can figure out the adjacent angle as 135, and the vertical angles all share the same degrees.