# How does atan2 work when getting angle of a vector?

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.

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

• You've read the wikipedia page right? Check out the illustration section. – MichaelHouse Feb 1 '14 at 1:46
• This is in radians, doesn't really help me. I always convert the radians to degrees. – user3150201 Feb 1 '14 at 1:54
• The blue part of your drawing is wrong. – sam hocevar Feb 1 '14 at 10:46

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.

• Thanks. Is there a way to convert atan2 values to standard 360 degrees system, without actually changing the angle, only how it's written? – user3150201 Feb 1 '14 at 1:55
• 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.) – Ryan Feb 1 '14 at 1:58

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)


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

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