Introduction#
The dot product turned two vectors into a single number. The cross product turns two vectors into a new vector: one that points at right angles to both of them. In 3D graphics that’s exactly what you need to work out which way a triangle faces, and it turns up in a surprising number of other places too. This post covers:
- What the cross product gives you, and the right-hand rule for which way it points
- What its length means (an area!)
- The simple 2D version, which tells you which side of a line a point is on
- Where graphics uses both
Two Arrows In, One Arrow Out#
Take two arrows, a and b, that start at the same point. Unless they’re parallel, they lie in a flat surface together, like two pencils lying on a table. There’s exactly one direction that’s at right angles to both: straight up out of the table (or straight down into it). The cross product, written a × b, gives you an arrow in that direction.
Drag the scene to look around it in 3D, and change the arrows with the sliders.
Two vectors (orange and purple) and their cross product (blue)
Things to try:
- Turn the scene so you’re looking straight down the blue arrow: the orange and purple arrows are both flat across your view, at right angles to it.
- Set the angle to 0° or 180°. The arrows are parallel, they don’t define a flat surface any more, and the cross product shrinks to nothing.
- Press Swap the order. b × a points the opposite way to a × b. Unlike ordinary multiplication, the order matters.
Which way does it point? The right-hand rule#
“At right angles to both” leaves two choices: up or down. The convention is the right-hand rule: point the fingers of your right hand along a, curl them towards b, and your thumb points along a × b. Swap a and b and you have to curl the other way, so your thumb flips over. (In a left-handed coordinate system, as Matrix Conventions explains, the arrow still comes out of the same calculation, but it looks as if it follows your left hand instead.)
Its length is an area#
The two arrows make a slanted rectangle called a parallelogram (shown in green). The cross product’s length equals that shape’s area. When the arrows are at right angles it’s as big as it can be; as they swing towards each other the parallelogram squashes flat and the area, and the arrow, shrink to zero. Half of it is the area of the triangle the two arrows make, which is useful in its own right.
The calculation#
For completeness, here’s how it’s worked out from the numbers. You don’t need to memorise it; every maths library and shading language has it built in (cross in GLSL):
Each part uses the other two axes: the x part only involves y and z, and so on. Six multiplications and three subtractions.
In 2D: Which Side of the Line?#
In 2D there’s no third direction to point in, but the last part of that formula, axby − aybx, is still very useful on its own. Its sign tells you which way you turn to get from a to b: positive for anticlockwise (to the left), negative for clockwise (to the right), zero if they’re in a straight line.
That means it can tell you which side of a line a point is on. Take the line’s direction as one vector and the direction from the line’s start to the point as the other. Drag the edge’s ends and the point below.
The 2D cross product: which side of an edge is a point on?
This little test is the edge function that graphics cards use billions of times a second to decide which pixels a triangle covers: a pixel is inside if it’s on the inner side of all three edges (Rasterisation). Test the third corner of a triangle against its first edge and you get the triangle’s winding order, which decides whether it faces you (Vertices, Triangles and Meshes).
Graphics cards don’t have a special cross product instruction. The compiler turns cross into a handful of multiply-and-subtract steps, which is cheap enough to do per pixel. The 2D version, though, is effectively built into the hardware: the rasteriser evaluates three of them, one per triangle edge, for every pixel it considers. Because the answer changes by a fixed amount from one pixel to the next, the hardware can step across the screen with just additions.
Where You’ll Use This#
- Surface normals: the cross product of two edges of a triangle points straight out of it. Normalise it (vectors) and you have the triangle’s normal, ready for lighting. The flat shading trick does exactly this, per pixel.
- Camera axes: a “look-at” camera finds its right and up directions with two cross products (Cameras).
- Tangent space: the third axis of a normal map’s frame comes from a cross product (Normal Mapping).
- Inside or outside, and winding: the 2D version decides which pixels a triangle covers and which way it faces.
- Physics: spinning and turning forces (torque) are cross products.
Questions You Might Have#
Is there a cross product in 2D or 4D?
The arrow-producing cross product is special to 3D (a similar one exists only in 7D). In 2D, the version shown in this post gives a single number instead, which is still very useful for telling left from right.
Summary#
- The cross product a × b is a vector at right angles to both a and b.
- The right-hand rule gives its direction; swapping a and b flips it.
- Its length is the area of the parallelogram a and b make (zero if they’re parallel).
- In 2D, axby − aybx tells you which side of a line a point is on.
Further Reading#
- 3Blue1Brown, Cross products: an animated explanation of why the length is an area.
- Maths Is Fun, Cross Product: worked examples and the right-hand rule.
- Fletcher Dunn and Ian Parberry, 3D Math Primer: Vector Multiplication: cross products in games, in depth.