Java Math atan2() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

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));
CallWhat 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

ParameterDescription
yThe y coordinate (the vertical, or "rise" part). It comes first.
xThe x coordinate (the horizontal, or "run" part). It comes second.

Return Value

Return ValueDescription
doubleThe 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.

Compare with Similar Methods

Ways to get an angle or a distance in Java:

MethodHow it differs
atan(ratio)One ratio only: no quadrant, range -PI/2 to PI/2.
acos(x)Angle from a cosine; 0 to PI and needs clamping.
hypot(x, y)The length of the same vector; pair it with atan2 for polar form.
toDegrees(rad)Turns the angle into degrees.

Java Examples

Example 1 — The Angle of a Point in Each Quadrant

Four points around the origin, printed as radians and as degrees. Remember: the call is atan2(y, x).

Java
import java.util.Locale;

public class MathAtan2Quadrants {
    public static void main(String[] args) {
        int[][] points = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
        for (int[] p : points) {
            double rad = Math.atan2(p[1], p[0]);
            System.out.println("(" + p[0] + ", " + p[1] + ") -> " + rad + " rad, "
                    + String.format(Locale.US, "%.1f", Math.toDegrees(rad)) + " deg");
        }
    }
}

How It Works

  1. y goes first. Each point is stored as {x, y}, so the call passes p[1] (y) before p[0] (x).
  2. 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.
  3. 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.

Java
import java.util.Locale;

public class MathAtan2Special {
    public static void main(String[] args) {
        System.out.println("atan2(0.0, -1.0)  = " + Math.atan2(0.0, -1.0));
        System.out.println("atan2(-0.0, -1.0) = " + Math.atan2(-0.0, -1.0));
        System.out.println("atan2(0, 0)       = " + Math.atan2(0, 0));
        System.out.println("atan2(1, 0)       = " + Math.atan2(1, 0));
        System.out.println("atan2(-1, 0)      = " + Math.atan2(-1, 0));
        System.out.println("atan2(NaN, 1)     = " + Math.atan2(Double.NaN, 1));
        System.out.println("atan2(3, 4) degrees: " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.atan2(3, 4))));
        System.out.println("atan2(4, 3) degrees: " + String.format(Locale.US, "%.2f", Math.toDegrees(Math.atan2(4, 3))));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: cbrt()

Cube root of a double. Works for negative numbers too.

cbrt() 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