6
\$\begingroup\$

I know that atan2 gives me the absolute angle of any vector. But it doesn't give a value from 0 to 360 degrees. Instead, it gives a value (if I'm not mistaken), between 0 and 180, or between 0 and -180. For example, instead of giving angle 270, it will say -90.

This is a little confusing for me. Let me try to describe how I think this works, and please tell me if I'm correct. Actually, I'll draw it.

enter image description here

Is this correct? I'm having a little trouble understanding this. Thanks

\$\endgroup\$
3
  • \$\begingroup\$ You've read the wikipedia page right? Check out the illustration section. \$\endgroup\$
    – House
    Feb 1, 2014 at 1:46
  • \$\begingroup\$ This is in radians, doesn't really help me. I always convert the radians to degrees. \$\endgroup\$ Feb 1, 2014 at 1:54
  • \$\begingroup\$ The blue part of your drawing is wrong. \$\endgroup\$ Feb 1, 2014 at 10:46

4 Answers 4

7
\$\begingroup\$

From the Wikipedia page:

Details of atan2 implementation

It is returning the shortest angle of rotation from the positive x direction to the point, where a positive angle indicates the counter-clockwise direction. Therefore it makes sense that it would only return values between 0 and 180, since anything greater than 180 can be 'better' approached by a smaller angle in the opposite direction.

Returning -90 instead of 270 also makes sense now, since moving 90 degrees clockwise is less than moving 270 degrees counter-clockwise to reach the same point. This also suggests that your interpretation has mixed up the positive and negative 90 degree directions but is otherwise fine.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks. Is there a way to convert atan2 values to standard 360 degrees system, without actually changing the angle, only how it's written? \$\endgroup\$ Feb 1, 2014 at 1:55
  • \$\begingroup\$ Well, you can multiply the result by (180/pi) to convert the magnitude to degrees. If it's negative then you would have to add 360 to it afterwards (assuming you want a range of 0 to 360.) \$\endgroup\$
    – Ryan
    Feb 1, 2014 at 1:58
1
\$\begingroup\$

The referenced Wikipedia article provides the logic of the atan2 function, which has been optimized computationally (it works faster than implementing the statement explicitly). To convert to 0-to-360 degree values, first you must adjust the range of atan2 results, then convert to degrees, shown here as a vectorized operation in pseudo-code:

rad = atan2(y,x)
rad[rad < 0] = rad[rad < 0] + 2*pi
deg = rad*(180/pi)

Hope that is helpful.

\$\endgroup\$
1
\$\begingroup\$

Just to answer you question simply: arctangent function atan2(y,x) uses the sign of BOTH parameters to determine the quadrant of the angle in the range -PI to +PI (-180 deg to +180 deg). If you want degrees in the range 0 to 360 do this:

struct vector {double x, double y}; // define a 2D vector
vector vec{ 3.0,2.0 }; // sets x and y values
const double convertRadiansToDegrees = 180.0 / 3.14159265359;

double resultInDegrees = atan2(vec.y, vec.x) * convertRadiansToDegrees + 180.0; // 0 to 360 degrees

Note that there is a faster but less accurate version for floats and not doubles called atan2f(y,x);

\$\endgroup\$
0
\$\begingroup\$

The following example isn't using vectors but rather simple unit-circle implemented on javascript: https://github.com/Drooids/unit-circle.

More particularly below this line: https://github.com/Drooids/unit-circle/blob/e608e67ed52db89025e0fc4c6900fddcdab01608/unit-circle.js#L391

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .