javascript atan2() 计算X 轴到某一点的角度

时间:2016-01-10
javascript atan2() 方法可返回从 x 轴到点 (x,y) 之间的角度。本函数有两个参数,第一个参数指定点的 X 坐标,第二个参数指定点的 Y 坐标,注意这个函数的参数顺序,Y 坐标在 X 坐标之前传递。本文章向大家讲解atan2函数的基本语法及使用实例。需要的码农可以参考一下。

atan2 方法返回由 X 轴到 (y,x) 点的角度(以弧度为单位)。

基本语法:

Math.atan2(y, x)

参数介绍

参数 描述
x 必需。描述笛卡儿 x 坐标的数值表达式。
y 必需。描述笛卡儿 y 坐标的数值表达式。

返回值

返回值在 -pi 和 pi 之间,表示提供的 (y,x) 点的角度。

注意:

  1. 这个函数的参数顺序,Y 坐标在 X 坐标之前传递。
  2. 函数中如果有一个参数的值为非数字,该函数将返回NaN

实例

<script type="text/javascript">
document.write(Math.atan2(0.50,0.50) + "<br />")
document.write(Math.atan2(-0.50,-0.50) + "<br />")
document.write(Math.atan2(5,5) + "<br />")
document.write(Math.atan2(10,20) + "<br />")
document.write(Math.atan2(-5,-5) + "<br />")
document.write(Math.atan2(-10,10) + "<br />")
document.write(Math.atan2("s","y") + "<br />")
</script>

在线运行

运行结果:

0.7853981633974483
-2.356194490192345
0.7853981633974483
0.4636476090008061
-2.356194490192345
-0.7853981633974483
NaN