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
    Do you know about rotation matrices?2012-08-23
  • 0
    Try [atan2](https://en.wikipedia.org/wiki/Atan2).2012-08-23
  • 0
    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.2012-08-23
  • 0
    @RahulNarain I tried both atan2 and atan, but I'm running into the same problem.2012-08-23
  • 0
    Really? $\pi - \operatorname{atan2}(y,x)$ doesn't do what you want?2012-08-23
  • 0
    No, that gives me the angle from the x-axis, but not the clockwise angle from the negative x-axis. For example, if my second point is in in the third quadrant I get an angle between 0 and PI/2, but what I want is a much larger angle, or more specifically the angle from the negative x-axis to the third quadrant.2012-08-23
  • 0
    Sorry, I believe what you said works for the third quadrant, but not any of the other ones. It only works for one quadrant.2012-08-23
  • 0
    Hold on, you might be right. Let me test it more first.2012-08-23
  • 0
    Make up your mind, man!!! :p2012-08-23
  • 0
    How can I mark your comment as the correct solution? My point wasn't really at the origin, which was my bad for not explaining. I was taking the absolute value of change to the origin, which was what was throwing me off. You're persistence was what helped me realize I was mistaking. Thank you. Please let me know if there is a way I can increase your reputation?2012-08-23
  • 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
    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.2012-08-23
  • 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'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.2012-08-23
  • 0
    @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.2012-08-23
  • 0
    Ahhh, gotcha. Thank you.2012-08-23
  • 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