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;
}
Share:
15,768
user3150201
Author by

user3150201

Updated on June 04, 2022

Comments

  • user3150201
    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 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

  • andand
    andand almost 9 years
    You 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) the y coordinate is the first argument since the common pattern for it is to use it in place of atan(y/x). Other programming languages may use a different convention.
  • J.M. Janzen
    J.M. Janzen over 7 years
    Incredibly simple solution. Thank you.