C# Math Atan2() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Atan2(y, x) returns the angle (in radians) of the point at position (x, y), measured from the positive x-axis (the 3 o'clock direction). The answer is between -Math.PI and Math.PI (-180 to 180 degrees).

It is the "smart" arctangent. A plain Atan(y / x) forgets the signs of x and y, so it cannot tell the points (1, 1) and (-1, -1) apart. Atan2 uses both signs and picks the correct quarter of the circle. It also copes with x = 0, where dividing would fail. Use it for the direction from one point to another, such as aiming a turret at a target or pointing a compass arrow. Remember the order: y first, then x.

This is a static member of the static class System.Math. You call it with the class name, such as Math.Atan2(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Atan2 is in every .NET version. Its two arguments are y first, then x, and it returns radians.

Syntax

Pass y first, then x. The answer is an angle in radians.

C#
double a = Math.Atan2(1, 1);       // 0.7853981633974483 (45 degrees)
double b = Math.Atan2(1, -1);      // 2.356194490192345 (135 degrees)
double c = Math.Atan2(-1, -1);     // -2.356194490192345 (-135 degrees)
double deg = Math.Atan2(1, 1) * 180 / Math.PI;   // 45
CallWhat it does
Math.Atan2(1, 1)Top-right point: about 0.7854 (45 degrees).
Math.Atan2(1, -1)Top-left point: about 2.3562 (135 degrees).
Math.Atan2(-1, -1)Bottom-left point: about -2.3562 (-135 degrees).
Math.Atan2(0, 0)The origin has no direction. The method returns 0 and does not throw.

The angle is counter-clockwise from the positive x-axis, in radians, from -Math.PI to Math.PI. To get a compass-like value from 0 to 360 degrees, convert to degrees and add 360 when the result is negative. The first argument is y, which surprises many people who expect x first.

Library declarations
C#
public static double Atan2(double y, double x);

Parameters

ParameterDescription
yRequired. The y coordinate (vertical position, or "rise") of the point.
xRequired. The x coordinate (horizontal position, or "run") of the point.

Return Value

Return ValueDescription
doubleThe angle in radians from -Math.PI to Math.PI. NaN if either argument is NaN.

C# Examples

Example 1 — The Angle in Each Quarter of the Circle

Print the angle in degrees for a point in each of the four quarters, rounded to 2 decimals.

C#
using System;
using System.Globalization;

class Program {
    static string Deg(double y, double x) {
        double degrees = Math.Atan2(y, x) * 180 / Math.PI;
        return Math.Round(degrees, 2).ToString(CultureInfo.InvariantCulture);
    }

    static void Main() {
        Console.WriteLine(Deg(1, 1));
        Console.WriteLine(Deg(1, -1));
        Console.WriteLine(Deg(-1, -1));
        Console.WriteLine(Deg(-1, 1));
    }
}

How It Works

  1. Argument order. Deg(y, x) passes y first, exactly as Math.Atan2 expects.
  2. Top-right (1, 1). Up and right is 45 degrees, 45.
  3. Top-left (1, -1). Moving left puts the point past the vertical, so the angle is 135 degrees.
  4. The bottom half. Points below the x-axis get negative angles: -135 for bottom-left and -45 for bottom-right.

Example 2 — Direction from One Point to Another

Find the direction from a start point to a target as a value from 0 to 360 degrees.

C#
using System;
using System.Globalization;

class Program {
    static string Bearing(double fromX, double fromY, double toX, double toY) {
        double angle = Math.Atan2(toY - fromY, toX - fromX) * 180 / Math.PI;
        if (angle < 0) {
            angle += 360;
        }
        return Math.Round(angle, 2).ToString(CultureInfo.InvariantCulture);
    }

    static void Main() {
        Console.WriteLine(Bearing(2, 3, 5, 7));
        Console.WriteLine(Bearing(2, 3, 0, 3));
        Console.WriteLine(Bearing(0, 0, -1, -1));
    }
}

How It Works

  1. The differences. The direction depends only on how far the target is across and up from the start, so we subtract the start from the target before calling Atan2.
  2. (2, 3) to (5, 7). The target is 3 across and 4 up. The angle is about 53.13 degrees.
  3. Straight left. From (2, 3) to (0, 3) the target is 2 to the left and level. Atan2(0, -2) is pi, so the bearing is 180. A plain Atan could not give this.
  4. Wrap negatives. Down-left from the origin gives -135, and adding 360 makes it 225. This turns the -180 to 180 range into 0 to 360.

Example 3 — Edge Cases and the Atan Mistake

See that Atan2 handles the origin and a vertical line, and where a plain Atan goes wrong.

C#
using System;
using System.Globalization;

class Program {
    static string R(double value) {
        return Math.Round(value, 4).ToString(CultureInfo.InvariantCulture);
    }

    static void Main() {
        Console.WriteLine(Math.Atan2(0, 0));
        Console.WriteLine(R(Math.Atan2(1, 0)));

        double wrong = Math.Atan(1 / -1.0) * 180 / Math.PI;
        double right = Math.Atan2(1, -1) * 180 / Math.PI;
        Console.WriteLine(Math.Round(wrong, 2).ToString(CultureInfo.InvariantCulture));
        Console.WriteLine(Math.Round(right, 2).ToString(CultureInfo.InvariantCulture));
    }
}

How It Works

  1. The origin. The point (0, 0) has no direction. Atan2(0, 0) simply returns 0 and does not throw an exception.
  2. Straight up. For x = 0, a division would fail, but Atan2(1, 0) returns pi / 2, about 1.5708 radians (90 degrees).
  3. The Atan mistake. For the point x = -1, y = 1, dividing gives -1, and Atan(-1) is -45 degrees. This points to the bottom-right, which is the wrong side.
  4. The Atan2 answer. Atan2(1, -1) looks at both signs and returns 135 degrees, the true direction of the top-left point.

Frequently Asked Questions

It returns the angle in radians of the point (x, y), measured from the positive x-axis, from -Math.PI to Math.PI. Math.Atan2(1, 1) is about 0.7854 (45 degrees).
y comes first, then x: Math.Atan2(y, x). Swapping them is a very common mistake and gives an angle measured from the wrong axis.
Atan takes one value (y / x) and loses the signs, so it only returns -90 to 90 degrees. Atan2 takes y and x separately, finds the right quarter of the circle, and returns -180 to 180 degrees. It also works when x is 0.
Convert the Atan2 result to degrees with * 180 / Math.PI. If the result is negative, add 360.
No. Math is a static class, so you call Math.Atan2(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Atanh()

Return the inverse hyperbolic tangent for inputs between -1 and 1.

Atanh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful