Convert atan2 value to standard 360-degree-system value
15,768
Solution 1
Try this:
double theta = Math.toDegrees(atan2(y, x));
if (theta < 0.0) {
theta += 360.0;
}
Solution 2
To convert it to a North referenced 0 - 360 degree value:
double degrees = 90.0d - Math.toDegrees( Math.atan2( y, x ) );
if( degrees < 0.0d )
{
degrees += 360.0;
}
Author by
user3150201
Updated on June 04, 2022Comments
-
user3150201 almost 2 years
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 between0
and180
degrees or between0
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
-
andand almost 9 yearsYou should look into how
atan2(y, x)
works in the various programming languages in which it is implemented. It takes into account the quadrant of the point(x, y)
and returns values in the range(-pi, pi]
not[-pi/2, pi/2]
. When doing so, please note the coordinate order. In many cases (C and C++ for example) they
coordinate is the first argument since the common pattern for it is to use it in place ofatan(y/x)
. Other programming languages may use a different convention. -
J.M. Janzen over 7 yearsIncredibly simple solution. Thank you.