In Java, Math.atan2(y, x) returns the angle of the point (x, y), measured in radians from the positive x axis, counter-clockwise. The result is between -PI and PI, and Java picks the correct quadrant by looking at the signs of both arguments.
That makes it the standard tool for the direction of a vector, for aiming from one point to another in a game, for converting (x, y) to polar coordinates, and for compass headings. It does not suffer from the division by zero or quadrant problems of Math.atan(y / x).
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.atan2(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass y first, then x, both as doubles. The angle comes back in radians.
Java
double a = Math.atan2(1, 1); // 0.7853981633974483 (45 degrees)
double b = Math.atan2(1, -1); // 2.356194490192345 (135 degrees)
double toTarget = Math.atan2(dy, dx); // direction from player to target
double deg = Math.toDegrees(Math.atan2(y, x));
Call
What it does
Math.atan2(1, 1)
Up and right: returns PI/4 (0.7853981633974483).
Math.atan2(1, -1)
Up and left: returns 3*PI/4 (2.356194490192345).
Math.atan2(-1, -1)
Down and left: returns -3*PI/4 (-2.356194490192345).
Math.atan2(0.0, -1.0)
Straight left: returns PI (3.141592653589793).
There is only a double version. The order is atan2(y, x), the opposite of how points are usually written as (x, y). Swapping the two arguments is the most common mistake: it mirrors the angle across the 45 degree line.
Library declarations
Java
// java.lang.Math
public static double atan2(double y, double x)
Parameters
Parameter
Description
y
The y coordinate (the vertical, or "rise" part). It comes first.
x
The x coordinate (the horizontal, or "run" part). It comes second.
Return Value
Return Value
Description
double
The angle in radians, in the range -PI to PI, measured counter-clockwise from the positive x axis. The result is NaN if either argument is NaN.
y goes first. Each point is stored as {x, y}, so the call passes p[1] (y) before p[0] (x).
Upper half is positive. The points above the x axis, (1, 1) and (-1, 1), give 45 and 135 degrees, so a point up and to the left is correctly beyond 90.
Lower half is negative. Points below the axis get negative angles, -135 and -45 degrees; atan(y / x) would have returned 45 for (-1, -1), because the two minus signs cancel in the ratio.
Example 2 — Signed Zeros, Zero Arguments, and the Argument-Order Mistake
Print the edge cases that the documentation spells out, then swap the arguments to see how large the mistake is.
Zero has a sign. Straight left sits right on the branch cut: +0.0 means "just above the axis" and gives PI, while -0.0 means "just below" and gives -PI.
No division by zero. A vertical vector (x = 0) gives plus or minus PI/2, and the origin (0, 0) gives 0.0 instead of an error or NaN.
Swapped arguments.atan2(3, 4) is the angle of the point (4, 3): 36.87 degrees; atan2(4, 3) is the angle of (3, 4): 53.13 degrees. The two always add up to 90.
Example 3 — Compass Heading to a Target and Polar Coordinates
A compass heading counts clockwise from north, so the two arguments are swapped on purpose. Then convert a point to a distance and an angle.
Java
import java.util.Locale;
public class MathAtan2Heading {
public static void main(String[] args) {
double[][] targets = {{0, 5}, {5, 0}, {0, -5}, {-5, 0}, {3, 3}};
String[] names = {"north", "east", "south", "west", "northeast"};
for (int i = 0; i < targets.length; i++) {
double east = targets[i][0];
double north = targets[i][1];
double heading = (Math.toDegrees(Math.atan2(east, north)) + 360) % 360;
System.out.println(names[i] + ": " + String.format(Locale.US, "%.1f", heading));
}
double r = Math.hypot(3, 4);
double theta = Math.toDegrees(Math.atan2(4, 3));
System.out.println(String.format(Locale.US, "Point (3, 4): r = %.2f, angle = %.2f degrees", r, theta));
}
}
Output
north: 0.0
east: 90.0
south: 180.0
west: 270.0
northeast: 45.0
Point (3, 4): r = 5.00, angle = 53.13 degrees
How It Works
Swap on purpose. Mathematicians measure from east, counter-clockwise; compasses measure from north, clockwise. Passing (east, north) instead of (y, x) converts one convention to the other.
Normalise to 0-360. West gives -90 degrees; adding 360 and taking % 360 turns it into 270.0, while east (90.0) is unchanged by the same line.
Polar form.Math.hypot(3, 4) gives the distance 5 and atan2(4, 3) gives the angle; the pair (5.00, 53.13) is the polar form of the point (3, 4).
Frequently Asked Questions
It returns the angle in radians, from -PI to PI, of the point (x, y), measured from the positive x axis. Math.atan2(1, 1) is 0.7853981633974483, which is 45 degrees.
It follows the C library convention atan2(y, x), mirroring the ratio y / x inside atan(y / x). Swapping the arguments gives the complementary angle, a very common bug.
atan(y / x) loses the signs of y and x, so it cannot find the quadrant and fails when x is 0. atan2(y, x) uses both signs and works for every point.
Convert with Math.toDegrees and normalise: (Math.toDegrees(Math.atan2(y, x)) + 360) % 360. The result is then never negative.
It returns 0.0, with no exception. The origin has no real direction, so check for it yourself if a missing direction matters in your program.