Вопрос

Normally, polar coordinates go from 0 to π to 2π (just before 2π really, as it equals 0 again). However, when using the JavaScript atan2() function, I'm getting a different, weird range:

Cartesian X | Cartesian Y | Theta (θ)
===========================================================
     1      |      0      | 0 (0 × π)
     1      |      1      | 0.7853981633974483 (0.25 × π)
     0      |      1      | 1.5707963267948966 (0.5 × π)
    -1      |      1      | 2.356194490192345 (0.75 × π)
    -1      |      0      | 3.141592653589793 (1 × π)
    -1      |     -1      | -2.356194490192345 (-0.75 × π)
     0      |     -1      | -1.5707963267948966 (-0.5 × π)
     1      |     -1      | -0.7853981633974483 (-0.25 × π)

As you can see, after it reaches π (180°), it jumps down to –π (–180°), and proceeds back up to 0. How can I get it to use the range {0, ..., 2π} instead of {–π, ..., π}? I've been trying to think of every calculation to "fix" the values, but I would also like to know why JavaScript chooses this range instead of the typical polar range. Thanks!

Это было полезно?

Решение

It's pretty standard for atan2 to return angles in that range; for instance, that's what the atan2 in the C standard library does.

If you want 0..2pi instead of -pi..pi, test whether the result is negative and add 2pi if it is.

Другие советы

It's atan2(y, x) not atan2(x, y)

If the result is negative just add 2 * PI to the result.

function atan2Normalized(x,y) {
    var result = Math.atan2(x, y);
    if (result < 0) {
        result += (2 * Math.PI);
    }
    return(result);
}

How can I get it to use the range {0, ..., 2π} instead of {–π, ..., π}?

The Math.atan2(Y,X) function will give different results for -0 than for 0.

For example, where Math.atan2(0, -1) returns 3.141592653589793, Math.atan2(-0, -1) returns -3.141592653589793.

You can use this fact to simplify the equation for getting a result in the range of [0, 2π).

Notice what you get if you negate the X and Y values:

   X   |   Y   |  -X   |  -Y   |  Math.atan2(-Y,-X)
===========================================================
   1   |   0   |  -1   |  -0   | -1.00π (-3.141592653589793)
   1   |   1   |  -1   |  -1   | -0.75π (-2.356194490192345)
   0   |   1   |  -0   |  -1   | -0.50π (-1.5707963267948966)
  -1   |   1   |   1   |  -1   | -0.25π (-0.7853981633974483)
  -1   |   0   |   1   |  -0   |  0.00π (0)
  -1   |  -1   |   1   |   1   |  0.25π (0.7853981633974483)
   0   |  -1   |  -0   |   1   |  0.50π (1.5707963267948966)
   1   |  -1   |  -1   |   1   |  0.75π (2.356194490192345)

If you then add π to the result, you will get a value in the range [0, 2π).

Therefore you can use:

var theta = Math.atan2(-Y, -X) + Math.PI;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top