📜  C#| Math.Atan2()方法

📅  最后修改于: 2021-05-29 17:57:35             🧑  作者: Mango

Math.Atan2()是一个内置的Math类方法,该方法返回其切线为两个指定数字的商的角度。基本上,它返回一个角度θ(以弧度为单位),该角度的值介于-π和π之间。这是正x轴和点(x,y)之间的逆时针角度。

句法:

public static double Atan2(double value1, double value2)

参数:

返回类型:返回System.Double类型的角度Θ。

注意:角度θ(以弧度为单位),使得-π≤θ≤π,且tan(θ)= value1 / value2,其中(value1,value2)是笛卡尔平面中的点。返回值有两个条件:

  • 当点位于笛卡尔平面中时
  • 当这些点位于象限的边界上时

以下是演示当点位于笛卡尔平面中时的Math.Atan2()方法的程序:

  • 程序1:如果point(value1,value2)位于第一象限,即0 <θ<π/ 2
    // 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)位于第三象限,即-π<θ<-π/ 2
    // 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 <θ<0
    // 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是PositiveInfinityNegativeInfinity ,则该方法将返回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