The Cross Product
The cross product takes two vectors in 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 and :
A way to remember it is a formal determinant with the standard basis vectors in the first row:
Example: .
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 is perpendicular to and to .
Geometry: direction and length
- Direction: perpendicular to both and , oriented by the right-hand rule: point your right hand's fingers along , curl them towards , and your thumb points along . For example .
- Length: , which is the area of the parallelogram with sides and (base , height ). Half of it is the area of the triangle.
- So exactly when and are parallel ( or ) or one of them is .
Algebraic rules
| Rule | Formula |
|---|---|
| anti-commutative | |
| self | |
| distributive | |
| scalars | |
| not associative | in general |
| scalar triple product |
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
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 -component of , that is , is positive when lies to the left of the line from to . Convex-hull algorithms, polygon triangulation and segment-intersection tests are built on this one sign.
- Physics: torque and angular momentum are cross products. Camera code builds a "right" vector as .
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 and check that it is perpendicular to both factors.
Answer
.
Solution
Exercise 2: Basis vectors
Compute , and .
Answer
, and .
Solution
The cyclic order gives positive results; going against it flips the sign (anti-commutativity: ).
Exercise 3: Triangle area
Find the area of the triangle with corners , , .
Answer
.
Solution
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 , , using the scalar triple product.
Answer
Solution
Exercise 5: Convex or not? (code)
A polygon given counter-clockwise is convex when every consecutive triple of vertices turns left (). 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 ).
Solution
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 is true?
- It is a number
- It is perpendicular to both and
- It equals
- It is defined in every
What does measure?
- The angle between and
- The projection of on
- The area of the parallelogram spanned by and
- The volume of a cube
A mesh triangle's normal suddenly points inward after an import. What is the most likely cause?
- The vertices were scaled
- The vertex winding order was reversed
- The triangle has zero area
- The cross product is not associative