math.atan2()

类型 函数
math.*
返回值 数字
修订版 版本 2024.3703
关键字 atan2、弧切
另请参见 math.atan()
math.tan()

概述

返回 y/x 的弧切(以弧度为单位),但使用两个参数的符号来查找结果的象限。它还能正确处理 x0 的情况。

结果将在 [-pi,+pi] 弧度区间内。

在转换矩形坐标为极坐标时,此函数很有用。math.atan2() 使用两个参数的符号将结果放置到正确的象限内,并且当其中一个参数为 0 或非常接近 0 时也能生成正确的值。

语法

math.atan2( y, x )
y (必需)

数字。表示 y 坐标的数字。

x (必需)

数字。表示 x 坐标的数字。

示例

print(math.atan2(1, 0))     -->  pi/2
print(math.atan2(-1, 0))    -->  -pi/2
print(math.atan2(0, 1))     -->  0
print(math.atan2(0, -1))    -->  pi