Math.Atan2() is an inbuilt Math class method which returns the angle whose tangent is the quotient of two specified numbers. Basically, it returns an angle θ (measured in radian) whose value lies between -π and π. This is a counterclockwise angle lies between the positive x-axis, and the point (x, y).
Syntax:
public static double Atan2(double value1, double value2)
Parameters:
value1: y coordinate of the point of type System.Double.
value2: x coordinate of the point of type System.Double.
Return Type: Returns the angle Θ of type System.Double.
Note: An angle, θ(measured in radians), such that -π ≤ θ ≤ π, and tan(θ) = value1 / value2, where (value1, value2) are the points in the cartesian plane. There are two conditions for the return values:
- When the points lies in the Cartesian plane
- When the points lies on the boundaries of the quadrants
Below are the programs to demonstrate the Math.Atan2() Method when the points lies in the Cartesian plane:
- Program 1: If point(value1, value2) lies in the first quadrant i.e., 0 < θ < π / 2
// C# program to demonstrate the// Math.Atan2() Method when point// lies in first quadrantusingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(10, 10) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:45
- Program 2: If point(value1, value2) lies in the second quadrant i.e., π / 2 < θ ≤ π
// C# program to demonstrate the// Math.Atan2() Method when point// lies in second quadrantusingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(10, -10) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:135
- Program 3: If point(value1, value2) lies in the third quadrant i.e., -π < θ < -π / 2
// C# program to demonstrate the// Math.Atan2() Method when point// lies in third quadrantusingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(-10, -10) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:-135
- Program 4: If point(value1, value2) lies in the fourth quadrant i.e., -π / 2 < θ < 0
// C# program to demonstrate the// Math.Atan2() Method when point// lies in fourth quadrantusingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(-10, 10) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:-45
Below are the programs to demonstrate the Math.Atan2() Method when the points lies on the boundaries of the quadrants:
- Program 1: If value1 is 0 and value2 is not negative i.e. θ = 0
// C# program to demonstrate the// Math.Atan2() Method when value1// is 0 and value2 is not negativeusingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(0, 10) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:
0
- Program 2: If value1 is 0 and value2 is negative i.e. θ = π
// C# program to demonstrate the// Math.Atan2() Method when value1// is 0 and value2 is negativeusingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(0, -10) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:180
- Program 3: If value1 is positive and value2 is 0 i.e. θ = π / 2
// C# program to demonstrate the// Math.Atan2() Method value1 is// positive and value2 is 0usingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(10, 0) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:90
- Program 4: If value1 is negative and value2 is 0 i.e. θ = -π / 2
// C# program to demonstrate the// Math.Atan2() Method value1 is// negative and value2 is 0usingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(-10, 0) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:-90
- Program 5: If value1 is 0 and value2 is 0 i.e. θ = 0
// C# program to demonstrate the// Math.Atan2() Method value1 is// 0 and value2 is 0usingSystem;classGeeks {// Main methodpublicstaticvoidMain(){// using Math.Atan2() Method &// converting result into degreeConsole.Write(Math.Atan2(0, 0) * (180 / Math.PI));}}chevron_rightfilter_noneOutput:0
Important Point to Remember: If value1 or value2 is NaN, or if value1 and value1 are either PositiveInfinity or NegativeInfinity, the method returns NaN.
Example:
// 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
Reference: https://msdn.microsoft.com/en-us/library/system.math.atan2
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Uri.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | Substring() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | EndsWith() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

