The atan2() function computes the angle from (0, 0) to a point (x, y) in the plane. It is the go-to C function for graphics, robotics, and game development when you need a direction angle that respects all four quadrants and handles x = 0 safely.
01
Two args
y then x.
02
Quadrants
Full circle.
03
Range
(−π, π].
04
x = 0
No crash.
05
vs atan
Better choice.
06
Radians
Output angle.
Fundamentals
Definition and Usage
atan2(y, x) returns the counterclockwise angle θ (in radians) from the positive x-axis to the ray through (x, y). Equivalently, it is the arc tangent of y/x with the signs of both arguments used to select the correct quadrant.
If you only know the slope y/x, atan(y/x) loses quadrant information. atan2 keeps y and x separate so (-1, -1) and (1, 1) produce different angles even though their ratios are equal.
💡
Beginner Tip
Remember the order: atan2(y, x)—vertical first, horizontal second. For a displacement from A to B, use atan2(by - ay, bx - ax).
Foundation
📝 Syntax
Standard C declaration:
C
double atan2(double y, double x);
Related variants
C
float atan2f(float y, float x); /* <math.h> */
long double atan2l(long double y, long double x); /* <math.h> */
Parameters
y — y-coordinate (vertical component, “rise”).
x — x-coordinate (horizontal component, “run”).
Return Value
Angle in radians in the range (−π, π].
atan2(0, 1) returns 0 (point on positive x-axis).
atan2(1, 0) returns π/2 (point straight up).
atan2(0, 0) is implementation-defined—avoid in production.
Headers and linking
#include <math.h>
Compile: gcc atan2.c -std=c11 -o atan2 -lm
Cheat Sheet
⚡ Quick Reference
Call
Point (x, y)
Angle (approx.)
atan2(0, 1)
Right on x-axis
0 rad (0°)
atan2(1, 1)
First quadrant
0.7854 (π/4, 45°)
atan2(1, 0)
Straight up
1.5708 (π/2, 90°)
atan2(0, -1)
Left on x-axis
3.1416 (π, 180°)
atan2(-1, -1)
Third quadrant
-2.356 (−3π/4)
Direction
atan2(y, x)
Angle to point
Delta
atan2(dy, dx)
From offset
Degrees
rad * 180 / M_PI
Convert
Avoid
atan(y/x)
Lost quadrant
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. atan2 is one of the most practical functions in <math.h>.
📚 Getting Started
Angle to the point (1, 1) in radians and degrees.
Example 1 — Basic atan2 with Degrees
Angle to the point where x = 1 and y = 1.
C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
int main(void) {
double y = 1.0;
double x = 1.0;
double rad = atan2(y, x);
double deg = rad * 180.0 / M_PI;
printf("atan2(%.1f, %.1f) = %.6f radians\n", y, x, rad);
printf("atan2(%.1f, %.1f) = %.1f degrees\n", y, x, deg);
return 0;
}
atan2( 5, 0) = 1.5708 rad (90.0 deg)
atan2(-5, 0) = -1.5708 rad (-90.0 deg)
How It Works
When x = 0, atan2 returns ±π/2 based on the sign of y. Never write atan(y/x) when x might be zero.
Applications
🚀 Common Use Cases
2D game graphics — rotate a sprite to face a target.
Robotics — heading angle from wheel odometry deltas.
Touch and mouse input — angle from center of joystick to touch point.
Navigation — bearing between two GPS-style coordinates.
Vector math — convert (x, y) components to polar angle.
🧠 How atan2() Works
1
Read y and x
Inspect signs and handle x = 0 without dividing.
Input
2
Select quadrant
Combine ratio y/x with sign information.
Logic
3
Return θ
Angle in (−π, π] from +x axis to (x, y).
Result
=
🚀
Direction angle
Ready for rotation matrices, aiming, or polar coordinates.
Important
📝 Notes
Argument order is atan2(y, x)—y first, not x first.
Prefer atan2 over atan(y/x) for coordinates.
Output range is (−π, π], not limited to [−π/2, π/2] like atan.
atan2(0, 0) is implementation-defined—guard against zero length.
Link with -lm when using GCC/Clang on Unix-like systems.
Performance
⚡ Optimization
atan2() is highly optimized in standard math libraries. It avoids the extra division and branch logic you would write by hand with atan(y/x). For hot loops with float data, use atan2f() to match your precision.
Wrap Up
Conclusion
atan2(y, x) is the standard way to get a full-circle angle from Cartesian coordinates. Remember y-before-x, avoid atan(y/x) for direction problems, and guard the (0, 0) case.
Continue with atanh() for inverse hyperbolic tangent, or review atan() for the single-argument version.
atan2(y, x) returns the angle in radians from the positive x-axis to the point (x, y). For example, atan2(1, 1) is pi/4 (45 degrees) because the point (1, 1) lies in the first quadrant at that angle.
C follows the mathematical convention atan2(y, x) for the angle whose tangent is y/x, while using both signs to pick the correct quadrant. Think 'rise (y), then run (x)'.
atan(x) takes one ratio and returns an angle in [-pi/2, pi/2]. atan2(y, x) uses separate coordinates, handles all four quadrants, returns (-pi, pi], and safely handles x = 0.
The result is in (-pi, pi] radians (about -180° to 180°). atan2(0, 1) is 0; atan2(1, 0) is pi/2; atan2(0, -1) is pi.
atan2(0, 0) is implementation-defined. Do not rely on a specific value—check for the zero vector before calling atan2 in production code.
Yes on many systems: gcc program.c -o program -lm. Include <math.h> for the declaration double atan2(double y, double x).
Did you know?
Many languages copied C’s atan2(y, x) signature—Python, JavaScript, Java, and Rust all use the same y-then-x order. Once you learn it in C, you can transfer the pattern directly to graphics code in other languages.