語言

Math.Atan2(Double, Double) 方法

定義

返回切線為兩個指定數的商角。

public:
 static double Atan2(double y, double x);
public static double Atan2(double y, double x);
static member Atan2 : double * double -> double
Public Shared Function Atan2 (y As Double, x As Double) As Double

參數

y
Double

一個點的 y 座標。

x
Double

一個點的 x 座標。

傳回

一個角度θ,以弧度為單位,使得tan(θ) = y / x,其中(x, y) 是笛卡兒平面上的一點。 請注意以下幾點:

  • 對於第一象限x的 (y, <) 0 θ < π/2。

  • 對於第二象限的(x, y),π/2 < θ ≤ π。

  • 對於第3象限的(x, y),-π ≤ θ < -π/2。

  • 對於第四象限的(x, y),-π/2 < θ < 0。

對於象限邊界上的點,回傳值如下:

  • 若 y 為 0 且 x 非負,則 θ = 0。

  • 若 y 為 0,x 為負,則 θ = π。

  • 若 y 為正且 x 為 0,則 θ = π/2。

  • 若 y 為負且 x 為 0,則 θ = -π/2。

  • 若 y 為 0,x 為 0,則 θ = 0。

若 或 為 ,或若 x 且 y 為 NaNx 或 ,則該方法返回 y。PositiveInfinityNegativeInfinityNaN

範例

以下範例示範如何計算角度與向量的弧切。 所得數值會顯示在主控台中。

// This example demonstrates Math.Atan()
//                           Math.Atan2()
//                           Math.Tan()
using System;

class Sample
{
    public static void Main()
    {
    double x = 1.0;
    double y = 2.0;
    double angle;
    double radians;
    double result;

// Calculate the tangent of 30 degrees.
    angle = 30;
    radians = angle * (Math.PI/180);
    result = Math.Tan(radians);
    Console.WriteLine("The tangent of 30 degrees is {0}.", result);

// Calculate the arctangent of the previous tangent.
    radians = Math.Atan(result);
    angle = radians * (180/Math.PI);
    Console.WriteLine("The previous tangent is equivalent to {0} degrees.", angle);

// Calculate the arctangent of an angle.
    String line1 = "{0}The arctangent of the angle formed by the x-axis and ";
    String line2 = "a vector to point ({0},{1}) is {2}, ";
    String line3 = "which is equivalent to {0} degrees.";

    radians = Math.Atan2(y, x);
    angle = radians * (180/Math.PI);

    Console.WriteLine(line1, Environment.NewLine);
    Console.WriteLine(line2, x, y, radians);
    Console.WriteLine(line3, angle);
    }
}
/*
This example produces the following results:

The tangent of 30 degrees is 0.577350269189626.
The previous tangent is equivalent to 30 degrees.

The arctangent of the angle formed by the x-axis and
a vector to point (1,2) is 1.10714871779409,
which is equivalent to 63.434948822922 degrees.
*/
// This example demonstrates Math.Atan()
//                           Math.Atan2()
//                           Math.Tan()
// Functions 'atan', 'atan2', and 'tan' may be used instead. 
open System

[<EntryPoint>]
let main _ =
    let x = 1.
    let y = 2.

    // Calculate the tangent of 30 degrees.
    let angle = 30.
    let radians = angle * (Math.PI / 180.)
    let result = Math.Tan radians
    printfn $"The tangent of 30 degrees is {result}."

    // Calculate the arctangent of the previous tangent.
    let radians = Math.Atan result
    let angle = radians * (180. / Math.PI)
    printfn $"The previous tangent is equivalent to {angle} degrees."

    // Calculate the arctangent of an angle.

    let radians = Math.Atan2(y, x)
    let angle = radians * (180. / Math.PI)

    printfn 
        $"""The arctangent of the angle formed by the x-axis and 
a vector to point ({x},{y}) is {radians},
which is equivalent to {angle} degrees."""
    0

//This example produces the following results:
//     The tangent of 30 degrees is 0.577350269189626.
//     The previous tangent is equivalent to 30 degrees.
//     
//     The arctangent of the angle formed by the x-axis and
//     a vector to point (1,2) is 1.10714871779409,
//     which is equivalent to 63.434948822922 degrees.
' This example demonstrates Math.Atan()
'                           Math.Atan2()
'                           Math.Tan()
Class Sample
   Public Shared Sub Main()
      Dim x As Double = 1.0
      Dim y As Double = 2.0
      Dim angle As Double
      Dim radians As Double
      Dim result As Double
      
      ' Calculate the tangent of 30 degrees.
      angle = 30
      radians = angle *(Math.PI / 180)
      result = Math.Tan(radians)
      Console.WriteLine("The tangent of 30 degrees is {0}.", result)
      
      ' Calculate the arctangent of the previous tangent.
      radians = Math.Atan(result)
      angle = radians *(180 / Math.PI)
      Console.WriteLine("The previous tangent is equivalent to {0} degrees.", angle)
      
      ' Calculate the arctangent of an angle.
      Dim line1 As [String] = "{0}The arctangent of the angle formed by the x-axis and "
      Dim line2 As [String] = "a vector to point ({0},{1}) is {2}, "
      Dim line3 As [String] = "which is equivalent to {0} degrees."
      
      radians = Math.Atan2(y, x)
      angle = radians *(180 / Math.PI)
      
      Console.WriteLine(line1, Environment.NewLine)
      Console.WriteLine(line2, x, y, radians)
      Console.WriteLine(line3, angle)
   End Sub
End Class
'
'This example produces the following results:
'
'The tangent of 30 degrees is 0.577350269189626.
'The previous tangent is equivalent to 30 degrees.
'
'The arctangent of the angle formed by the x-axis and
'a vector to point (1,2) is 1.10714871779409,
'which is equivalent to 63.434948822922 degrees.
'

備註

返回值是由 x 軸和一個從原點 (0,0) 開始,終止於點 (x,y) 所形成的笛卡兒平面角度。

此方法會呼叫底層的 C 執行時,且不同作業系統或架構間的精確結果或有效輸入範圍可能有所不同。

適用於