您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
Ready 发布于2014年08月05日 00:25

原创 JavaScript Math.atan2() 函数详解

3336 次浏览 读完需要≈ 4 分钟

内容目录

Math.atan2()函数用于返回从x轴到指定坐标点(x, y)的角度(以弧度为单位)。

该函数属于JS的内置全局对象Math所有主流浏览器均支持该函数

语法

静态函数Math.atan2()的语法如下:

Math.atan2( y, x )

参数

强烈注意:该函数的参数顺序,第一个参数是y坐标,第二个参数是x坐标,这与我们平常的写法(x, y)恰好相反。

参数 描述
y Number类型笛卡尔坐标系上指定坐标点的y坐标
x Number类型笛卡尔坐标系上指定坐标点的x坐标

返回值

返回值为Number类型。返回从x轴正方向通过逆时针旋转到达坐标点(x, y)所经过的角度(单位为弧度),返回值介于 [-π, π] 之间。

如果参数x或y不是数字,或无法转换为数字,则返回NaN

示例&说明

// 坐标 (5, -5)
document.writeln( Math.atan2( -5, 5 ) );	// -0.7853981633974483

// 坐标 (2.5, 1.5)
document.writeln( Math.atan2( 1.5, 2.5 ) );	// 0.5404195002705842

// 坐标 (1, 1)
document.writeln( Math.atan2( 1, 1 ) );		// 0.7853981633974483

// 坐标 (0, 0)
document.writeln( Math.atan2( 0, 0 ) );		// 0

// 坐标 (-1, 0)
document.writeln( Math.atan2( 0, -1 ) );	// 3.141592653589793

// 坐标 (-1, 1)
document.writeln( Math.atan2( 1, -1 ) );	// 2.356194490192345

// 坐标 ("CodePlayer", "365mini.com")
document.writeln( Math.atan2( "365mini.com", "CodePlayer" ) );	// NaN

运行代码

  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论

打开导航菜单