Dot Product & Projection — 2D
Math for AI
Linear Algebra
Animation of two vectors, the angle θ, dot product a·b = |a||b|cosθ, and the projection of a onto b.
Dot Product & Projection — 2D
Speed
Relations:
a·b = |a||b| cosθ, projb(a) = (a·b/|b|2) b. Here, b is fixed along +x; a rotates, so projection length = |a| cosθ.|a| =
|b| =
θ =
a·b =
proj len =
Step 0
What you’re seeing
Two vectors a and b from the origin. The angle between them is \(\theta\). The dot product is both:
\[ a\cdot b \;=\; a_x b_x + a_y b_y \;=\; \lVert a\rVert \,\lVert b\rVert \cos\theta. \]
The projection of \(a\) onto \(b\) is the shadow of \(a\) along \(b\):
\[ \mathrm{proj}_b(a) \;=\; \frac{a\cdot b}{\lVert b\rVert^2}\, b \quad\text{and its length is}\quad \lVert a\rVert\cos\theta. \]
Try this in the demo
- Rotate \(a\) until \(a\cdot b = 0\) — the projection should collapse to the origin.
- Fix \(\lVert a\rVert\) and sweep \(\theta\): watch dot and projection length follow \(\cos\theta\).
- Make \(\theta>90^\circ\): the projection vector flips direction along \(-b\).
Why this matters (intuition)
- \(a\cdot b\) measures alignment. Positive → same-ish direction, zero → perpendicular, negative → opposite.
- Projection = “how much of \(a\) lives along \(b\).” It’s the component of \(a\) in the \(b\) direction.
Where it’s used (some use-cases)
- Cosine similarity in NLP/IR (compare sentence/embedding direction).
- Work/energy in physics (force along a displacement).
- Shading in graphics (light direction \(\cdot\) surface normal).
- Linear models & PCA (coefficients/projections onto axes/PCs).
- Optimization (step size along a search direction).
Quick read of the picture
- \(\theta = 0^\circ \Rightarrow a\cdot b = \lVert a\rVert\lVert b\rVert\) (max alignment).
- \(\theta = 90^\circ \Rightarrow a\cdot b = 0\) (orthogonal).
- \(\theta = 180^\circ \Rightarrow a\cdot b = -\lVert a\rVert\lVert b\rVert\) (opposite).
Common gotchas
- Sign of projection: If \(\theta>90^\circ\), projection length is negative (points opposite \(b\)).
- Normalize carefully: If you use the projection length, you need either \(\hat b = b/\lVert b\rVert\) or divide by \(\lVert b\rVert\) appropriately.
- Zero vectors: Dot/product and projection are undefined when \(\lVert b\rVert=0\).
One-liners to remember
- Dot product = alignment × sizes.
- Projection = the piece of \(a\) that points along \(b\).
Earning Opportunity with AI
Grab Great Deals
Enjoyed this post?
If this article helped you,
☕ Buy me a coffee
Your support keeps more things coming!