当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Math.Atan2()用法及代码示例


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



相关用法


注:本文由纯净天空筛选整理自Mithun Kumar大神的英文原创作品 C# | Math.Atan2() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。