Math.Atan2()是內置的Math類方法,該方法返回其切線為兩個指定數字的商的角度。本質上,它返回一個角度θ(以弧度為單位),該角度的值介於-π和π之間。這是正x軸和點(x,y)之間的逆時針角度。
用法:
public static double Atan2(double value1, double value2)
參數:
-
value1:類型System.Double的點的y坐標。
value2:類型點的x坐標係統雙。
返回類型:返回System.Double類型的角度Θ。
注意:角度θ(以弧度為單位),使-π≤θ≤π,且tan(θ)= value1 /value2,其中(value1,value2)是笛卡爾平麵中的點。返回值有兩個條件:
- 當點位於笛卡爾平麵中時
- 當點位於象限的邊界上時
以下是演示當點位於笛卡爾平麵中時的Math.Atan2()方法的程序:
- 示例1:如果point(value1,value2)位於第一象限,即0
// C# program to demonstrate the // Math.Atan2() Method when point // lies in first quadrant using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(10, 10) * (180 / Math.PI)); } }
輸出:45
- 示例2:如果point(value1,value2)位於第二象限即π/2
// C# program to demonstrate the // Math.Atan2() Method when point // lies in second quadrant using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(10, -10) * (180 / Math.PI)); } }
輸出:135
- 示例3:如果point(value1,value2)位於第三象限,即-π
// C# program to demonstrate the // Math.Atan2() Method when point // lies in third quadrant using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(-10, -10) * (180 / Math.PI)); } }
輸出:-135
- 示例4:如果point(value1,value2)位於第四個象限,即-π/2
// C# program to demonstrate the // Math.Atan2() Method when point // lies in fourth quadrant using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(-10, 10) * (180 / Math.PI)); } }
輸出:-45
以下是演示當點位於象限邊界上時的Math.Atan2()方法的程序:
- 示例1:如果value1為0且value2不為負,即θ= 0
// C# program to demonstrate the // Math.Atan2() Method when value1 // is 0 and value2 is not negative using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(0, 10) * (180 / Math.PI)); } }
輸出:
0
- 示例2:如果value1為0且value2為負,即θ=π
// C# program to demonstrate the // Math.Atan2() Method when value1 // is 0 and value2 is negative using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(0, -10) * (180 / Math.PI)); } }
輸出:180
- 示例3:如果value1為正且value2為0,即θ=π/2
// C# program to demonstrate the // Math.Atan2() Method value1 is // positive and value2 is 0 using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(10, 0) * (180 / Math.PI)); } }
輸出:90
- 示例4:如果value1為負且value2為0,即θ=-π/2
// C# program to demonstrate the // Math.Atan2() Method value1 is // negative and value2 is 0 using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(-10, 0) * (180 / Math.PI)); } }
輸出:-90
- 示例5:如果value1為0且value2為0,即θ= 0
// C# program to demonstrate the // Math.Atan2() Method value1 is // 0 and value2 is 0 using System; class Geeks { // Main method public static void Main() { // using Math.Atan2() Method & // converting result into degree Console.Write(Math.Atan2(0, 0) * (180 / Math.PI)); } }
輸出:0
要記住的重要點:如果value1或value2為NaN,或者value1和value1為PositiveInfinity或NegativeInfinity,則該方法返回NaN。
例:
// C# program to demonstrate the Math.Atan2()
// method when arguments are of type either
// NaN, PositiveInfinity or NegativeInfinity
using System;
class Geeks {
// Main method
public static void Main()
{
double val1 = 0;
double val2 = Double.NaN;
Console.WriteLine(Math.Atan2(val1, val2));
double val3 = Double.NaN;
double val4 = Double.NaN;
Console.WriteLine(Math.Atan2(val3, val4));
double val5 = Double.NaN;
double val6 = Double.PositiveInfinity;
Console.WriteLine(Math.Atan2(val5, val6));
double val7 = Double.PositiveInfinity;
double val8 = Double.NegativeInfinity;
Console.WriteLine(Math.Atan2(val7, val8));
}
}
輸出:
NaN NaN NaN NaN
參考:https://msdn.microsoft.com/en-us/library/system.math.atan2
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自Mithun Kumar大神的英文原創作品 C# | Math.Atan2() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。