C# Math - Atan2() Method
The C# Atan2() method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). The returned value will be in the range -𝜋 through 𝜋. In special cases it returns the following:
- If either argument is NaN, then the result is NaN.
- If both arguments are positive infinity, then the result is closest to 𝜋/4.
- If the first argument is positive or negative finite and the second argument is positive infinity, then the result is zero.
- If the first argument is positive and finite and the second argument is negative infinity, then the result is closest to 𝜋.
- If the first argument is negative and finite and the second argument is negative infinity, then the result is closest to -𝜋.
- If the first argument is positive and the second argument is zero, or the first argument is positive infinity and the second argument is finite, then the result is closest to 𝜋/2.
- If the first argument is negative and the second argument is zero, or the first argument is negative infinity and the second argument is finite, then the result is closest to -𝜋/2.
- If the first argument is positive infinity and the second argument is negative infinity, then the result is closest to 3*𝜋/4.
- If the first argument is negative infinity and the second argument is positive infinity, then the result is closest to -𝜋/4.
- If both arguments are negative infinity, then the result is closest to -3*𝜋/4.
Syntax
public static double Atan2 (double y, double x);
Parameters
y |
Specify the ordinate coordinate. |
x |
Specify the abscissa coordinate. |
Return Value
Returns theta of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.
Example:
In the example below, Atan2() method is used to calculate the theta of a point.
using System; class MyProgram { static void Main(string[] args) { Console.WriteLine("Math.Atan2(10, 20) = " + Math.Atan2(10, 20)); Console.WriteLine("Math.Atan2(-10, 20) = " + Math.Atan2(-10, 20)); Console.WriteLine("Math.Atan2(10, -20) = " + Math.Atan2(10, -20)); Console.WriteLine("Math.Atan2(-10, -20) = " + Math.Atan2(-10, -20)); Console.WriteLine("Math.Atan2(10, Double.NaN) = " + Math.Atan2(10, Double.NaN)); Console.WriteLine("Math.Atan2(10, Double.NegativeInfinity) = " + Math.Atan2(10, Double.NegativeInfinity)); Console.WriteLine("Math.Atan2(10, Double.PositiveInfinity) = " + Math.Atan2(10, Double.PositiveInfinity)); } }
The output of the above code will be:
Math.Atan2(10, 20) = 0.463647609000806 Math.Atan2(-10, 20) = -0.463647609000806 Math.Atan2(10, -20) = 2.67794504458899 Math.Atan2(-10, -20) = -2.67794504458899 Math.Atan2(10, Double.NaN) = NaN Math.Atan2(10, Double.NegativeInfinity) = 3.14159265358979 Math.Atan2(10, Double.PositiveInfinity) = 0
❮ C# Math Methods