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.
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
Parameter
Description
y
Required. The y coordinate (vertical position, or "rise") of the point.
x
Required. The x coordinate (horizontal position, or "run") of the point.
Return Value
Return Value
Description
double
The 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));
}
}
Output
45
135
-135
-45
How It Works
Argument order.Deg(y, x) passes y first, exactly as Math.Atan2 expects.
Top-right (1, 1). Up and right is 45 degrees, 45.
Top-left (1, -1). Moving left puts the point past the vertical, so the angle is 135 degrees.
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.
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, 3) to (5, 7). The target is 3 across and 4 up. The angle is about 53.13 degrees.
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.
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));
}
}
Output
0
1.5708
-45
135
How It Works
The origin. The point (0, 0) has no direction. Atan2(0, 0) simply returns 0 and does not throw an exception.
Straight up. For x = 0, a division would fail, but Atan2(1, 0) returns pi / 2, about 1.5708 radians (90 degrees).
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.
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.