TactivergeLite

Lite / Intelligent Robotic Systems / Week 10

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 · Full interactive version

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.

Predict

Which direction (anticlockwise from +x) does the vector (−1, −1) point in?

  1. 45°
  2. 135°
  3. 225°
  4. 315°
Guess first, then reveal

225°. It is in the third quadrant (both parts negative). tan⁻¹(−1/−1) = tan⁻¹(1) = 45° because the two minus signs cancel in the division, so the quadrant information is lost. atan2(−1, −1) keeps both signs and returns −135°, which is the same direction as 225°.

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?

What the interactive shows (text version)

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).

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Vectors and 2D Motion: Crash Course Physics #4” by CrashCourse (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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.

Technical

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.

Mathematical

(−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.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. atan2(3, −4) is closest to which angle?

°

m

m

Practice 5. For which vector does tan⁻¹(y/x) give the correct direction?

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).

Reflect

Describe a real robot failure that could come from using tan⁻¹ instead of atan2. Who would notice first, and how?

Write your answer on paper or in your own notes. Lite never stores what you write.

Go further

← Vectors: arrows you can addThe dot product: how much do two arrows agree? →