MathF.Atan2() Method in C# with Examples

The MathF.Atan2() method in C# returns the angle whose tangent is the quotient of two specified floating-point numbers. It calculates the arctangent of y/x and returns the angle in radians between the positive x-axis and the point (x, y).

This method is particularly useful for determining the angle of a point in a coordinate system and handles all four quadrants correctly, unlike the regular arctangent function.

Syntax

Following is the syntax −

public static float Atan2(float y, float x);

Parameters

  • y − The y-coordinate of a point (numerator).

  • x − The x-coordinate of a point (denominator).

Return Value

Returns a float value representing the angle in radians, ranging from -? to ?. The sign of the result indicates which quadrant the angle falls into.

Atan2() Quadrant Coverage +x +y Q2 ?/2 to ? Q1 0 to ?/2 Q3 -? to -?/2 Q4 -?/2 to 0 (x,y) ?

Using MathF.Atan2() for Basic Calculations

Example

using System;

public class Demo {
   public static void Main() {
      float y = 0.1f;
      float x = 0.9f;
      float angleInRadians = MathF.Atan2(y, x);
      Console.WriteLine("Point: (" + x + ", " + y + ")");
      Console.WriteLine("Angle in radians: " + angleInRadians);
      Console.WriteLine("Angle in degrees: " + (angleInRadians * 180 / MathF.PI));
   }
}

The output of the above code is −

Point: (0.9, 0.1)
Angle in radians: 0.1106572
Angle in degrees: 6.340192

Using MathF.Atan2() for 45-Degree Angles

Example

using System;

public class Demo {
   public static void Main() {
      float y = 25f;
      float x = 25f;
      float angleInRadians = MathF.Atan2(y, x);
      float angleInDegrees = angleInRadians * (180 / MathF.PI);
      Console.WriteLine("Point: (" + x + ", " + y + ")");
      Console.WriteLine("Angle in radians: " + angleInRadians);
      Console.WriteLine("Angle in degrees: " + angleInDegrees);
   }
}

The output of the above code is −

Point: (25, 25)
Angle in radians: 0.7853982
Angle in degrees: 45

Using MathF.Atan2() for All Quadrants

Example

using System;

public class Demo {
   public static void Main() {
      // Test points in all four quadrants
      float[,] points = { {1, 1}, {-1, 1}, {-1, -1}, {1, -1} };
      string[] quadrants = {"Q1", "Q2", "Q3", "Q4"};
      
      for (int i = 0; i < 4; i++) {
         float x = points[i, 0];
         float y = points[i, 1];
         float angle = MathF.Atan2(y, x) * (180 / MathF.PI);
         Console.WriteLine(quadrants[i] + " - Point(" + x + ", " + y + "): " + angle + "°");
      }
   }
}

The output of the above code is −

Q1 - Point(1, 1): 45°
Q2 - Point(-1, 1): 135°
Q3 - Point(-1, -1): -135°
Q4 - Point(1, -1): -45°

Conclusion

The MathF.Atan2() method in C# calculates the arctangent of the ratio y/x, returning the angle in radians from -? to ?. It correctly handles all four quadrants, making it ideal for coordinate system calculations and angle determinations in 2D graphics and mathematical applications.

Updated on: 2026-03-17T07:04:36+05:30

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements