2
$\begingroup$

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.

  • 0
    let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/4606/discussion-between-xyrthon-and-rahul-narain)2012-08-23

2 Answers 2

3

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

  • 0
    I don't think you need rep to [click the accepted answer check mark](http://math.stackexchange.com/faq#howtoask).2012-08-23
0

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

  • 0
    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.2012-08-23