1. ## Angle calculations

I had this problem on my last programming contest and my team had no idea as to how to approach it. I'm not asking for an exact answer to this but can anyone give me an general idea about how to do this, or what I should research to figure out how to do this?

Thanks!

2. Originally Posted by chili5
I had this problem on my last programming contest and my team had no idea as to how to approach it. I'm not asking for an exact answer to this but can anyone give me an general idea about how to do this, or what I should research to figure out how to do this?

Thanks!
Do you want the mathematical method to work it out, or how to turn this method into a piece of code?

3. Math method.

4. Hello chili5

The mathematics required is very simple, but you'll have to look at the logic of the various possible positions of the pairs of points to solve the problem. I suggest you proceed as follows:

Suppose that the line joining the origin to the point $\displaystyle (x_1, y_1)$ makes an angle $\displaystyle \theta_1$ with the positive direction of the $\displaystyle x$-axis, measured clockwise, with $\displaystyle 0\le\theta<360^o$; and similarly for $\displaystyle (x_2, y_2)$ and $\displaystyle \theta_2$. Then the output angle is the difference between these two angles: $\displaystyle \theta_2 - \theta_1$; or, if this is negative, $\displaystyle 360^o +\theta_2 - \theta_1$.

So how do we find the angle $\displaystyle \theta_1$? The first thing to do is to calculate the acute angle that the line joining the origin to $\displaystyle (x_1, y_1)$ makes with the horizontal. If this angle is denoted by $\displaystyle \phi_1$, then $\displaystyle \phi_1=\arctan\left|\frac{y_1}{x_1}\right|$. Note that:
You will need to check for $\displaystyle x_1=0$ to avoid a division by zero error.

If $\displaystyle x_1\ne0, 0\le\phi_1<90$.
Then the value of $\displaystyle \theta_1$ will depend upon which quadrant $\displaystyle (x_1, y_1)$ is in; as follows:
QI: $\displaystyle x_1>0, y_1>0: \theta_1 = 360-\phi_1$

QII: $\displaystyle x_1<0, y_1>0: \theta_1 = 180+\phi_1$

QIII: $\displaystyle x_1<0, y_1<0: \theta_1 = 180-\phi_1$

QIV: $\displaystyle x_1>0, y_1<0: \theta_1 = \phi_1$
Also, if $\displaystyle x_1 = 0$ then:
If $\displaystyle y_1>0, \theta_1 = 270$

If $\displaystyle y_1<0, \theta_1 = 90$
Clearly you find $\displaystyle \phi_2, \;\theta_2$ in the same way.

I hope that makes sense. (Drawing a diagram should help to clarify things.)

5. Thanks. I'll play around with it some and see if that makes any sense =)

So how do we find the angle $\displaystyle \theta_1$? The first thing to do is to calculate the acute angle that the line joining the origin to $\displaystyle (x_1, y_1)$ makes with the horizontal. If this angle is denoted by $\displaystyle \phi_1$, then $\displaystyle \phi_1=\arctan\left|\frac{y_1}{x_1}\right|$. Note that:
You will need to check for $\displaystyle x_1=0$ to avoid a division by zero error.

If $\displaystyle x_1\ne0, 0\le\phi_1<90$.
Then the value of $\displaystyle \theta_1$ will depend upon which quadrant $\displaystyle (x_1, y_1)$ is in; as follows:
QI: $\displaystyle x_1>0, y_1>0: \theta_1 = 360-\phi_1$

QII: $\displaystyle x_1<0, y_1>0: \theta_1 = 180+\phi_1$

QIII: $\displaystyle x_1<0, y_1<0: \theta_1 = 180-\phi_1$

QIV: $\displaystyle x_1>0, y_1<0: \theta_1 = \phi_1$
Also, if $\displaystyle x_1 = 0$ then:
[INDENT]If $\displaystyle y_1>0, \theta_1 = 270$

Well today in math we actually went over the arctan thing required to calculate the acute angle but what I'm not sure of is the four quadrant thing that you posted above. Sorry, I'm just so lost.

7. Hello chili5
Originally Posted by chili5
Well today in math we actually went over the arctan thing required to calculate the acute angle but what I'm not sure of is the four quadrant thing that you posted above. Sorry, I'm just so lost.
Have a look at the attached diagram. I have indicated the four quadrants, and as an example the line in QIII with its angles $\displaystyle \phi$ and $\displaystyle \theta$. You'll see that in this case
$\displaystyle \theta = 180-\phi$
If you look at the acute angles the lines in the other three quadrants make with the $\displaystyle x$-axis, and then the clockwise angles from the positive direction of the $\displaystyle x$-axis, I think you'll see where the other equations I gave you come from.