每个角度对应于单位圆上的一个点(单位圆是以原点为圆心,半径为 1 的唯一圆;即单位圆是满足 的点的集合x^2 + y^2 = 1
)。对应关系如下:给定角度theta
,theta
对应点(cos theta, sin theta)
。为什么(cos theta, sin theta)
住在单位圆上?因为大家最喜欢的身份
cos^2 theta + sin^2 theta = 1.
即x = cos theta
和y = sin theta
,该点(x, y)
满足x^2 + y^2 = 1
,因此(x, y)
在单位圆上。
为了扭转这一点,给定单位圆上的一个点,您可以通过使用反正切(也许您知道arctan
或atan
有时称为tan
-1
)来找到角度。准确地说,(x, y)
在单位圆上给定,您可以(x, y)
通过计算找到对应的角度theta = arctan(y / x)
。
当然,这里也有一些乱七八糟的细节。该函数arctan
无法区分输入之间的差异(x, y)
,(-x, -y)
因为y / x
和(-y / -x)
具有相同的符号。此外,arctan
无法处理 where 的输入x = 0
。因此,我们通常通过定义atan2
将为我们处理这些杂乱细节的函数来处理这些问题
atan2(y, x) = arctan(y / x) if x > 0
= pi + arctan(y / x) if y >= 0, x < 0
= -pi + arctan(y / x) if y < 0, x < 0
= pi / 2 if y > 0, x = 0
= -pi / 2 if y < 0, x = 0
= NaN if y = 0, x = 0
在 C# 中,Math.Atan
是arctan
我上面提到Math.Atan2
的函数,也是我上面提到的函数atan2
。