Convert atan2 value to standard 360-degree-system value

user3150201 picture user3150201 · Feb 1, 2014 · Viewed 15.1k times · Source

Lets say I'm using atan2 to get the angle between two vectors.

atan2 gives a value in radians. I convert it to degrees using a built in function in Java. This gives me a value between 0 and 180 degrees or between 0 and -180 (the nature of atan2).

Is there a way to convert the value received with this function (after it's been converted to degrees), to the standard 360-degree-system, without changing the angle - only the way it's written? It would make it easier for me to work with.

Thanks

Answer

andand picture andand · Feb 1, 2014

Try this:

double theta = Math.toDegrees(atan2(y, x));

if (theta < 0.0) {
    theta += 360.0;
}