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 $(x_1, y_1)$ makes an angle $\theta_1$ with the positive direction of the $x$-axis, measured clockwise, with $0\le\theta<360^o$; and similarly for $(x_2, y_2)$ and $\theta_2$. Then the output angle is the difference between these two angles: $\theta_2 - \theta_1$; or, if this is negative, $360^o +\theta_2 - \theta_1$.

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

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

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

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

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

If $y_1<0, \theta_1 = 90$
Clearly you find $\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 $\theta_1$? The first thing to do is to calculate the acute angle that the line joining the origin to $(x_1, y_1)$ makes with the horizontal. If this angle is denoted by $\phi_1$, then $\phi_1=\arctan\left|\frac{y_1}{x_1}\right|$. Note that:
You will need to check for $x_1=0$ to avoid a division by zero error.

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

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

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

QIV: $x_1>0, y_1<0: \theta_1 = \phi_1$
Also, if $x_1 = 0$ then:
[INDENT]If $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 $\phi$ and $\theta$. You'll see that in this case
$\theta = 180-\phi$
If you look at the acute angles the lines in the other three quadrants make with the $x$-axis, and then the clockwise angles from the positive direction of the $x$-axis, I think you'll see where the other equations I gave you come from.