Question

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

Was it helpful?

Solution

Try this:

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

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

OTHER TIPS

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

According to what I learned in my trig class, the above answers are incorrect. For example, if you have a vector intersecting point (-1,-1) at an angle of 225 degrees (standard position), the tangent will be positive 1, which yields an arc tangent of 45 degrees. This will not be noticed by the above solutions, and the angle of the vector will be wrong. That is why arc tangent's formula is different depending on the situation. You have to have an idea of what the angle is going to be first.

Recall that we can apply trig functions to any angle, including large and negative angles. But when we consider the inverse function we run into a problem, because there are an infinite number of angles that have the same tangent. For example 45° and 360+45° would have the same tangent. For more on this see Inverse trigonometric functions.

To solve this problem, the range of inverse trig functions are limited in such a way that the inverse functions are one-to-one, that is, there is only one result for each input value.

From http://www.mathopenref.com/arctan.html

A formula that gives an angle from 0 to 360 degrees.

f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)

-(180/pi())*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top