Skip to the lesson
FixTheRobot Explore Robotics / Linear Algebra / M1 Mathematical Foundations / M1-T2

Linear Algebra · Theory · M1

M1-T2 — Dot Product, Norms and Angles

The dot product answers one question: how much does one arrow point along another? Robots use it to measure, compare directions and split motion into parts.

  • Lesson 2 of 3
  • Module 1: Mathematical Foundations
  • Foundations
  • About 11 minutes to read

A robot constantly needs numbers, not just arrows: how far is the goal, how many degrees must I turn, is that obstacle in front of me, how fast am I progressing along the path? All four come from one operation. You will derive the dot product from the law of cosines, read it as a shadow, and learn the numerical trap that makes the textbook angle formula unreliable in code.

Learning objectives

After this lesson, you should be able to:

  • Compute the length of a vector, the distance between two positions and a unit vector, and say when a unit vector is undefined.
  • Compute a dot product from components and interpret its sign and size as how much two arrows agree.
  • Derive u · v = |u| |v| cos θ from the law of cosines, and use it to find the angle between two vectors.
  • Split a vector into the part along a direction and the part perpendicular to it, and apply that to heading error and along-path speed.

Introduction and motivation

A robot cannot act on an arrow directly. It needs numbers: how far is the goal, how many degrees must I turn, is that obstacle in front of me, how fast am I advancing along the path? The dot product answers all four, which is why it appears in almost every navigation, perception and control routine you will meet, from a camera field-of-view test to a path-following controller.

The intuition

Take any arrow and shine a light straight down onto a line. The arrow casts a shadow on the line. The dot product with a unit arrow along the line is the signed length of that shadow: positive when the arrow leans the same way as the line, zero when it is perpendicular, negative when it leans against it.

Before that, you need the plain measuring tools. The length (or norm) of a vector comes from Pythagoras: walk 4 m east and 3 m north and you are 5 m from where you started. The distance between two places is the length of the displacement between them. A unit vector is an arrow of length 1: it keeps only the direction, which is what you want when you say “head that way”.

Interactive figure. Two arrows u and v start at the origin. The shadow of u on the line through v is drawn in green, with a dashed perpendicular drop. The readout shows u · v computed from components, the same number computed as |u| |v| cos θ, the signed length of the shadow, and whether the angle is acute, right or obtuse. This figure needs JavaScript to be interactive.

Drag u and v. The green arrow is the shadow of u on the line of v. The dot product, the |u||v|cos θ form and the shadow length are read out below; watch them agree, and watch the sign flip when the angle passes 90°.

Three cases to remember

  • Acute angle (less than 90°): the dot product is positive. The arrows point the same general way.
  • Right angle (exactly 90°): the dot product is zero. The arrows are orthogonal: neither has any shadow on the other.
  • Obtuse angle (more than 90°): the dot product is negative. The arrows point against each other.

The sign of a dot product tells you which side of a perpendicular wall a direction is on. That is why one multiplication can answer “is it in front of me?”.

Geometric and physical interpretation

Geometric view. The dot product of u with a unit arrow is the signed length of the shadow of u on that arrow: positive when they lean the same way, zero at a right angle, negative when they oppose. Physical view. It is the work done by a force along a displacement, and the part of a velocity that makes progress along a path. Both are the same shadow.

The mathematics

For vectors in the plane, u = (ux, uy) and v = (vx, vy). The same definitions hold in 3D with a third component.

|u|
the length (norm) of u.
u · v
the dot product of u and v: a single number, not a vector.
θ
the angle between u and v, from 0° to 180°.
v̂
the unit vector along v, v̂ = v / |v|.

Length, distance and direction

|u| = √(ux² + uy²)         dist(p, q) = |q − p|         û = u / |u|   (u ≠ 0)

What it means. The length is Pythagoras on the components. The distance between two positions is the length of the displacement between them. Dividing by the length leaves only direction; for the zero vector there is no direction, so û does not exist.

The dot product

u · v = ux·vx + uy·vy

What it means. Multiply matching components and add. Each product is positive when the two components point the same way along that axis and negative when they oppose, so the total measures how much the arrows agree overall.

u · v = |u| |v| cos θ

What it means. The same number from geometry. The length of u times the length of v times how aligned they are: cos θ is 1 when parallel, 0 when perpendicular and −1 when opposite. The derivation below shows why the two formulas agree.

Angle between two vectors

θ = arccos( u · v / (|u| |v|) )

What it means. Rearranging the geometric form gives the cosine of the angle; arccos turns it back into an angle between 0° and 180°. Beware: this is the formula that fails near 0° and 180° in floating point. See the engineering section.

Shadows: projection

comp_v(u) = u · v̂              proj_v(u) = ( (u · v) / (v · v) ) v

What it means. The scalar projection is the signed length of the shadow of u on the line of v. The vector projection is that shadow drawn as an arrow along v. What is left, u − proj_v(u), is perpendicular to v.

Length, distance, angle and shadow are all the same ingredient: the dot product. Orthogonal means a dot product of zero.

Step-by-step derivation: where u · v = |u||v|cos θ comes from

The two arrows u and v and the third side u − v form a triangle. The picture shows it.

The triangle behind the law of cosinesTwo arrows u and v start at the same point with an angle theta between them. The third side of the triangle is the arrow from the tip of v to the tip of u, which is u minus v. Its squared length is the squared lengths of u and v minus twice |u| |v| cos theta.u v u − v θ |u − v|² = |u|² + |v|² − 2 |u| |v| cos θ law of cosines (geometry) |u − v|² = |u|² − 2 (u · v) + |v|² expand with components (algebra) so u · v = |u| |v| cos θ
Geometry says |u − v|² = |u|² + |v|² − 2|u||v| cos θ. Algebra says |u − v|² = |u|² − 2 u·v + |v|². Both are the same quantity, so u·v = |u||v| cos θ.
  1. Geometry (law of cosines). In a triangle with two sides of length |u| and |v| meeting at angle θ, the third side satisfies |u − v|² = |u|² + |v|² − 2 |u||v| cos θ. (For θ = 90° this is Pythagoras.)
  2. Algebra. Write the squared length with components: |u − v|² = (ux − vx)² + (uy − vy)².
  3. Expand the squares: = ux² − 2 ux vx + vx² + uy² − 2 uy vy + vy².
  4. Regroup: = (ux² + uy²) + (vx² + vy²) − 2 (ux vx + uy vy) = |u|² + |v|² − 2 (u · v).
  5. Compare. Both steps 1 and 4 equal |u − v|². Cancel |u|² + |v|² and divide by −2: u · v = |u| |v| cos θ.

Perpendicular means zero. At θ = 90°, cos θ = 0, so u · v = 0; and if u · v = 0 with both vectors non-zero then cos θ = 0, so θ = 90°.

The shadow formula. Let t be the signed length of the shadow of u on the line of v, so the shadow is t v̂. The leftover u − t v̂ must be perpendicular to v̂, so its dot product with v̂ is zero:

(u − t v̂) · v̂ = 0   ⇒   u · v̂ − t (v̂ · v̂) = 0   ⇒   t = u · v̂

What it means. Because v̂ · v̂ = |v̂|² = 1, the unknown t drops out directly. The shadow length is the dot product with the unit vector. Multiplying by v̂ = v/|v| gives proj_v(u) = ((u·v)/|v|²) v.

Two consequences used constantly. First, since |cos θ| ≤ 1, we get |u · v| ≤ |u| |v| (the Cauchy–Schwarz inequality). Second, expanding |a + b|² = |a|² + 2 a·b + |b|² ≤ (|a| + |b|)² gives the triangle inequality |a + b| ≤ |a| + |b|: a detour is never shorter than the direct way.

A worked example: is the beacon ahead, and how far must the robot turn?

A robot at r = (1, 2) faces θh = 30°, so its heading vector is h = (cos 30°, sin 30°) = (0.8660, 0.5). A beacon is at t = (5, 5). Its camera sees 45° to either side of its heading.

  1. Displacement and distance. d = t − r = (4, 3), so |d| = √(16 + 9) = 5 m.
  2. Dot product with the heading. d · h = 4(0.8660) + 3(0.5) = 3.4641 + 1.5 = 4.9641. It is positive, so the beacon is in front of the robot.
  3. Cosine of the angle. cos θ = d · h / (|d| |h|) = 4.9641 / 5 = 0.99282, so θ = arccos(0.99282) = 6.87°. (Check with bearings: the beacon's bearing is atan2(3, 4) = 36.87°, and 36.87° − 30° = 6.87°.)
  4. Is it visible? The field of view needs cos θ ≥ cos 45° = 0.7071. Since 0.99282 ≥ 0.7071, the beacon is inside the camera cone.
  5. Forward part. The shadow of d on the heading is the forward component, 4.9641 m. The rest is sideways: √(5² − 4.9641²) = √(25 − 24.6422) = 0.5981 m.

A second object at displacement (−2, 1) gives d · h = −2(0.8660) + 0.5 = −1.2321, negative: it is behind the robot's centre line, and cos θ = −1.2321 / 2.2361 = −0.551 fails the 0.7071 test. Two quick extras: u = (3, 1) and v = (−1, 3) give u · v = −3 + 3 = 0, so they are perpendicular; and in 3D, u = (1, 2, 2) (length 3) and v = (2, 0, 0) (length 2) give u · v = 2, cos θ = 2 / 6 = 1/3, θ = 70.53°.

Robotics application

Heading error and the forward component. The dot product of the displacement with the heading is how far the goal is straight ahead. A controller that drives forward at a speed proportional to that number slows down and then stops when the goal is abeam, instead of circling it.

Field-of-view and “in front of me” tests. A camera, a radar or a bumper zone covers a cone around the heading. An object is inside it when h · d̂ ≥ cos(half-angle). No trigonometric inversion, one comparison per object, which is why it is used inside inner loops.

A camera field of view tested with the dot productA robot faces right with a camera that sees 45 degrees to each side of its heading. Object A is inside the cone, and its direction has cosine 0.99 with the heading. Object B is above the cone and has cosine 0.62. Object C is behind the robot and has cosine minus 0.95. The threshold is cosine 45 degrees, about 0.71. heading h A: cos θ = 0.99 ≥ 0.71inside the cone: seen B: cos θ = 0.62 < 0.71outside the cone C: cos θ = −0.95behind the robot threshold: cos 45° ≈ 0.71
The dot product of the heading with the unit direction to an object is cos θ. Comparing it with cos 45° decides visibility without computing any angle.

Along-path speed. A robot with velocity v on a path with unit direction û makes progress at v · û. The rest, √(|v|² − (v · û)²), is sideways motion that does not advance it. Velocity (2, 1) m/s along a path with û = (0.6, 0.8) progresses at 1.2 + 0.8 = 2.0 m/s and slides at √(5 − 4) = 1.0 m/s.

Goal tolerance and ranges. “Arrived” is |t − r| < tolerance. A LiDAR range is the length of the vector from sensor to hit point. A wall approach is perpendicular when the direction of travel dotted with the wall's direction is zero. These small uses of the norm and dot product are in almost every navigation stack, including the pure pursuit controller.

Engineering implications and limitations

Practical rules

  • Do not trust arccos near ±1. The ratio u·v / (|u||v|) can round to 1.0000000000000002, which makes arccos raise a domain error. It also loses the angle: for u = (1, 0) and v = (1, 10⁻⁹) the cosine is 1.0 to full precision, so arccos returns 0 instead of 10⁻⁹. The remedy is θ = atan2(|u × v|, u · v), using the cross product of the next lesson, which stays accurate for every angle.
  • Clamp if you must use arccos. Force the ratio into [−1, 1] first. Mathematically it is already there (Cauchy–Schwarz), so the clamp only absorbs rounding.
  • Guard the zero vector. Normalising a zero vector divides by zero. Decide what “no direction” should do (stay put, keep the last heading) and handle it explicitly.
  • Compute lengths safely. math.hypot and scaled norms avoid overflow and underflow that a naive sqrt(x*x + y*y) can hit with extreme values.
  • Compare tolerances, not zero. Two directions are “perpendicular” when |u·v| is below a tolerance scaled by |u||v|, never when it equals 0.0 exactly.

Limits

The angle between two vectors is always between 0° and 180°: it has no sign. It cannot tell you whether to turn left or right. For that you need the signed angle, which uses the cross product (next lesson).

Common misconceptions

“The dot product is a vector.”

It is a single number. Two arrows go in, one signed number comes out. The cross product, in 3D, is the one that returns a vector.

“A zero dot product means one of the vectors is zero.”

It is far more often a sign that the two arrows are perpendicular. (3, 1) · (−1, 3) = 0 with both non-zero.

“|a + b| = |a| + |b|.”

Only when a and b point the same way. In general |a + b| ≤ |a| + |b|, with equality only for parallel, same-direction arrows. This is the triangle inequality.

“arccos of the normalised dot product is always a fine way to get an angle.”

It is exact on paper and fragile in code: it can raise a domain error and it loses all resolution for nearly parallel or nearly opposite vectors. Use atan2 with the cross product instead.

Engineering takeaways

  1. Length is √(x² + y²), distance is the length of a difference, and a unit vector keeps only direction (and does not exist for the zero vector).
  2. u · v = ux·vx + uy·vy = |u| |v| cos θ: components and geometry agree, and the law of cosines proves it.
  3. The sign of u · v says acute, perpendicular or obtuse; zero means orthogonal.
  4. u · v̂ is the signed length of the shadow of u on v; subtracting the shadow leaves the perpendicular part.
  5. In code, avoid arccos near ±1; use atan2(|u × v|, u · v) and guard against zero vectors.

References and further reading

Where to read more. Free to read does not mean free to copy: follow each licence.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

Two vectors have lengths 3 and 4 and a dot product of −6. What is the angle between them, and what would that sign mean for a camera that looks along u and sees an object at v?

Explanation

cos θ = u·v / (|u||v|) = −6 / 12 = −0.5, so θ = 120°. The negative dot product means the object is more than 90° away from the viewing direction, so it is on the far side of the camera's centre line: with a typical field of view (well under ±90°) it would not be seen.

Question 2

Explain why computing an angle as arccos(u·v / (|u||v|)) is risky when u and v are nearly parallel, and write the safer formula.

Explanation

Near θ = 0 the cosine is very flat (cos θ ≈ 1 − θ²/2), so a tiny angle disappears in rounding: for θ = 10⁻⁹ the cosine rounds to exactly 1 and arccos returns 0. Rounding can also push the ratio just above 1, which makes arccos fail. The safer formula is θ = atan2(|u × v|, u · v), which uses the cross product and has no flat region. Clamping the ratio into [−1, 1] only fixes the domain error, not the lost precision.

Question 3

A robot moves with velocity (2, 1) m/s along a straight path whose unit direction is (0.6, 0.8). How fast is it advancing along the path, and how fast is it moving sideways?

Explanation

Advancing speed is the dot product v · û = 2(0.6) + 1(0.8) = 2.0 m/s. The total speed is |v| = √5 ≈ 2.236 m/s, so the sideways speed is √(|v|² − 2.0²) = √(5 − 4) = 1.0 m/s. Only the shadow of the velocity on the path counts as progress.

What’s next

The dot product tells you how far two arrows agree, but not which way to turn. The cross product adds the missing sign, and with it you can describe lines, paths and planes.

Practice this lesson

2D Robot Vector Navigator

Steer a simulated robot to a beacon with vectors: compute the heading, the distance and the sideways error from the planned path.

Next lesson

M1-T3 — Cross Product, Lines and Planes

Use the cross product for turning direction, torque and surface normals, and write lines and planes in vector form.

Nothing below is required to finish this lesson.

  • Before
    M1-T1: Vectors and Coordinates

    Describe a robot's position, velocity and heading as vectors in a chosen coordinate system, and combine motions by adding and scaling vectors.

  • Lab
    M1-L1: 2D Robot Vector Navigator

    Steer a simulated robot to a beacon with vectors: compute the heading, the distance and the sideways error from the planned path.