Affine Transformations Lab

Linear, plus a shift

The Matrices lab's transformations stretch, rotate and shear space, but the origin never moves. Add one more ingredient — a translation — and you get the affine maps: the transformations of computer graphics, robotics and image processing, and the Wx + b inside every neural-network layer. One trick, homogeneous coordinates, turns all of them back into a single matrix multiply, and that is where both the elegance and the speed come from.

Scale, shear, rotate and translate the letter F. The four steps are multiplied together into one 3×3 homogeneous matrix, shown live: its top-left 2×2 block (teal) is the linear part, its third column (orange) is the translation, and its bottom row is always 0 0 1. The faint teal grid is where the whole coordinate grid goes — straight lines stay straight and parallel lines stay parallel, whatever you do.

Transform explorer

Dashed outline: the original F. Yellow: its image. Orange: where the origin goes. Pink: a segment and its midpoint.

Order
Homogeneous matrix M
linear part translation always 0 0 1
Kind of map
—
Area scale (det)
—
Preserves
—
Cost of applying this pipeline to 1,000,000 points
Step by step—
One composed matrix—

Experiments to try

Order matters

Rotate by 90° and translate x by 3. Switch between "translate last" and "translate first": the F lands in different places, because a shift that happens before the rotation gets rotated too.

Break the shape, keep the lines

Shear hard and scale x and y differently. Lengths and angles change, the area changes by det, yet the grid stays a grid of parallel lines and the midpoint stays the midpoint.

Watch the cost panel

Turn on one step, then all four. Step by step, every step is one more pass over a million points; composed, it is always one matrix and one pass.

Most people who use affine transforms every day, graphics and CNC programmers included, carry a picture of them that is almost right. This page sorts out the four places where it usually slips: what an affine space is, how an affine map differs from the matrix that stores it, where the extra 1 really goes, and what any of it buys a neural network. Start with the panel: it shows the one idea the rest depends on.

Move the datum

Drag P, Q or the orange datum, and turn the datum with the slider. The table gives each quantity's homogeneous coordinates, measured from the datum.

Measured from the datum

1. An affine space has no origin

It's tempting to think of an affine space as just a set of points in some number of dimensions. What makes it affine is which operations are allowed on those points, and the panel shows the rule. The datum is an arbitrary choice, so anything that changes when you move it isn't a real place.

OperationAllowed?Result
point − pointyesa vector: a displacement, like a feed move
point + vectoryesa point
point + pointnodepends on the datum, so it means nothing
weighted average of points, weights summing to 1yesa point (a midpoint, a centroid)

A shop floor is a good picture of one. A position means something only relative to a chosen datum, such as a work offset. Move the datum and every coordinate changes, but the part hasn't moved. That's why people describe an affine space as a vector space that has forgotten where its origin is. The weighted average survives because its weights sum to 1: move the datum by d and the average moves by exactly d too, so it lands on the same spot on the floor.

2. The map is what; homogeneous coordinates are how

An affine map is a function from one affine space to another that keeps straight lines straight, parallel lines parallel, and ratios along a line (so midpoints stay midpoints). In coordinates it is always $x \mapsto Ax + t$. That's a statement about what the transformation does.

Homogeneous coordinates are a way of writing it down so that every affine map becomes a single matrix and a chain of them becomes a single product. The two get blurred because they travel together in graphics, but they're separate ideas. You can use affine maps without homogeneous coordinates, keeping $A$ and $t$ apart, which is exactly what neural networks do. And homogeneous coordinates can hold more than affine maps, as section 4 shows. So "I use affine transforms to keep all my operations in one matrix" is really a sentence about homogeneous coordinates: a very good reason to use them, just a different thing from what makes a map affine.

3. Where the 1 goes

The 1 goes on the point, not in the matrix: $(x, y)$ becomes $(x, y, 1)$. The matrix gains a third column, holding the shift $t$, and a third row, $0\ 0\ 1$: $$\begin{pmatrix}x'\\y'\\1\end{pmatrix} = \begin{pmatrix}a&b&t_x\\c&d&t_y\\0&0&1\end{pmatrix}\begin{pmatrix}x\\y\\1\end{pmatrix}$$ That puts every point on the flat plane $w = 1$ inside an ordinary 3-D vector space, and the rules of section 1 then enforce themselves through the last coordinate, exactly as the panel's table shows:

last coordinate, wmeaning
a point1gets shifted by $t$
a vector0ignores $t$
point − point1 − 1 = 0a vector
point + vector1 + 0 = 1a point
point + point1 + 1 = 2off the plane: not a point

So direction vectors (surface normals, a tool's axis, a feed direction) are carried with $w = 0$. A translation leaves them alone, but a rotation still turns them. Turn the datum in the panel and you'll see it: Q − P's numbers change when the datum turns, never when it slides.

4. The bottom row is the promise

The row $0\ 0\ 1$ is what makes the matrix affine: it guarantees that $w = 1$ comes out whenever $w = 1$ goes in, so points stay points. Change that row and you have a projective (perspective) transform. Now $w$ comes out as something else, and you divide by it to get back to the plane, which is the divide every 3-D graphics pipeline does. Perspective still keeps straight lines straight, but it no longer keeps ratios along them, so a midpoint doesn't stay a midpoint (the reason texture mapping needs "perspective-correct" interpolation).

Kind of mapMatrixKeeps
Linear2×2the origin, lines, ratios
Affine3×3 with bottom row $0\ 0\ 1$lines, parallels, ratios
Projectiveany 3×3, then divide by $w$lines only

In one line: an affine space is the flat slice $w = 1$ of a vector space one dimension bigger, and an affine map is a linear map of that bigger space that leaves the slice in place.

5. Two everyday consequences

  • Rotating about a point that isn't the origin. Move the centre $c$ to the origin, rotate, move it back: $T(c)\,R\,T(-c)$. Three matrices, composed once into one. The Transform explorer's order buttons show what goes wrong when the shift lands on the wrong side of the rotation.
  • A linearized path survives exactly. Because an affine map keeps straight lines straight and ratios along them, transforming a path made of straight segments only means transforming its vertices. Every point in between lands exactly where interpolating the new vertices puts it. Under perspective that is no longer true: the segments stay straight, but the spacing along them changes.

6. What carries over to neural networks, and what doesn't

Every network layer computes $Wx + b$ before its activation: an affine map, exactly as above. But networks don't use the one-matrix trick. Collapsing a chain only works when nothing sits between the maps, and a network puts an activation (a "fold") between every pair of layers, so there is no chain to collapse. The code keeps $W$ and $b$ apart, and on a GPU adding $b$ is merged into the multiplication at almost no cost. Many recent language models go further and drop most of their biases altogether: the model behind this site's live labs keeps them only in part of its attention. The Layers station shows what's inside a real layer and why the missing shifts aren't missed.

A single neuron computes w·x + b and fires (through a ReLU) on one side of the line where that is zero. The weights w turn the line; the bias b slides it. Two clusters sit in the same direction from the origin — try to separate them with the bias switched off, then on.

Layer explorer

Shading shows how strongly the neuron fires (its ReLU output). Ringed points are on the wrong side.

Bias
Correct now
—
Best possible, no bias
—
Best possible, with bias
—

What to look for

With the bias off, the line can only spin about the origin, and both clusters lie the same way from it: no angle splits them. Switch the bias on, set the direction near 45°, and slide b negative until the line falls between the clusters. The equation line shows the homogeneous view of the same neuron: b is just a third weight, multiplied by an input that is always 1 — the same trick that put the translation into the 3×3 matrix. (The "best possible" figures come from simply trying every angle and offset, not from training.)

An affine map is a linear map followed by a shift. Homogeneous coordinates write it as a single matrix, which is what makes it cheap to compose, cheap to apply in bulk, and the natural form for graphics hardware — and it is exactly what each layer of a neural network computes before its activation.

1. Linear versus affine

A linear map is $f(x) = Ax$: it can stretch, rotate and shear, but $f(0)=0$ always. An affine map adds a translation, $f(x) = Ax + t$. Translation is not linear — $f(x+y) \ne f(x)+f(y)$ once $t\ne0$ — so no 2×2 matrix can move the origin. That is the gap between the Matrices lab and the real transformations of graphics and machine learning.

2. The homogeneous trick

Give every point a third coordinate that is always 1, $(x, y) \to (x, y, 1)$. Then the affine map becomes one matrix product: $$\begin{pmatrix}x'\\y'\\1\end{pmatrix} = \begin{pmatrix}a&b&t_x\\c&d&t_y\\0&0&1\end{pmatrix}\begin{pmatrix}x\\y\\1\end{pmatrix}$$ The 2×2 block is the linear part, the last column is the translation, and the bottom row keeps the 1 in place (the Points & vectors tab explains why the 1 belongs to the point, and what a 0 there means). In 3-D the same idea gives the $4\times4$ matrices used by every graphics engine; a rigid motion there is a rotation from the Unit Sphere & Rotations lab plus a translation column.

3. What survives an affine map

Straight lines stay straight, parallel lines stay parallel, and ratios along a line are kept — so midpoints stay midpoints and centroids stay centroids. Lengths and angles are not kept in general, and every area is multiplied by $\det A$. The family nests: a rigid motion (rotation + translation) keeps lengths and angles; a similarity adds uniform scaling and keeps angles; a general affine map adds shear and uneven scaling and keeps only lines and ratios.

4. Why homogeneous coordinates are fast

The performance case is the main reason the whole graphics and robotics world works in homogeneous coordinates. It comes from five places:

  • Compose once, apply once. A chain of $k$ transforms multiplies into one matrix before any point is touched, $M = M_k \cdots M_2 M_1$. Each point then costs one matrix–vector product — 4 multiplies and 4 adds in 2-D, 9 and 9 in 3-D — no matter how long the chain is. Applied step by step, the cost per point grows with $k$. A typical 3-D scene chains object → parent → world → camera → screen; for a million vertices, composing those five transforms first does one product per vertex instead of five, while composing the matrices themselves takes a few hundred operations in total.
  • One pass over memory. Step by step, every stage reads and writes every point: $k$ full passes over the data. Composed, it is one pass. For large point sets, moving data costs more than the arithmetic, so this is often the larger saving.
  • Batching becomes matrix multiplication. Stack all $N$ points as the columns of a $3\times N$ matrix $P$ and the whole job is the single product $MP$. Matrix multiplication is the most heavily optimized operation in computing (BLAS libraries, SIMD instructions, GPU tensor cores), so this runs close to the hardware's peak speed.
  • One uniform code path. Rotation, scaling, shear and translation all become the same operation, so there is no branching on "what kind of transform is this?". GPUs are built around $4\times4$ matrix multiplies for exactly this reason, and undo, inverses and camera moves are all matrix operations too.
  • Perspective comes free. Let the bottom row be something other than $0\ 0\ 1$ and divide by the last coordinate afterward: the same $4\times4$ machinery now does perspective projection, so a 3-D renderer's entire vertex pipeline is matrix multiplies plus one divide.

The honest limits: for a single transform applied once, the homogeneous form is not faster — it can cost a couple of extra additions, and well-written code skips multiplying by the constant bottom row. The gains come from composition, batching and uniformity, which is why they are largest in exactly the places that use them most: renderers, robot kinematics and point-cloud processing. In neural networks, absorbing the bias into the weight matrix is mostly bookkeeping, since libraries fuse the bias add into the multiply anyway.

5. Why a layer needs a bias, and why big models drop most of them

A neural-network layer computes $Wx + b$ and then applies an activation such as ReLU. Without $b$ the layer is linear, and each neuron's boundary $w\cdot x = 0$ must pass through the origin — the Layer explorer shows how badly that can fail. The bias lets each boundary sit wherever the data needs it. In homogeneous form, $w\cdot x + b = [w,\ b]\cdot[x,\ 1]$: the bias is simply one more weight on a constant input of 1.

Large language models are a notable exception. Many recent ones keep only a few biases (the model behind this site's live labs has them only on part of its attention) because each layer adds its output to a running total that is already far from the origin, so the boundaries can sit where they need to without one.

6. Affine combinations

A weighted average whose weights sum to 1, $\sum_i \lambda_i p_i$ with $\sum_i \lambda_i = 1$, is called an affine combination, and affine maps preserve it: transform the points and the average moves with them. That is why centroids and midpoints survive, why barycentric coordinates work inside triangles, and it describes the output of attention: softmax weights are positive and sum to 1, so each output is a weighted average of the value vectors.

Self check

1. Which of these can a 2×2 matrix not do to the plane?

2. After any affine map, the midpoint of a segment lands…

3. The main reason homogeneous coordinates speed up a chain of k transforms applied to many points is that…

4. A neuron with no bias has a decision boundary that…

5. In homogeneous coordinates P = (2, 1, 1) and Q = (4, 5, 1). What is Q − P?

6. You move the datum (the origin). Which of these changes where it is on the floor?

Answer every question before checking.