Which way is it pointing? Direction with atan2
Your calculator says tan⁻¹(−1 ÷ −1) = 45°. But (−1, −1) points down and to the left. Who is wrong?
About 30 min · 3 goals · Checked by automated tests
1.Why should I care?
Robots constantly flip between two descriptions of the same arrow: components (good for adding) and length plus angle (good for steering a wheel or setting a joint). Getting the angle wrong by 180° sends a rover backwards, so every robotics library has a special function, atan2, to get it right.
- ◇ Convert between components (x, y) and magnitude–direction form (r, θ).
- ◇ Use atan2(y, x) to find a direction correctly in every quadrant.
- ◇ Explain why tan⁻¹(y/x) fails when x is negative.
2.Discover
Set b = (0, 0) so that a + b is just a. Put a in each quadrant in turn: (3, 2), (−3, 2), (−3, −2), (3, −2), and record the direction the readout gives. For each, also compute tan⁻¹(y/x) on a calculator. Which quadrants does tan⁻¹ get wrong?
Loading the interactive…
Can't use the interactive? Read a description instead
In the vector playground, a blue arrow from the origin can be set to any component values; the readout gives its length and its direction from atan2 in degrees. Placing it in each quadrant shows directions of about 34°, 146°, 214° and 326° for (±3, ±2).
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
atan2(3, −4) is closest to which angle?
4.Predict
Which direction (anticlockwise from +x) does the vector (−1, −1) point in?
Guess first. Being wrong here is how the surprise works.
5.Understand
Two ways to write the same arrow
Components: (x, y). Polar form: length r and direction θ, measured anticlockwise from +x. To go from polar to components: x = r cos θ, y = r sin θ. To go back: r = √(x² + y²) and θ from the inverse tangent, carefully.
Why tan⁻¹(y/x) lies
tan θ = y/x, but dividing throws away the signs: (1, 1) and (−1, −1) both give y/x = 1. So tan⁻¹ can only answer between −90° and 90°: it is right for vectors pointing rightwards (x > 0) and exactly 180° out for vectors pointing leftwards. At x = 0 it divides by zero.
atan2: the angle that knows its quadrant
atan2(y, x) takes the two parts separately, checks their signs and returns an angle in (−180°, 180°]. Add 360° to negative answers if you want 0–360°. It is in every language: Python's math.atan2, JavaScript's Math.atan2, C's atan2. Note the order: y first. Robot code uses it to turn components back into headings and joint angles.
Explain it another way
tan⁻¹ is like asking 'how steep is this line?' A line has two ends, so steepness can't tell you which end the arrow points to. atan2 looks at the arrow's actual x and y and says which way it really points.
atan2(y, x) equals tan⁻¹(y/x) for x > 0, tan⁻¹(y/x) + 180° for x < 0 and y ≥ 0, tan⁻¹(y/x) − 180° for x < 0 and y < 0, and ±90° on the y-axis; it is undefined only at the origin.
(−3, 2): r = √13 ≈ 3.606; tan⁻¹(2/−3) = −33.7°, wrong; atan2(2, −3) = 146.3°. Check: 3.606 × cos 146.3° ≈ −3.000, 3.606 × sin 146.3° ≈ 2.000.
A mobile robot that knows its target is at (−3, 2) m relative to itself turns to atan2(2, −3) ≈ 146° before driving. With tan⁻¹ it would face −34° and drive away from the target.
Worked example 1
Give the direction of (4, −9) in degrees from 0 to 360.
Fourth quadrant. atan2(−9, 4) ≈ −66.0°; add 360° → 294.0°. (Here tan⁻¹ happens to agree, −66°, because x > 0.)
Worked example 2
A 2.5 m link points at 200°. Where is its tip?
x = 2.5 cos 200° ≈ −2.349 m, y = 2.5 sin 200° ≈ −0.855 m. Both negative: third quadrant, as expected for 200°.
Common mix-ups
- tan⁻¹(y/x) always gives a vector's direction.
✓ Only when x > 0. Use atan2(y, x), which checks both signs. - atan2 takes (x, y) like a coordinate.
✓ It takes (y, x): the y component first. Swapping them gives the angle measured from the y axis instead.
6.Watch
→ Watch for how a velocity at an angle is split into horizontal and vertical components with cosine and sine.
Splits motion into horizontal and vertical parts that run independently, the trick behind every projectile calculation.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
What is the direction of the vector (−6, 6), in degrees measured anticlockwise from the +x axis? Give a value from 0 to 360.
Practice 3 · a fresh version every time
A robot link 0.8 m long points at 140° (anticlockwise from +x). What is the y position of its tip, in metres? (3 decimal places)
Practice 4
A target is at (−5, 12) m from a robot. How far away is it, in metres?
Practice 5
For which vector does tan⁻¹(y/x) give the correct direction?
8.Apply it
Write a three-line Python function heading(x, y) that returns a direction in degrees from 0 to 360 using math.atan2 and math.degrees. Test it on one vector in each quadrant and on (0, −3).
9.Reflect
10.Go further
- atan2 (Wikipedia) ↗The full definition, case by case, and its history in programming languages.
Concepts: Vectors · Angles and trigonometry