Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert atan2 value to standard 360-degree-system value

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

like image 453
user3150201 Avatar asked Feb 01 '14 01:02

user3150201


People also ask

Is atan2 in degrees or radians?

atan2() function returns the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to the point (x, y), for Math.

How is atan2 calculated?

Arc tangent of two numbers, or four-quadrant inverse tangent. ATAN2(y,x) returns the arc tangent of the two numbers x and y. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result. The result is an angle expressed in radians.

What is the range of atan2?

The atan2() function returns a value in the range -π to π radians. If both arguments of the atan2() function are zero, the function sets errno to EDOM, and returns a value of 0.

What is atan2 in trigonometry?

The quantity atan2(y,x) is the angle measure between the x-axis and a ray from the origin to a point (x, y) anywhere in the Cartesian plane. The signs of x and y are used to determine the quadrant of the result and select the correct branch of the multivalued function Arctan(y/x).


2 Answers

Try this:

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

if (theta < 0.0) {
    theta += 360.0;
}
like image 95
andand Avatar answered Sep 20 '22 01:09

andand


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;
}
like image 41
Denny Avatar answered Sep 19 '22 01:09

Denny