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)可省略

有關圖片[編輯]

File:Atan2 60.svg
單位圓內的atan2取值

旁邊的圖片顯示內容是:在一個單位圓內<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]

File:Atan2-discontinuity.svg

下面的插圖分別顯示的是<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>的函數中,經過原點的直線有常數值。

File:Atan2Diagram.png File:AtanDiagram.png

參考文獻[編輯]

  1. ^ 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). 
  2. ^ 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."
  3. ^ Computation of the external argument by Wolf Jung. [2011-04-10]. (原始內容存檔於2011-07-14). 

參見[編輯]

外部連結[編輯]