The dot product: how much do two arrows agree?
One number can tell a robot whether a target is in front of it or behind it, without a single angle being measured. How?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
The dot product turns two arrows into a single number that measures how much they point the same way. Robots use it to check whether an obstacle is ahead, how much of a force pushes along a rail, and how bright a lit surface looks. It's the cheapest useful calculation in all of geometry.
- ◇ Compute a dot product from components.
- ◇ Use a · b = |a||b| cos θ to find the angle between two vectors.
- ◇ Interpret the sign of a dot product and use it to test for perpendicular vectors and projections.
2.Predict
a = (3, 1) and b = (−1, 3). Without drawing, what can you say about the angle between them?
Guess first. Being wrong here is how the surprise works.
3.Discover
Switch to 'Dot product and angle'. Keep b = (3, 0). Swing a round (try (3, 2), (0, 3), (−2, 2), (−3, −1)). Watch the sign of a · b and the green shadow. When is the dot product zero? When is it biggest for a fixed |a|?
Loading the interactive…
Can't use the interactive? Read a description instead
In dot-product mode the playground draws a and b from the origin and shades the shadow of a along b in green. The readout gives the dot product, both lengths, the angle between them and the shadow length. For b = (3, 0) the dot product is 3 × a's x part: positive when a leans right, zero when a points straight up or down, negative when it leans left.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Find a · b for a = (4, −1) and b = (2, 3).
5.Watch
→ Watch for the projection picture: the dot product is the length of one vector's shadow on the other, times the other's length.
Why multiplying components and adding gives the same number as lengths times the cosine of the angle: the projection picture behind the dot product.
14 min 11 s · 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.
6.Understand
The recipe
a · b = ax·bx + ay·by: multiply matching parts and add. The result is a plain number (a scalar), not a vector. Order doesn't matter: a · b = b · a. And a · a = |a|², so the dot product contains Pythagoras.
What the sign says
From a · b = |a||b| cos θ: positive when the angle is under 90° (pointing roughly together), zero when perpendicular, negative when over 90° (pointing roughly apart). Lengths are never negative, so the sign comes entirely from cos θ.
Angles and shadows
Rearranged, cos θ = a · b ÷ (|a||b|), which gives the angle between any two non-zero vectors. And a · b ÷ |b| is the scalar projection of a on b: the length of a's shadow along b's direction. That is how a robot finds the part of a force that acts along a slide, or how far ahead an obstacle is along its heading.
Explain it another way
Imagine shining a torch straight down onto b. a casts a shadow along b. A long shadow pointing the same way gives a big positive dot product; a shadow pointing backwards gives a negative one; no shadow at all means the arrows are at right angles.
The dot product is the standard inner product on ℝ². It is linear in each argument, symmetric, and positive-definite (a · a > 0 for a ≠ 0). It is invariant under rotation, so the angle it measures doesn't depend on the frame you choose.
a = (3, −6), b = (−1, −6): a · b = −3 + 36 = 33. |a| = √45 ≈ 6.708, |b| = √37 ≈ 6.083. cos θ = 33 / 40.80 ≈ 0.8087, θ ≈ 36.0°.
A rover with heading vector h = (cos ψ, sin ψ) and a rock at relative position r computes h · r. Positive: the rock is ahead. Negative: behind. Its size is how far ahead, measured along the heading. No trigonometry needed in the loop.
Derivation: Why a · b = |a||b| cos θ (from the law of cosines)
Draw a and b from the same point, with angle θ between them. The third side of the triangle is the vector a − b.
The law of cosines for that triangle says: |a − b|² = |a|² + |b|² − 2|a||b| cos θ.
Expand the left side with components: |a − b|² = (ax − bx)² + (ay − by)² = ax² + ay² + bx² + by² − 2(ax·bx + ay·by).
But ax² + ay² = |a|² and bx² + by² = |b|², so the left side is |a|² + |b|² − 2(ax·bx + ay·by).
Compare the two expressions: the |a|² and |b|² cancel, leaving ax·bx + ay·by = |a||b| cos θ. The component recipe and the geometric meaning are the same number.
Worked example 1
Is the target at r = (−2, 5) ahead of or behind a robot facing h = (0.6, −0.8)?
h · r = 0.6 × (−2) + (−0.8) × 5 = −1.2 − 4 = −5.2. Negative: the target is behind, 5.2 m back along the heading line (h has length 1).
Common mix-ups
- The dot product of two vectors is another vector.
✓ It is a single number. (Multiplying each part and keeping them separate is a different operation.) - A negative dot product means one vector is 'negative'.
✓ It means the angle between them is more than 90°: they point roughly in opposite directions.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Find the dot product a · b for a = (−5, 5) and b = (−4, −5).
Practice 3 · a fresh version every time
What is the angle between a = (−2, 0) and b = (1, 1), in degrees (0 to 180)?
Practice 4
Which vector is perpendicular to (2, 5)?
Practice 5
A 10 N force F = (8, 6) N pushes a cart that can only roll along the x axis. How many newtons act along the rail? (Hint: dot F with the unit vector (1, 0).)
8.Apply it
Write a function is_ahead(heading_deg, rx, ry) that returns True when a point at (rx, ry) relative to a robot is in front of it, using only a dot product. Test it with a heading of 90° and points (0, 3), (2, −1) and (−4, 0.5).
9.Reflect
10.Go further
- Dot product (Wikipedia) ↗Both definitions, their equivalence and many applications.
Concepts: Vectors