10

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

patientia
  • 5
  • 2
user3150201
  • 1,901
  • 5
  • 26
  • 29
  • Can you provide an example of what you mean because your question doesn't make sense to me as written? – jmcilhinney Feb 01 '14 at 02:02
  • 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 Feb 01 '14 at 03:36
  • 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 Feb 01 '14 at 03:52
  • @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 Feb 01 '14 at 11:29
  • @user3150201: Yes, it is that simple. Actually that is exactly what I suggested in my answer to your previous question (using radians instead of degrees). – Martin R Feb 01 '14 at 12:01

4 Answers4

11

Try this:

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

if (theta < 0.0) {
    theta += 360.0;
}
andand
  • 17,134
  • 11
  • 53
  • 79
4

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;
}
Denny
  • 41
  • 1
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

Community
  • 1
  • 1
zephos2014
  • 331
  • 1
  • 2
  • 13
  • 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. – andand Jun 11 '15 at 19:45
0

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)))
Unheilig
  • 16,196
  • 193
  • 68
  • 98