In C++, std::atan2(y, x) finds the angle of a point(x, y) as seen from the origin. It is like atan(y / x), but it looks at the signs of y and x separately, so it knows which of the four quarters (quadrants) the point is in.
The result is in radians, from -pi to pi. A point to the right gives 0, straight up gives pi / 2, to the left gives pi, and straight down gives -pi / 2. Note the order: y comes first, then x.
std::atan2() is declared in <cmath> and is part of the group Trigonometry. Trigonometric functions use radians, not degrees. To convert degrees, multiply by std::numbers::pi / 180 (C++20).
Syntax
Include <cmath> and call std::atan2. Start with these lines.
C++
double a = std::atan2(1.0, 1.0); // 0.785398 (45 degrees)
double b = std::atan2(1.0, -1.0); // 2.35619 (135 degrees)
float c = std::atan2(1.0f, 0.0f); // 1.5708f (90 degrees)
Call
What it does
std::atan2(1.0, 1.0)
The point (1, 1) lies at 45 degrees. Returns 0.785398.
std::atan2(1.0, -1.0)
The point (-1, 1) is in the top left. Returns 2.35619, or 135 degrees.
std::atan2(1.0, 0.0)
A point straight up. atan(1 / 0) would fail, but this returns 1.5708.
Remember the order of the arguments: y first, then x. This is the opposite of how a point is written, (x, y).
Library declarations
C++
float atan2(float x, float y);
double atan2(double x, double y);
long double atan2(long double x, long double y);
// plus overloads for mixed and integer arguments
Parameters
Parameter
Description
y
Required. The vertical value (the rise) of the point.
x
Required. The horizontal value (the run) of the point.
Return Value
Return Value
Description
Angle
The angle in radians from -pi to pi, measured from the positive x axis.
0
atan2(0, 0) returns 0, and atan2(+0, +x) returns 0.
pi / -pi
atan2(+0, -x) returns pi, and atan2(-0, -x) returns -pi.
C++ Examples
Example 1 — Angles in Four Quadrants
Print the angle in degrees for one point in each quadrant.
To (5.00, 7.00): 53.13 degrees
To (2.00, 8.00): 90.00 degrees
To (-1.00, 3.00): 180.00 degrees
How It Works
Subtracting the player's position turns each target into a vector: dx = t[0] - px and dy = t[1] - py.
The first target has dx = 3 and dy = 4, so the angle is 53.13 degrees. The second has dx = 0, where a division would fail, but atan2() returns 90.00.
The third target is directly to the left, with dy = 0 and dx = -3. atan2() returns exactly pi, which is 180.00 degrees.
Frequently Asked Questions
std::atan2(y, x) returns the angle, in radians, of the point (x, y) measured from the positive x axis. It gives the right quadrant, and the result is between -pi and pi.
An angle in radians from -pi to pi. atan2(0, 0) returns 0, and the sign of a zero argument is used to pick between 0 and pi.
The y value comes first, then x. So std::atan2(y, x). Swapping them is a common bug, and it gives the angle from the y axis.
It returns 0 in most implementations, and it may set an error. The angle of the origin is not defined, so check for the point (0, 0) yourself if it can happen.
Include <cmath> and write std::atan2(). That always works and picks the C++ overloads. The global name ::atan2 often works too, but the standard does not promise it, and it may only take a double.