Atan2 在三角函数 中,两个参数的函数 atan2 {\displaystyle \operatorname {atan2} } 是正切函数 tan {\displaystyle \tan } 的一个变种。对于任意不同时等于0的实参数 x {\displaystyle x} 和 y {\displaystyle y} , atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} 所表达的意思是坐标原点为起点,指向 ( x , y ) {\displaystyle (x,y)} 的射线在坐标平面上与x轴正方向之间的角的角度。当 y > 0 {\displaystyle y>0} 时,射线与x轴正方向的所得的角的角度指的是x轴正方向绕逆时针方向到达射线旋转的角的角度;而当 y < 0 {\displaystyle y<0} 时,射线与x轴正方向所得的角的角度指的是x轴正方向绕顺时针方向达到射线旋转的角的角度。
在几何意义上, atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} 等价于 atan ( y x ) {\displaystyle \operatorname {atan} ({\frac {y}{x}})} ,但 atan2 {\displaystyle \operatorname {atan2} } 的最大优势是可以正确处理 x = 0 {\displaystyle x=0} 而 y ≠ 0 {\displaystyle y\neq 0} 的情况,而不必进行会引发除零异常 的 y x {\displaystyle {\frac {y}{x}}} 操作。
atan2 {\displaystyle \operatorname {atan2} } 函数最初在计算机编程语言中被引入,但是现在它的应用在科学和工程等其他多个领域十分常见。他的出现最早可以追溯到FORTRAN语言[ 1] ,并且可以在C语言 的数学标准库的math.h文件中找到,此外在Java数学库、.NET的System.Math(可应用于C#、VB.NET等语言)、Python的数学模块以及其他地方都可以找到atan2的身影。许多脚本语言,比如Perl,也包含了C语言风格的atan2函数[ 2] 。
函数定义 该函数基于值域为 ( − π 2 , π 2 ) {\displaystyle \left(-{\frac {\pi }{2}},{\frac {\pi }{2}}\right)} 的反正切 函数,定义如下:
atan2 ( y , x ) = { arctan ( y x ) x > 0 arctan ( y x ) + π y ≥ 0 , x < 0 arctan ( y x ) − π y < 0 , x < 0 + π 2 y > 0 , x = 0 − π 2 y < 0 , x = 0 undefined y = 0 , x = 0 {\displaystyle \operatorname {atan2} (y,x)={\begin{cases}\arctan \left({\frac {y}{x}}\right)&\qquad x>0\\\arctan \left({\frac {y}{x}}\right)+\pi &\qquad y\geq 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}}} 说明:
该函数的值域为 ( − π , π ] {\displaystyle \left(-\pi ,\pi \right]} ,可以通过对负数结果加 2 π {\displaystyle 2\pi } 的方法,将函数的结果映射到 [ 0 , 2 π ) {\displaystyle \left[0,2\pi \right)} 范围内。
其他软件中的变形 不同计算机语言中该函数的实现各有差异。
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)可省略
有关图片 单位圆内的atan2取值 旁边的图片显示内容是:在一个单位圆内 atan2 {\displaystyle \operatorname {atan2} } 函数在各点的取值。圆内标注代表各点的取值的幅度表示。
图片中,从最左端开始,角度的大小随着逆时针方向逐渐从 − π {\displaystyle -\pi } 增大到 + π {\displaystyle +\pi } ,并且角度大小在点位于最右端时,取值为0。
另外要注意的是,函数 atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} 中参数的顺序是倒置的, atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} 计算的值相当于点 ( x , y ) {\displaystyle (x,y)} 的角度值。
下方的图片显示的是单位圆上各点在atan2函数上的值,从原点射向 ( 0 , 1 ) {\displaystyle (0,1)} 点的射线,开始绕逆时针方向可以与x轴正方向得到对应各点的复平面的复角,其中几个特殊点取值:
( 0 , 1 ) {\displaystyle (0,1)} 对应的复平面夹角为 π 2 {\displaystyle {\frac {\pi }{2}}} , ( − 1 , 0 ) {\displaystyle (-1,0)} 对应复平面的夹角为 π {\displaystyle \pi } , ( 0 , − 1 ) {\displaystyle (0,-1)} 对应复平面的夹角为 3 π 2 {\displaystyle {\frac {3\pi }{2}}} , 回到 ( 1 , 0 ) {\displaystyle (1,0)} 复平面的夹角为 0 = ( 2 n π mod 2 π ) {\displaystyle 0=(2n\pi \mod 2\pi )} 。 这些你可以直观地从图中看出。[ 3]
下面的插图分别显示的是 atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} 和 atan ( y x ) {\displaystyle \operatorname {atan} ({\frac {y}{x}})} 在坐标平面的三维景象。
注意在 atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)} 函数中,从原点辐射出的射线上有常数值,而在 atan ( y x ) {\displaystyle \operatorname {atan} ({\frac {y}{x}})} 的函数中,经过原点的直线有常数值。
参考文献 ^ 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).
参见
外部链接 Java 1.6 SE JavaDoc(页面存档备份,存于互联网档案馆 ) C++ Programmer's Reference(页面存档备份,存于互联网档案馆 )