JavaScript atan2() function not giving expected results

人走茶凉 提交于 2019-12-14 03:45:20

问题


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!


回答1:


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.




回答2:


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




回答3:


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



回答4:


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;


来源:https://stackoverflow.com/questions/10343448/javascript-atan2-function-not-giving-expected-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!