C# Math.Atan2() – Examples
In this tutorial, we will learn about the C# Math.Atan2() method, and learn how to use this method to find angle of the tangent given a point in XY coordinate system, with the help of examples.
Atan2(Double, Double)
Math.Atan2(y, x) returns the angle whose tangent is the quotient of two specified numbers y
and x
.
Syntax
The syntax of Atan2(x, y) method is
</>
Copy
Math.Atan2(Double y, Double x)
where
Parameter | Description |
---|---|
y | Y-coordinate of the point (x, y) |
x | X-coordinate of the point (x, y) |
Return Value
The method returns Double value.
Example 1 – Atan2(y, x)
In this example, we will find the angle made by the Points (x, y) with the origin using Atan2() method.
C# Program
</>
Copy
using System;
class Example {
static void Main(string[] args) {
Double x, y, result;
x = 2;
y = 2;
result = Math.Atan2(y, x);
Console.WriteLine($"Atan2({y},{x}) = {result} radians");
x = -2;
y = 2;
result = Math.Atan2(y, x);
Console.WriteLine($"Atan2({y},{x}) = {result} radians");
x = -2;
y = -2;
result = Math.Atan2(y, x);
Console.WriteLine($"Atan2({y},{x}) = {result} radians");
x = 2;
y = -2;
result = Math.Atan2(y, x);
Console.WriteLine($"Atan2({y},{x}) = {result} radians");
}
}
Output
Atan2(2,2) = 0.785398163397448 radians
Atan2(2,-2) = 2.35619449019234 radians
Atan2(-2,-2) = -2.35619449019234 radians
Atan2(-2,2) = -0.785398163397448 radians
Conclusion
In this C# Tutorial, we have learnt the syntax of C# Math.Atan2() method, and also learnt how to use this method with the help of C# example programs.