JavaScript atan2() 方法


定义和用法

atan2() 方法可返回从 x 轴到点 (x,y) 之间的角度。

语法
  1. Math.atan2(y,x)
参数 描述
x 必需。指定点的 X 坐标。
y 必需。指定点的 Y 坐标。
返回值

-PI 到 PI 之间的值,是从 X 轴正向逆时针旋转到点 (x,y) 时经过的角度。


提示和注释

注释:请注意这个函数的参数顺序,Y 坐标在 X 坐标之前传递。


实例

下面这个例子可通过 atan2() 方法返回不同 (x,y) 点的角度:

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. document.write(Math.atan2(0.50,0.50) + "<br />")
  5. document.write(Math.atan2(-0.50,-0.50) + "<br />")
  6. document.write(Math.atan2(5,5) + "<br />")
  7. document.write(Math.atan2(10,20) + "<br />")
  8. document.write(Math.atan2(-5,-5) + "<br />")
  9. document.write(Math.atan2(-10,10))
  10. </script>
  11. </body>
  12. </html>

输出:

  1. 0.7853981633974483
  2. -2.356194490192345
  3. 0.7853981633974483
  4. 0.4636476090008061
  5. -2.356194490192345
  6. -0.7853981633974483

分类导航