What is Math.Atan2() in C#?
C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Atan2() function, which is used to compute the angle whose tangent is the quotient of two specified numbers.
Syntax
public static double Atan2 (double yaxis, double xaxis);
Parameters
xaxis: This parameter is of theDoubletype and represents the x coordinate of the input point.yaxis: This parameter is of theDoubletype and represents the y coordinate of the input point.
Return value
-
Double:This value returns an angle Θ measured in radians, and its type isDouble. Its range is:-
-π <= θ <= π (radians)
where tan(θ) =
yaxis/xaxis-
For (
xaxis,yaxis) in quadrant 1, 0 < θ < π/2. -
For (
xaxis,yaxis) in quadrant 2, π/2 < θ ≤ π. -
For (
xaxis,yaxis) in quadrant 3, -π < θ < -π/2. -
For (
xaxis,yaxis) in quadrant 4, -π/2 < θ < 0.
Boundary points
-
If
yaxis = 0andxaxis > 0,θ = 0. -
If
yaxis = 0andxaxis < 0,θ = π. -
If
yaxis > 0andxaxis = 0,θ = π/2. -
If
yaxis < 0andxaxis = 0,θ = -π/2. -
If
yaxis = 0andxaxis = 0,θ = 0.
-
-
NaN: The function returns this ifxaxisoryaxisisNaN,PositiveInfinity, orNegativeInfinity.
Example
using System;class Educative{static void Main(){double result = Math.Atan2(20,15);System.Console.WriteLine("Atan2(20,15) = "+ result + " radians");double result1 = Math.Atan2(-20,-15);System.Console.WriteLine("Atan2(-20,-15) = "+ result1 + " radians");double result2 = Math.Atan2(-20,Double.NaN);System.Console.WriteLine("Atan2(-20,NaN) = "+ result2);}}