JavaScript atan2()函数未提供预期结果 [英] JavaScript atan2() function not giving expected results

查看:146
本文介绍了JavaScript atan2()函数未提供预期结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,极坐标从0到π 到2 π (实际上在2 π 之前,因为它等于0再次)。但是,当使用JavaScript atan2()函数时,我得到了另一个奇怪的范围:

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

如您所见,到达后π (180°),它会跳到– π (– 180°),然后又回到0。如何使用范围 {0,...,2 π } ,而不是 {– π ,...,&pi ; } ?我一直在尝试使用每种计算来固定这些值,但是我也想知道为什么JavaScript选择这个范围而不是典型的极坐标范围。谢谢!

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!

推荐答案

atan2 返回角度非常标准范围;例如,这就是C标准库中的 atan2 的作用。

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.

如果要使用0..2pi -pi..pi,测试结果是否为负,如果为2pi,则添加2pi。

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

这篇关于JavaScript atan2()函数未提供预期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆