8
votes

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

4
Can you provide an example of what you mean because your question doesn't make sense to me as written? - jmcilhinney
@jmcilhinney Please see my edit. - user3150201
The angle between two vectors is always between 0 and 180. The only way for it to be between -180 and 180 is if you've measured the second vector in relation to the first (because swapping the vectors will negate the angle you have). So, what do you mean by "the standard 360-degree-system"? Could you give some sample vectors? - Teepeemm
Are you saying that you want the angles less that zero to be represented in the range between 180 and 360? If so then just use a bit of simple arithmetic. - jmcilhinney
@jmcilhinney Yes, but without changing the actual angle, only how it's written. -90 degrees, the output of atan2, is actually 270 in the normal 360-system. Is the way to do that simply add 360 to any angle below zero? - user3150201

4 Answers

11
votes

Try this:

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

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

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

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

0
votes

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