Atan2
在三角函数中,两个参数的函数<math>\operatorname{atan2}</math>是正切函数<math>\tan</math>的一个变种。对于任意不同时等于0的实参数<math>x</math>和<math>y</math>,<math>\operatorname{atan2}(y,x)</math>所表达的意思是坐标原点为起点,指向<math>(x,y)</math>的射线在坐标平面上与x轴正方向之间的角的角度。当<math>y>0</math>时,射线与x轴正方向的所得的角的角度指的是x轴正方向绕逆时针方向到达射线旋转的角的角度;而当<math>y<0</math>时,射线与x轴正方向所得的角的角度指的是x轴正方向绕顺时针方向达到射线旋转的角的角度。
在几何意义上,<math>\operatorname{atan2}(y,x)</math>等价于<math>\operatorname{atan}(\frac{y}{x})</math>,但<math>\operatorname{atan2}</math>的最大优势是可以正确处理<math>x=0</math>而<math>y\neq 0</math>的情况,而不必进行会引发除零异常的<math>\frac{y}{x}</math>操作。
<math>\operatorname{atan2}</math>函数最初在计算机编程语言中被引入,但是现在它的应用在科学和工程等其他多个领域十分常见。他的出现最早可以追溯到FORTRAN语言[1],并且可以在C语言的数学标准库的math.h文件中找到,此外在Java数学库、.NET的System.Math(可应用于C#、VB.NET等语言)、Python的数学模块以及其他地方都可以找到atan2的身影。许多脚本语言,比如Perl,也包含了C语言风格的atan2函数[2]。
函数定义[编辑]
该函数基于值域为 <math>\left ( -\frac{\pi}{2}, \frac{\pi}{2} \right )</math> 的反正切函数,定义如下:
- <math>\operatorname{atan2}(y, x) = \begin{cases}
\arctan\left(\frac y x\right) & \qquad x > 0 \\ \arctan\left(\frac y x\right) + \pi& \qquad y \ge 0 , x < 0 \\ \arctan\left(\frac y x\right) - \pi& \qquad y < 0 , x < 0 \\ +\frac{\pi}{2} & \qquad y > 0 , x = 0 \\ -\frac{\pi}{2} & \qquad y < 0 , x = 0 \\ \text{undefined} & \qquad y = 0, x = 0 \end{cases}</math>
说明:
- 该函数的值域为<math>\left( -\pi,\pi\right]</math>,可以通过对负数结果加<math>2\pi</math>的方法,将函数的结果映射到<math>\left [ 0,2\pi\right )</math>范围内。
其他软件中的变形[编辑]
不同计算机语言中该函数的实现各有差异。
vb6:
atan2(x,y)=
(x<>0+y<>0)*
(x<=0)*2*Atn(sgn(y)^sgn(y))/2^(x<>0)-
(x<>0)*Atn(y*x^(x<>0))
adodb.connect.execute:
SELECT (x<>0+y<>0)*(x<=0)*2*Atn(sgn(y)^sgn(y))/2^(x<>0)-(x<>0)*Atn(y*x^(x<>0)) AS AT_ FROM (SELECT Col1 AS x,Col2 AS y) T_
(x<>0+y<>0)可省略
有关图片[编辑]
旁边的图片显示内容是:在一个单位圆内<math>\operatorname{atan2}</math>函数在各点的取值。圆内标注代表各点的取值的幅度表示。
图片中,从最左端开始,角度的大小随着逆时针方向逐渐从<math>-\pi</math>增大到<math>+\pi</math>,并且角度大小在点位于最右端时,取值为0。
另外要注意的是,函数<math>\operatorname{atan2}(y,x)</math>中参数的顺序是倒置的,<math>\operatorname{atan2}(y,x)</math>计算的值相当于点<math>(x,y)</math>的角度值。
下方的图片显示的是单位圆上各点在atan2函数上的值,从原点射向<math>(0,1)</math>点的射线,开始绕逆时针方向可以与x轴正方向得到对应各点的复平面的复角,其中几个特殊点取值:
- <math>(0,1)</math>对应的复平面夹角为<math>\frac{\pi}{2}</math>,
- <math>(-1,0)</math>对应复平面的夹角为<math>\pi</math>,
- <math>(0,-1)</math>对应复平面的夹角为<math>\frac{3\pi}{2}</math>,
- 回到<math>(1,0)</math>复平面的夹角为<math>0=(2n\pi \mod 2\pi)</math>。
这些你可以直观地从图中看出。[3]
下面的插图分别显示的是<math>\operatorname{atan2}(y,x)</math>和<math>\operatorname{atan}(\frac{y}{x})</math>在坐标平面的三维景象。
注意在<math>\operatorname{atan2}(y,x)</math>函数中,从原点辐射出的射线上有常数值,而在<math>\operatorname{atan}(\frac{y}{x})</math>的函数中,经过原点的直线有常数值。
参考文献[编辑]
- ^ Organick, Elliott I. A FORTRAN IV Primer. Addison-Wesley. 1966: 42.
Some processors also offer the library function called ATAN2, a function of two arguments (opposite and adjacent).
- ^ The Linux Programmer's Manual [1] (页面存档备份,存于互联网档案馆) says:
- "The atan2() function calculates the arc tangent of the two variables y and x. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result."
- ^ Computation of the external argument by Wolf Jung. [2011-04-10]. (原始内容存档于2011-07-14).