TExcelTAD
Home › Linear Algebra & Matrix Theory › The Cross Product

The Cross Product

The cross product takes two vectors in R3\mathbb{R}^3 and returns a third vector that is perpendicular to both, with a length equal to the area of the parallelogram they span. It only exists in three dimensions, and it is how 3D software computes surface normals, areas and orientations.

The definition

For u=(u1,u2,u3)\mathbf{u} = (u_1, u_2, u_3) and v=(v1,v2,v3)\mathbf{v} = (v_1, v_2, v_3):

u×v=(u2v3−u3v2, u3v1−u1v3, u1v2−u2v1)\mathbf{u} \times \mathbf{v} = (u_2v_3 - u_3v_2,\ u_3v_1 - u_1v_3,\ u_1v_2 - u_2v_1)

A way to remember it is a formal 3×33 \times 3 determinant with the standard basis vectors in the first row:

u×v=∣e1e2e3u1u2u3v1v2v3∣=e1(u2v3−u3v2)−e2(u1v3−u3v1)+e3(u1v2−u2v1)\mathbf{u} \times \mathbf{v} = \begin{vmatrix} \mathbf{e}_1 & \mathbf{e}_2 & \mathbf{e}_3 \\ u_1 & u_2 & u_3 \\ v_1 & v_2 & v_3 \end{vmatrix} = \mathbf{e}_1(u_2v_3 - u_3v_2) - \mathbf{e}_2(u_1v_3 - u_3v_1) + \mathbf{e}_3(u_1v_2 - u_2v_1)

Example: (1,2,3)×(4,5,6)=(2⋅6−3⋅5,3⋅4−1⋅6,1⋅5−2⋅4)=(−3,6,−3)(1, 2, 3) \times (4, 5, 6) = (2 \cdot 6 - 3 \cdot 5, 3 \cdot 4 - 1 \cdot 6, 1 \cdot 5 - 2 \cdot 4) = (-3, 6, -3).

python · try it yourself
import numpy as np
from sympy import Matrix

u, v = np.array([1, 2, 3]), np.array([4, 5, 6])
w = np.cross(u, v)
print("u x v =", w)
print("w . u =", np.dot(w, u), "  w . v =", np.dot(w, v))
print("v x u =", np.cross(v, u))

# SymPy does it exactly
print(Matrix([1, 2, 3]).cross(Matrix([4, 5, 6])).T)
u x v = [-3  6 -3]
w . u = 0   w . v = 0
v x u = [ 3 -6  3]
Matrix([[-3, 6, -3]])

The zero dot products confirm that u×v\mathbf{u} \times \mathbf{v} is perpendicular to u\mathbf{u} and to v\mathbf{v}.

Geometry: direction and length

  • Direction: perpendicular to both u\mathbf{u} and v\mathbf{v}, oriented by the right-hand rule: point your right hand's fingers along u\mathbf{u}, curl them towards v\mathbf{v}, and your thumb points along u×v\mathbf{u} \times \mathbf{v}. For example e1×e2=e3\mathbf{e}_1 \times \mathbf{e}_2 = \mathbf{e}_3.
  • Length: ∥u×v∥=∥u∥ ∥v∥sin⁡θ\lVert \mathbf{u} \times \mathbf{v} \rVert = \lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert \sin\theta, which is the area of the parallelogram with sides u\mathbf{u} and v\mathbf{v} (base ∥u∥\lVert \mathbf{u} \rVert, height ∥v∥sin⁡θ\lVert \mathbf{v} \rVert \sin\theta). Half of it is the area of the triangle.
  • So u×v=0\mathbf{u} \times \mathbf{v} = \mathbf{0} exactly when u\mathbf{u} and v\mathbf{v} are parallel (θ=0∘\theta = 0^\circ or 180∘180^\circ) or one of them is 0\mathbf{0}.

Algebraic rules

Rule Formula
anti-commutative v×u=−(u×v)\mathbf{v} \times \mathbf{u} = -(\mathbf{u} \times \mathbf{v})
self u×u=0\mathbf{u} \times \mathbf{u} = \mathbf{0}
distributive u×(v+w)=u×v+u×w\mathbf{u} \times (\mathbf{v} + \mathbf{w}) = \mathbf{u} \times \mathbf{v} + \mathbf{u} \times \mathbf{w}
scalars (c⋅u)×v=c⋅(u×v)(c \cdot \mathbf{u}) \times \mathbf{v} = c \cdot (\mathbf{u} \times \mathbf{v})
not associative (u×v)×w≠u×(v×w)(\mathbf{u} \times \mathbf{v}) \times \mathbf{w} \ne \mathbf{u} \times (\mathbf{v} \times \mathbf{w}) in general
scalar triple product u⋅(v×w)=det⁡[u v w]=±volume of the parallelepiped\mathbf{u} \cdot (\mathbf{v} \times \mathbf{w}) = \det[\mathbf{u}\ \mathbf{v}\ \mathbf{w}] = \pm \text{volume of the parallelepiped}

The order matters: swapping the factors flips the result. In 3D graphics this is the reason a triangle's winding order (clockwise or counter-clockwise vertex order) decides which way its normal points.

In software: normals, areas and orientation

python · try it yourself
import numpy as np

def triangle_normal(A, B, C):
    n = np.cross(B - A, C - A)
    return n / np.linalg.norm(n)

def triangle_area(A, B, C):
    return 0.5 * np.linalg.norm(np.cross(B - A, C - A))

A = np.array([0.0, 0.0, 0.0])
B = np.array([2.0, 0.0, 0.0])
C = np.array([0.0, 3.0, 0.0])
print("normal (A,B,C):", triangle_normal(A, B, C))
print("normal (A,C,B):", triangle_normal(A, C, B))
print("area:", triangle_area(A, B, C))

# a tilted triangle: normal and lighting with the dot product
P, Q, R = np.array([0.0, 0, 0]), np.array([1.0, 0, 1]), np.array([0.0, 1, 0])
n = triangle_normal(P, Q, R)
light = np.array([0.0, 0.0, 1.0])
print("tilted normal:", np.round(n, 4), " brightness:", round(float(max(0, n @ light)), 4))
normal (A,B,C): [0. 0. 1.]
normal (A,C,B): [ 0.  0. -1.]
area: 3.0
tilted normal: [-0.7071  0.      0.7071]  brightness: 0.7071
  • Surface normals: every lit triangle needs a normal for the dot-product lighting formula, and the cross product of two edges gives it. Counter-clockwise vertices (seen from the front) give a normal pointing towards the viewer, which is also what back-face culling tests.
  • Areas: mesh surface area, and the weights of area-weighted vertex normals, are sums of half cross-product lengths.
  • Orientation tests in 2D: the zz-component of (B−A)×(C−A)(B - A) \times (C - A), that is (b1−a1)(c2−a2)−(b2−a2)(c1−a1)(b_1 - a_1)(c_2 - a_2) - (b_2 - a_2)(c_1 - a_1), is positive when CC lies to the left of the line from AA to BB. Convex-hull algorithms, polygon triangulation and segment-intersection tests are built on this one sign.
  • Physics: torque τ=r×F\boldsymbol{\tau} = \mathbf{r} \times \mathbf{F} and angular momentum L=r×p\mathbf{L} = \mathbf{r} \times \mathbf{p} are cross products. Camera code builds a "right" vector as forward×up\text{forward} \times \text{up}.
python · try it yourself
def orient(a, b, c):
    """> 0: c is left of a->b, < 0: right, 0: collinear."""
    return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])

a, b = (0, 0), (4, 1)
for c in [(1, 3), (3, -2), (8, 2)]:
    s = orient(a, b, c)
    print(c, s, "left" if s > 0 else "right" if s < 0 else "on the line")
(1, 3) 11 left
(3, -2) -11 right
(8, 2) 0 on the line

Pure integer arithmetic, no square roots and no angles: that is why geometry libraries prefer this test. See also Game Math: Cross Product, Dot and Cross Products and The Cross Product (in depth).

Exercises

Exercise 1: Compute

Compute (2,0,−1)×(1,3,2)(2, 0, -1) \times (1, 3, 2) and check that it is perpendicular to both factors.

Answer

(3,−5,6)(3, -5, 6).

Solution
(0⋅2−(−1)⋅3, (−1)⋅1−2⋅2, 2⋅3−0⋅1)=(3,−5,6)(3,−5,6)⋅(2,0,−1)=6−6=0 ✓(3,−5,6)⋅(1,3,2)=3−15+12=0 ✓\begin{aligned} &(0 \cdot 2 - (-1) \cdot 3,\ (-1) \cdot 1 - 2 \cdot 2,\ 2 \cdot 3 - 0 \cdot 1) = (3, -5, 6) \\ &(3, -5, 6) \cdot (2, 0, -1) = 6 - 6 = 0 \ \checkmark \\ &(3, -5, 6) \cdot (1, 3, 2) = 3 - 15 + 12 = 0 \ \checkmark \end{aligned}

Exercise 2: Basis vectors

Compute e2×e3\mathbf{e}_2 \times \mathbf{e}_3, e3×e1\mathbf{e}_3 \times \mathbf{e}_1 and e2×e1\mathbf{e}_2 \times \mathbf{e}_1.

Answer

e1\mathbf{e}_1, e2\mathbf{e}_2 and −e3-\mathbf{e}_3.

Solution

The cyclic order e1→e2→e3→e1\mathbf{e}_1 \to \mathbf{e}_2 \to \mathbf{e}_3 \to \mathbf{e}_1 gives positive results; going against it flips the sign (anti-commutativity: e2×e1=−(e1×e2)=−e3\mathbf{e}_2 \times \mathbf{e}_1 = -(\mathbf{e}_1 \times \mathbf{e}_2) = -\mathbf{e}_3).

Exercise 3: Triangle area

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

Answer

19≈4.359\sqrt{19} \approx 4.359.

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

Exercise 4: Volume of a box

Find the volume of the parallelepiped spanned by u=(1,0,0)\mathbf{u} = (1, 0, 0), v=(1,2,0)\mathbf{v} = (1, 2, 0), w=(0,1,3)\mathbf{w} = (0, 1, 3) using the scalar triple product.

Answer
Solution
v×w=(2⋅3−0⋅1, 0⋅0−1⋅3, 1⋅1−2⋅0)=(6,−3,1)u⋅(v×w)=6\begin{aligned} \mathbf{v} \times \mathbf{w} &= (2 \cdot 3 - 0 \cdot 1,\ 0 \cdot 0 - 1 \cdot 3,\ 1 \cdot 1 - 2 \cdot 0) = (6, -3, 1) \\ \mathbf{u} \cdot (\mathbf{v} \times \mathbf{w}) &= 6 \end{aligned}

Exercise 5: Convex or not? (code)

A polygon given counter-clockwise is convex when every consecutive triple of vertices turns left (orient>0\text{orient} > 0). Test the polygons [(0,0), (4,0), (4,3), (0,3)] and [(0,0), (4,0), (2,1), (4,3), (0,3)].

Answer

The rectangle is convex; the second polygon is not (it turns right at (2,1)(2, 1)).

Solution
python · try it yourself
def orient(a, b, c):
    return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])

def is_convex(poly):
    n = len(poly)
    return all(orient(poly[i], poly[(i + 1) % n], poly[(i + 2) % n]) > 0 for i in range(n))

print(is_convex([(0, 0), (4, 0), (4, 3), (0, 3)]))
print(is_convex([(0, 0), (4, 0), (2, 1), (4, 3), (0, 3)]))
True
False

Test yourself

Which statement about u×v\mathbf{u} \times \mathbf{v} is true?

  1. It is a number
  2. It is perpendicular to both u\mathbf{u} and v\mathbf{v}
  3. It equals v×u\mathbf{v} \times \mathbf{u}
  4. It is defined in every Rn\mathbb{R}^n

What does ∥u×v∥\lVert \mathbf{u} \times \mathbf{v} \rVert measure?

  1. The angle between u\mathbf{u} and v\mathbf{v}
  2. The projection of u\mathbf{u} on v\mathbf{v}
  3. The area of the parallelogram spanned by u\mathbf{u} and v\mathbf{v}
  4. The volume of a cube

A mesh triangle's normal suddenly points inward after an import. What is the most likely cause?

  1. The vertices were scaled
  2. The vertex winding order was reversed
  3. The triangle has zero area
  4. The cross product is not associative