2
$\begingroup$

I've looked at Wikipedia and don't really understand what I'm reading or how to implement atan2(). I was hoping someone could point me to a better resource or give me an example of how to use atan2().

For some context, I'm asking about atan2() because I have a a homework asking:

Write the solution for each of the following in terms of atan2() before giving your final answer (e.g., = atan2(a,b) = ...

An example problem: *If

sin $\theta$ = -1/2

and

cos $\theta$ = $\sqrt{3}/2$

what is $\theta$ ?

  • 1
    $\cos\,\theta$ is always between $-1$ and $1$ for real angles, so I can't see how it'd be equal to $3/2$. Anyway, two-argument arctangent is essentially the same as the arctangent of a certain ratio. Have you already taken up polar coordinates?2011-09-23
  • 1
    You might want to think a bit about your example problem. The value of $\cos(\theta)$ is always between $-1$ and $+1$, and so $\cos(\theta) = 3/2$ is not right.2011-09-23
  • 0
    Good catch, I forgot the square root :)2011-09-23
  • 1
    But now you have too much of a square root. It should be $\sqrt{3}/2$ -- otherwise $\sin^2+\cos^2$ doesn't hold.2011-09-23
  • 0
    See edits. End of a long week... haha.2011-09-23
  • 0
    Let me repeat my question again: have you taken up polar coordinates?2011-09-23
  • 0
    I have not, I remember doing it in Calc III along with spherical but its been awhile.2011-09-23
  • 0
    I think that atan2 is a totally sick concept and that mathematicians should refuse to adopt it. There is the function $(x,y)\mapsto{\rm arg}(x,y)$ defined in the punctured plane with values in ${\mathbb R}/(2\pi{\mathbb Z})$ giving the polar angle of the point $(x,y)\ne(0,0)$ up to multiples of $2\pi$, and there is its principal value Arg giving the polar angle in the interval $]-\pi,\pi[$ for points $\ne(-r,0)$, $r\geq0$.2011-09-23
  • 4
    @Christian, atan2 is nothing more or less than how most programming language spell the name of the principal-argument function. What's sick about that?2011-09-23
  • 1
    You will need to check the order of the arguments for `atan2` in your particular system. If `atan2(1,0)=0` then you want $\text{atan2}(r\sin(\theta),r\cos(\theta))$; otherwise `atan2(0,1)=0` and you want $\text{atan2}(r\cos(\theta),r\sin(\theta))$.2011-09-23
  • 0
    @Henry raises an excellent point. FORTRAN and C++ for instance take the y-coordinate first. *Mathematica* takes the x-coordinate first. But wait... I know *Mathematica* takes the x-coordinate first, and `ArcTan[1, 0] == 0`. Hmm... maybe it's backwards?2011-09-24
  • 0
    @Christian: you say $\arg(x,y)$, I say $\arctan(x,y)$, other people say $\mathrm{atan2}(y,x)$. "To-may-to, to-mah-to" and all that jazz...2011-09-24

2 Answers 2

8

Are you asking how to use atan2, or how to implement it?

$\mathrm{atan2}(y,x)$ gives you the angle between the x-axis and the vector $(x,y)$, usually in radians and signed such that for positive $y$ you get an angle between $0$ and $\pi$, and for negative $y$ the result is between $-\pi$ and $0$.

It's named that way because when $x$ is positive, $\mathrm{atan2}(y,x)=\tan^{-1}(\frac{y}{x})$, which also explains (more or less) why the arguments are "backwards".

So if you have the vector $(\sqrt 3/2, -1/2)$ you can get its angle by $\mathrm{atan2}(-1/2, \sqrt 3/2)$ which is $-\pi/6$. You then know that for some positive $r$ it holds that $$(\sqrt 3/2, -1/2) = r(\cos(-\pi/6), \sin(-\pi/6))$$ (in fact it turns out that $r=1$ in this case, but atan2 does not tell you that).

As for implementing atan2 in the (these days somewhat unusual) case that you don't have a hardware implementation available, I think CORDIC should give excellent results with little effort. Alternatively, you can sacrifice precision for table space by splitting into octants, dividing the numerically smaller argument by the larger, and using a polynomial approximation of the arctangent between 0 and 1.

  • 0
    Heh, I picked up something useful from the ol' wiki. You can "emulate" `atan2()` by picking the appropriate root $t^\ast$ of the quadratic $yt^2+2xt-y=0$ and then $\arctan(x,y)=2\arctan\,t^\ast$. One might be able to squeeze a bit more accuracy if one uses a rational approximation instead of a polynomial one.2011-09-24
  • 0
    Sorry to come back, but does r = the angle?2011-09-24
  • 0
    No, $r$ is the absolute length of the vector.2011-09-24
  • 0
    Ah, I got -30 for the angle, does that sound right?2011-09-24
  • 0
    Yes, $-30^\circ = -\pi/6$.2011-09-24
  • 0
    Do you know if when we use `atan2()` within OpenCV, the angle is from the `x`-axis?2017-03-29
4

The slope of the straight line from the origin $(0,0)$ to the point $(a,b)$ is $b/a$ which takes on values from $-\infty$ to $+\infty$. But if you look at the angle between the line from $(0,0)$ to $(a,b)$, this angle can be anything from $0$ to $2\pi$. In particular, the line from $(0,0)$ to $(a,b)$ has the same slope as the line from $(0,0)$ to $(-a, -b)$ but the angle differs by $\pi$. The atan2 function was developed to take into account this difference. If you specify the angle as $\arctan(b/a)$ or $\text{atan}(b/a)$, you will get an answer between $-\pi/2$ and $+\pi/2$, that is, atan() cannot distinguish between the points $(a,b)$ and $(-a,-b)$ in terms of the angle. But if you specify the angle as: $\text{atan2}(a,b)$, you will get an answer between $0$ and $2\pi$ because the information about the signs of $a$ and $b$ (which tells you whether the point is in the first, second, third, or fourth quadrants) is not lost.