0
$\begingroup$

I have two points, one is always at the origin (0,0), and the other can be anywhere else in the world. I'd like to find the angle between them, respective to the horizontal axis.

   |     2    |    /    |   /     |  /      | /         |/ a   ---1-------------- (horizontal axis)    |  a = angle (~50 degrees, counter clockwise) 

In the above I would construct a right triangle and use sohcahtoa to figure out the missing angle I want, but it gets a bit ugly when the second point is in a different quadrant like in this case:

2    | \    |  \   |   \  |    \a|a        \|a   ---1--------------      |      |  a = angle (~135, counter clockwise) 

I just end up with a bunch of different cases depending on what quadrant the second point is in. I'm thinking there must be a much simpler, general solution. This is kind of like trying to find the angle between a point on the edge of a circle and its center, respective to the origin's horizontal axis.

I'm using a C-like language, and it has an atan2() function available. I think these mostly work the same way, would I just feed it the x, y coordinate of my second point and I get back that angle in radians?

Thank you

  • 0
    Yes, two-argument arctangent is intended precisely for your situation. You didn't specify what specific language you have, so: check the docs to see whether it takes the vertical coordinate $y$ first (`atan2(y,x)`) or the horizontal coordinate $x$ first (`atan2(x,y)`).2011-10-20
  • 0
    One thing is generally agreed upon for two-argument arctangent: the angles (in radians) returned are within the interval $(-\pi,\pi]$.2011-10-20
  • 0
    Hey, yes using java and it wants atan(y,x). Question though - are there any special cases I need to worry about for input? Like if x or y equal zero? A return value in [−π,π] is fine I think - it'll just convert to +180 / -180, right? My rendering framework can handle that ok. Thanks!2011-10-20
  • 0
    `atan2()` is also designed to handle the case where *one* of the coordinates is zero. I haven't checked how `atan2(0,0)` evaluates in Java, but it's indeterminate (`NaN`) in some systems and zero in other systems. If you need results in degrees, just perform the usual conversions.2011-10-20
  • 0
    Hmm, I don't know what this means: resp the horizontal axis. How can the angle between the two vectors be respectively other than resp to the origin? If that was only some naming, then the correlation between two vectors are the cosine, and the angle between them the arccosine. And if the two point have the x/y-coordinates (x0,y0) and x1,y1) and the lengthes l0=sqrt(x0^2+y0^2), l1 similarly, and with this (c0,s0)=(x0,y0)/l0 and (c1,s1)=(x1,y1)/l1 then the cosine is c0*c1-s0*s1 and the sine is c0*s1+c1*s0. (and the tan=sin/cos) Then you can use the appropriate inverse function2011-10-20
  • 0
    @Gottfried: I believe this was just an error of expression. The intention seems to be to find the angle between the horizontal axis and the line/vector from the origin 1 to the point 2.2011-10-20
  • 1
    @wwww: What I don't understand: Why did you go to all the trouble of writing down this question and creating those ASCII diagrams when you already knew about the atan2 function and could have just tried it out to check whether it does what you want?2011-10-20
  • 0
    @joriki: I originally posted on stackoverflow but was directed here, one of the commenters said this is what atan2() is for. I wanted to ask for some help verifying. Thanks all!2011-10-20

2 Answers 2