概述
Math.atan2() 返回从原点(0,0)到(x,y)点的线段与x轴正方向之间的平面角度(弧度值),也就是Math.atan2(y,x)
语法
Math.atan2(y, x)
参数
-
y, x - 数值
描述
atan2 方法返回一个 -pi 到 pi 之间的数值,表示点 (x, y) 对应的偏移角度。这是一个逆时针角度,以弧度为单位,正X轴和点 (x, y) 与原点连线 之间。注意此函数接受的参数:先传递 y 坐标,然后是 x 坐标。
atan2 接受单独的 x 和 y 参数,而 atan 接受两个参数的比值。
由于 atan2 是 Math 的静态方法,所以应该像这样使用:Math.atan2(),而不是作为你创建的 Math 实例的方法。
示例
例子:使用 Math.atan2
Math.atan2(90, 15) // 1.4056476493802699 Math.atan2(15, 90) // 0.16514867741462683Math.atan2( ±0, -0 )//±PI.Math.atan2( ±0, +0 )//±0.Math.atan2( ±0, -x )//±PIfor x > 0.Math.atan2( ±0, x )//±0for x > 0.Math.atan2(-y, ±0 )// -PI/2for y > 0.Math.atan2(y, ±0 )//PI/2for y > 0.Math.atan2( ±y, -Infinity )//±PIfor finite y > 0.Math.atan2( ±y, +Infinity )//±0for finite y > 0.Math.atan2( ±Infinity, x )//±PI/2for finite x.Math.atan2( ±Infinity, -Infinity )//±3*PI/4.Math.atan2( ±Infinity, +Infinity )//±PI/4.
规范
| 规范版本 | 规范状态 | 注解 |
|---|---|---|
| ECMAScript 1st Edition. Implemented in JavaScript 1.0 | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) Math.atan2 |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) Math.atan2 |
Standard |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
相关链接
- The
Mathobject it belongs to. Math.acos()Math.asin()Math.atan()Math.cos()Math.sin()Math.tan()