# How do I find clockwise angle of a point from the negative x-axis?

Title pretty much says it. How can I rotate a point around the origin and find the clockwise angle of the point from the negative x-axis? I tried using the atan(height/width), but that gives me the angle in the specific quadrant, not from the negative x-axis.

Edit I got some good advice in the comments. What this question is really asking is, "How can I calculate the clockwise angle between a vector and the negative x-axis?". I was looking for a programming answer, but both perspectives (programming and not) are answered below.

-
Do you know about rotation matrices? –  EuYu Aug 23 '12 at 17:56
Try atan2. –  Rahul Aug 23 '12 at 17:57
More or less. Maybe I wasn't clear enough. I'm given a point p1, and a point p2. Point p1 will always be at the origin, and p2 can be anywhere. What I need to find out is the clockwise angle from the negative x-axis, and p2. I'm not sure how a rotation matrix would help me solve this since it's the theta of the rotation I need to figure out. –  Xyrthon Aug 23 '12 at 17:59
@RahulNarain I tried both atan2 and atan, but I'm running into the same problem. –  Xyrthon Aug 23 '12 at 18:00
Really? $\pi - \operatorname{atan2}(y,x)$ doesn't do what you want? –  Rahul Aug 23 '12 at 18:11

Most programming languages provide an $\operatorname{atan2}$ function that deals with quadrants correctly. If you have a point $(x,y) \ne (0,0)$, then $\operatorname{atan2}(y,x)$ gives the counter-clockwise angle from the positive $x$-axis to $(x,y)$, in the range $(-\pi,\pi]$. Since you want the clockwise angle from the negative $x$-axis, it is enough to observe that when $\operatorname{atan2}(y,x) = 0$ the angle you want is $\pi$, and when it is $\pi/2$ you want $\pi/2$, so in general what you want is $\pi-\operatorname{atan2}(y,x)$, which lies in the range $[0,2\pi)$.

-
Drat, not enough rep to mark ya up, but as soon as I get enough rep, I'll make sure to indicate this is what I was looking for. –  Xyrthon Aug 23 '12 at 19:30
I don't think you need rep to click the accepted answer check mark. –  Rahul Aug 23 '12 at 19:42

Leverage the dot product. If $\vec a = (a_1,a_2,\dots,a_n)$ is any point, we have two ways to compute the dot product with any vector $\vec b=(b_1, b_2, \dots, b_n)$:

$$\vec a \cdot \vec b = (a_1 b_1, a_2b_2, \dots, a_nb_n)$$ $$\vec a \cdot \vec b = \lVert \vec a \rVert \lVert \vec b \rVert \cos \theta$$ where $\theta$ is the angle between $\vec a$ and $\vec b$ (the primary angle, i.e. $\theta \in [0,\pi]$). So now, taking $\vec b = - \hat i$ (a unit vector on the negative x-axis) we have that

$$\theta = \arccos\left(\frac {\vec a \cdot \vec b} {\lVert \vec a \rVert \lVert \vec b \rVert}\right )$$

Now by the first way of computing the dot product we have that $\vec a \cdot \vec b = -a_1$, and since $\lVert \vec b \rVert =1$ we have that $$\theta = \arccos \left( \frac {-a_1} {\lVert \vec a \rVert} \right )\,.$$

Now you mentioned that you wanted the clockwise angle in $\Bbb R ^2$, so you just need to add $\pi$ if $\vec a$ is below the x-axis, i.e. if $a_2<0$. The value $a_2/|a_2|$ is equal to the sign of $a_2$, so let's consider $\frac {1-a_2/|a_2|}{2}$ which will equal $1$ when $a_2<0$ and $0$ when $a_2>0$. Thus the function you want in total is just

$$f(\vec a)=\arccos\left ( \frac {-a_1} {\lVert \vec a \rVert} \right )+\frac {1-a_2/|a_2|}{2} \pi$$

(Note that the heading of your question, as well as the initial description, gives a much different impression of what you are looking for.)

-
I'm not sure how to make what I was looking for any more clear. Rahul Narain answered my question just like I asked it in the comments. I learned that I need to be more thorough when explaining my problem, but this is my very first post here, so I'll attempt to do that in the future. –  Xyrthon Aug 23 '12 at 18:44
@Xyrthon As a suggestion - when I read "how do I rotate a point around the origin" I, like EuYu, initially thought that what you wanted was a function which rotated a point around the origin. This is precisely what a rotation matrix would do. Perhaps "How can I calculate the clockwise angle between a vector and the negative x-axis" would more accurately describe your question. In addition, it would be helpful if you said that you were using this in a computer program, as that suggests the answer Rahul provided. Your question was fine, I would just have phrased it differently. –  process91 Aug 23 '12 at 18:56
Ahhh, gotcha. Thank you. –  Xyrthon Aug 23 '12 at 19:28
I'll up vote you as soon as I gain enough rep as well, because this is what I'd be looking for if I wasn't writing a program. –  Xyrthon Aug 23 '12 at 19:31