TExcelTAD
Home › Calculus Essentials › Dot and Cross Products

Dot and Cross Products

There are two ways to multiply two vectors. The dot product gives a number that measures how much they point the same way. The cross product gives a new vector perpendicular to both. Between them they handle angles, projections, lighting, field-of-view checks and surface normals.

The dot product

For a=⟨a1,a2,a3⟩\mathbf{a} = \langle a_1, a_2, a_3 \rangle and b=⟨b1,b2,b3⟩\mathbf{b} = \langle b_1, b_2, b_3 \rangle:

a⋅b=a1b1+a2b2+a3b3(algebraic form)a⋅b=∥a∥ ∥b∥cos⁡θ(geometric form, θ=angle between them)\begin{aligned} \mathbf{a} \cdot \mathbf{b} &= a_1b_1 + a_2b_2 + a_3b_3 && \text{(algebraic form)} \\ \mathbf{a} \cdot \mathbf{b} &= \lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert \cos\theta && \text{(geometric form, } \theta = \text{angle between them)} \end{aligned}

The two forms are equal, which gives a way to find the angle between any two vectors: cos⁡θ=a⋅b∥a∥ ∥b∥\cos\theta = \dfrac{\mathbf{a} \cdot \mathbf{b}}{\lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert}. The sign of the dot product alone tells you a lot:

a⋅b\mathbf{a} \cdot \mathbf{b} Angle θ\theta Meaning
>0> 0 less than 90° roughly the same direction
=0= 0 exactly 90° perpendicular (orthogonal)
<0< 0 more than 90° roughly opposite directions

Two more facts: a⋅a=∥a∥2\mathbf{a} \cdot \mathbf{a} = \lVert \mathbf{a} \rVert^2, and the projection of b\mathbf{b} onto a\mathbf{a}, the part of b\mathbf{b} that lies along a\mathbf{a}, is (a⋅ba⋅a)a\left(\dfrac{\mathbf{a} \cdot \mathbf{b}}{\mathbf{a} \cdot \mathbf{a}}\right) \mathbf{a}. Its signed length a⋅b∥a∥\dfrac{\mathbf{a} \cdot \mathbf{b}}{\lVert \mathbf{a} \rVert} is the scalar projection (the component of b\mathbf{b} along a\mathbf{a}).

python · try it yourself
import numpy as np

a = np.array([2.0, 1.0, -2.0])
b = np.array([3.0, 4.0, 0.0])

dot = np.dot(a, b)
cos_t = dot / (np.linalg.norm(a) * np.linalg.norm(b))
print("a . b       =", dot)
print("angle (deg) =", round(float(np.degrees(np.arccos(cos_t))), 2))
proj = dot / np.dot(a, a) * a
print("proj of b on a =", np.round(proj, 4))
print("rest of b      =", np.round(b - proj, 4), " (dot with a:", round(float(np.dot(b - proj, a)), 10), ")")
a . b       = 10.0
angle (deg) = 48.19
proj of b on a = [ 2.2222  1.1111 -2.2222]
rest of b      = [0.7778 2.8889 2.2222]  (dot with a: 0.0 )

Projection splits b\mathbf{b} into a part along a\mathbf{a} and a part perpendicular to a\mathbf{a}. The check at the end shows that the leftover part really is perpendicular to a\mathbf{a}.

The dot product in games

Diffuse lighting. In the Lambert model a surface is brightest when it faces the light. With unit normal n\mathbf{n} and unit vector l\mathbf{l} pointing toward the light, the brightness is max⁡(0,n⋅l)\max(0, \mathbf{n} \cdot \mathbf{l}), which is cos⁡θ\cos\theta clamped at zero for surfaces facing away.

Field of view. An enemy with unit forward vector f\mathbf{f} can see a target if the angle between f\mathbf{f} and the unit direction d\mathbf{d} to the target is at most half the view cone. Comparing cosines avoids calling acos at all: the target is visible when f⋅d≥cos⁡(fov/2)\mathbf{f} \cdot \mathbf{d} \ge \cos(\text{fov} / 2).

python · try it yourself
import numpy as np

def unit(v):
    return v / np.linalg.norm(v)

# Lambert lighting for three surface normals
light = unit(np.array([1.0, 2.0, 2.0]))
for n in [np.array([0.0, 1.0, 0.0]), unit(np.array([1.0, 1.0, 1.0])), np.array([0.0, -1.0, 0.0])]:
    print("normal", np.round(n, 3), "brightness", round(max(0.0, float(np.dot(n, light))), 3))

# Field-of-view check: 90° cone, guard at the origin facing +x
forward, half_fov_cos = np.array([1.0, 0.0]), np.cos(np.radians(45))
for target in [np.array([5.0, 2.0]), np.array([3.0, 4.0]), np.array([-1.0, 0.5])]:
    d = unit(target)
    print(target, "visible" if np.dot(forward, d) >= half_fov_cos else "hidden")
normal [0. 1. 0.] brightness 0.667
normal [0.577 0.577 0.577] brightness 0.962
normal [ 0. -1.  0.] brightness 0.0
[5. 2.] visible
[3. 4.] hidden
[-1.   0.5] hidden

The target at (3, 4) is 53° off the forward axis, just outside the 45° half-angle. More in Game Math's Dot Product and Angle Between Vectors. The same formula, called cosine similarity, compares text embeddings in vector search.

The cross product

The cross product exists only in 3D. It is easiest to remember as a determinant:

a×b=∣ijka1a2a3b1b2b3∣=⟨a2b3−a3b2, a3b1−a1b3, a1b2−a2b1⟩\mathbf{a} \times \mathbf{b} = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \end{vmatrix} = \langle a_2b_3 - a_3b_2,\ a_3b_1 - a_1b_3,\ a_1b_2 - a_2b_1 \rangle

Its properties:

  • a×b\mathbf{a} \times \mathbf{b} is perpendicular to both a\mathbf{a} and b\mathbf{b}. Its direction follows the right-hand rule: curl the fingers of your right hand from a\mathbf{a} toward b\mathbf{b}, and your thumb points along a×b\mathbf{a} \times \mathbf{b}.
  • ∥a×b∥=∥a∥ ∥b∥sin⁡θ\lVert \mathbf{a} \times \mathbf{b} \rVert = \lVert \mathbf{a} \rVert \, \lVert \mathbf{b} \rVert \sin\theta, the area of the parallelogram spanned by a\mathbf{a} and b\mathbf{b}. Half of it is the area of the triangle.
  • It is anti-commutative: b×a=−(a×b)\mathbf{b} \times \mathbf{a} = -(\mathbf{a} \times \mathbf{b}). Swapping the order flips the normal.
  • a×b=0\mathbf{a} \times \mathbf{b} = \mathbf{0} exactly when a\mathbf{a} and b\mathbf{b} are parallel.

Normals from triangles

A 3D mesh is a list of triangles. The normal of triangle (P,Q,R)(P, Q, R) is (Q−P)×(R−P)(Q - P) \times (R - P), normalised. Vertex order matters: counter-clockwise order (seen from outside) makes the normal point outward, and that is how engines decide which side of a triangle is the front for back-face culling. See Cross Product.

python · try it yourself
import numpy as np

P = np.array([0.0, 0.0, 0.0])
Q = np.array([4.0, 0.0, 0.0])
R = np.array([0.0, 3.0, 0.0])

n = np.cross(Q - P, R - P)
print("cross    :", n)
print("unit n   :", n / np.linalg.norm(n))
print("area     :", np.linalg.norm(n) / 2)
print("reversed :", np.cross(R - P, Q - P))
print("checks   :", np.dot(n, Q - P), np.dot(n, R - P))
cross    : [ 0.  0. 12.]
unit n   : [0. 0. 1.]
area     : 6.0
reversed : [  0.   0. -12.]
checks   : 0.0 0.0

The triangle is a 3-4-5 right triangle in the xy-plane, so its area is 12⋅3⋅4=6\frac{1}{2} \cdot 3 \cdot 4 = 6, and its normal points straight up the z-axis.

Exercises

Exercise 1: Perpendicular or not?

Which pairs are perpendicular? (a) ⟨1,2,3⟩\langle 1, 2, 3 \rangle and ⟨3,0,−1⟩\langle 3, 0, -1 \rangle, (b) ⟨2,−1⟩\langle 2, -1 \rangle and ⟨1,2⟩\langle 1, 2 \rangle, (c) ⟨1,1,1⟩\langle 1, 1, 1 \rangle and ⟨1,−2,2⟩\langle 1, -2, 2 \rangle.

Answer

(a) and (b) are perpendicular; (c) is not.

Solution
(a) 1⋅3+2⋅0+3⋅(−1)=3+0−3=0perpendicular(b) 2⋅1+(−1)⋅2=0perpendicular(c) 1−2+2=1≠0not perpendicular (angle slightly under 90∘)\begin{aligned} &\text{(a) } 1 \cdot 3 + 2 \cdot 0 + 3 \cdot (-1) = 3 + 0 - 3 = 0 && \text{perpendicular} \\ &\text{(b) } 2 \cdot 1 + (-1) \cdot 2 = 0 && \text{perpendicular} \\ &\text{(c) } 1 - 2 + 2 = 1 \ne 0 && \text{not perpendicular (angle slightly under } 90^\circ\text{)} \end{aligned}

Exercise 2: Angle between vectors

Find the angle between u=⟨1,0,1⟩\mathbf{u} = \langle 1, 0, 1 \rangle and v=⟨0,1,1⟩\mathbf{v} = \langle 0, 1, 1 \rangle.

Answer

60°.

Solution
u⋅v=0+0+1=1∥u∥=∥v∥=2cos⁡θ=12⋅2=12⇒θ=60∘\begin{aligned} \mathbf{u} \cdot \mathbf{v} &= 0 + 0 + 1 = 1 \\ \lVert \mathbf{u} \rVert = \lVert \mathbf{v} \rVert &= \sqrt{2} \\ \cos\theta &= \frac{1}{\sqrt{2} \cdot \sqrt{2}} = \frac{1}{2} \quad\Rightarrow\quad \theta = 60^\circ \end{aligned}

Exercise 3: Triangle area

Find the area of the triangle with corners A=(1,0,0)A = (1, 0, 0), B=(0,2,0)B = (0, 2, 0), C=(0,0,2)C = (0, 0, 2).

Answer

6≈2.449\sqrt{6} \approx 2.449.

Solution
AB→=⟨−1,2,0⟩,AC→=⟨−1,0,2⟩AB→×AC→=⟨2⋅2−0⋅0, 0⋅(−1)−(−1)⋅2, (−1)⋅0−2⋅(−1)⟩=⟨4,2,2⟩∥AB→×AC→∥=16+4+4=24=26area=12⋅26=6≈2.449\begin{aligned} \overrightarrow{AB} &= \langle -1, 2, 0 \rangle, \qquad \overrightarrow{AC} = \langle -1, 0, 2 \rangle \\ \overrightarrow{AB} \times \overrightarrow{AC} &= \langle 2 \cdot 2 - 0 \cdot 0,\ 0 \cdot (-1) - (-1) \cdot 2,\ (-1) \cdot 0 - 2 \cdot (-1) \rangle = \langle 4, 2, 2 \rangle \\ \lVert \overrightarrow{AB} \times \overrightarrow{AC} \rVert &= \sqrt{16 + 4 + 4} = \sqrt{24} = 2\sqrt{6} \\ \text{area} &= \tfrac{1}{2} \cdot 2\sqrt{6} = \sqrt{6} \approx 2.449 \end{aligned}
python · try it yourself
import numpy as np
A, B, C = np.array([1, 0, 0]), np.array([0, 2, 0]), np.array([0, 0, 2])
n = np.cross(B - A, C - A)
print(n, round(float(np.linalg.norm(n) / 2), 3))
[4 2 2] 2.449

Exercise 4: Is the target behind me? (code)

Write is_behind(forward, to_target) that returns True when the target lies more than 90° away from the forward direction. Test it with forward ⟨0,1⟩\langle 0, 1 \rangle and targets ⟨2,−1⟩\langle 2, -1 \rangle and ⟨−3,0.5⟩\langle -3, 0.5 \rangle.

Answer

⟨2,−1⟩\langle 2, -1 \rangle is behind (True); ⟨−3,0.5⟩\langle -3, 0.5 \rangle is not (False).

Solution

"More than 90°" means a negative dot product. No normalisation is needed because only the sign matters.

python · try it yourself
import numpy as np

def is_behind(forward, to_target):
    return bool(np.dot(forward, to_target) < 0)

f = np.array([0.0, 1.0])
print(is_behind(f, np.array([2.0, -1.0])), is_behind(f, np.array([-3.0, 0.5])))
True False

Test yourself

If a⋅b=0\mathbf{a} \cdot \mathbf{b} = 0 for two non-zero vectors, what is the angle between them?

  1. 0°
  2. 45°
  3. 90°
  4. 180°

What does ∥a×b∥\lVert \mathbf{a} \times \mathbf{b} \rVert measure?

  1. The angle between a\mathbf{a} and b\mathbf{b}
  2. The area of the parallelogram spanned by a\mathbf{a} and b\mathbf{b}
  3. The length of a\mathbf{a} plus b\mathbf{b}
  4. The projection of a\mathbf{a} on b\mathbf{b}

A triangle's normal is computed as (Q−P)×(R−P)(Q - P) \times (R - P). What happens if you list the vertices in the opposite order?

  1. Nothing changes
  2. The normal gets twice as long
  3. The normal points the opposite way
  4. The cross product becomes zero