Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 62 ยท Linear Transformations

Homogeneous Coordinates and Translation

Add one extra coordinate so translation becomes a matrix product, build 3x3 affine matrices for 2D, chain them, and see the idea behind graphics pipelines.

Every transformation in the last two lessons had a catch: it kept the origin fixed. That is not enough for real work. Moving a character across a screen, placing a camera or sliding an object into position all need a translation, a shift by a fixed vector. Translation is not linear, because it moves the origin, so no 2x2 matrix can do it.

The fix is a clever trick called homogeneous coordinates: describe a 2D point (x, y) with three numbers (x, y, 1). In that extended space, a translation becomes an ordinary matrix product, and it chains with rotations and scalings by plain multiplication. The same idea, with four coordinates, runs the pipelines of every 3D engine.

The homogeneous trick

Point Write (x, y) as (x, y, 1).
Matrix Use a 3 by 3 matrix instead of 2 by 2.
Translation Now a shift is a matrix product.

One extra coordinate buys translation.

1 / 2

Why translation is not linear

A translation by (tx, ty) sends (x, y) to (x + tx, y + ty). The origin goes to (tx, ty), so the map fails T(0) = 0. A linear map A v always fixes the origin, so there is no matrix A of size 2x2 for it. Maps of the form v -> A v + t (a linear map followed by a shift) are called affine maps. They keep straight lines straight and parallel lines parallel, but they do not have to fix the origin.

The extra coordinate

Place the 2D plane inside 3D at height 1. The point (x, y) becomes the column (x, y, 1). Now a 3x3 matrix of the form

TEXT
T = [ 1  0  tx ]
    [ 0  1  ty ]
    [ 0  0   1 ]

computes T @ (x, y, 1) = (x + tx, y + ty, 1). The shift comes from the third column multiplied by the constant 1. The last row (0, 0, 1) keeps the extra coordinate equal to 1 so the output is again a valid point.

A general affine matrix in 2D looks like

TEXT
M = [ a  b  tx ]
    [ c  d  ty ]
    [ 0  0   1 ]

where the top-left 2x2 block [[a, b], [c, d]] is the linear part (rotation, scaling, shear, reflection) and the right-hand column (tx, ty) is the translation. A pure linear map is the special case tx = ty = 0, so everything you learned in Rotation, Scaling, Shear and Reflection carries over by embedding the 2x2 matrix into the top-left corner of a 3x3 identity matrix.

Python Translation as a matrix product

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The conversion helpers make the pattern clear: append a row of ones going in, drop the extra row coming out. The translated square is the unit square shifted three right and two up.

Chaining: scale, rotate, translate in one matrix

Because everything is now a 3x3 matrix, a sequence of steps collapses into a single product, with the first step on the right (exactly as before). To rotate a shape about its own center (cx, cy) instead of about the origin, you need three steps:

  1. translate by (-cx, -cy) so the center sits at the origin,
  2. rotate,
  3. translate back by (cx, cy).

As matrices, the combined transformation is M = T(cx, cy) @ R @ T(-cx, -cy).

Python Rotating a square about its own center

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The red dashed square swings away from the picture because rotating about the origin throws it around a big circle. The green square spins in place. The combined matrix has the rotation in its upper-left block and a computed translation in its last column, which is exactly the shift needed to keep the center fixed. A single 3x3 product replaced three separate steps.

The last row and points at infinity

All the matrices so far end with the row (0, 0, 1), and the third coordinate of every point stays 1. In general, homogeneous coordinates allow any nonzero third value w: the triple (x, y, w) represents the point (x / w, y / w), so (2, 4, 2) and (1, 2, 1) are the same point. The helper from_homogeneous above already divides by w.

This matters for two reasons.

  • Directions versus points. A vector with w = 0, like (1, 0, 0), is a direction, not a place. Translation does not move it, which is the right behaviour: the direction "east" does not change when you slide a map.
  • Perspective. If the last row of the matrix is not (0, 0, 1), the output w varies with the input, and dividing by it shrinks far objects. That is the mathematical core of perspective projection in 3D graphics.
Python Points, directions and the divide by w

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The translation moves the point from (1, 1) to (6, -1) but leaves the direction exactly as it was. The last loop shows the perspective effect in miniature: as y grows, w grows, and dividing by it pulls the x-coordinate in from 2 to 1.

The graphics pipeline idea

A 3D engine uses the same pattern with 4x4 matrices acting on (x, y, z, 1). Each vertex of a model passes through a chain:

flowchart LR
    A["Model coordinates"] --> B["World coordinates<br/>(model matrix)"]
    B --> C["Camera coordinates<br/>(view matrix)"]
    C --> D["Clip coordinates<br/>(projection matrix)"]
    D --> E["Screen pixels<br/>(divide by w)"]

Each arrow is a matrix product; the final step is the divide by w.

The matrices on the way multiply into a single one that is applied to millions of vertices in parallel by the graphics card. You will build a small version of this chain in Project: A Tiny 3D Wireframe Renderer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can no 2 by 2 matrix translate points in the plane?

    A matrix times the zero vector is always zero, so the origin cannot move. A translation sends the origin to (tx, ty), so it is not linear.
  2. 2. What does the 3 by 3 translation matrix [[1,0,4],[0,1,-1],[0,0,1]] do to the homogeneous point (2, 3, 1)?

    The result is (2 + 4, 3 - 1, 1) = (6, 2, 1). The third column is added once because the last coordinate is 1.
  3. 3. To rotate a shape about its center (cx, cy), what is the correct order of matrices (left to right in the product)?

    The rightmost matrix acts first: move the center to the origin with T(-c), rotate, then move back with T(c). Written as a product that is T(c) @ R @ T(-c).
  4. 4. What does the homogeneous triple (6, 9, 3) represent?

    Divide the first two entries by the third: (6 / 3, 9 / 3) = (2, 3).

Summary

  • Translation is not linear because it moves the origin; the maps v -> A v + t are called affine.
  • Homogeneous coordinates append a 1: (x, y) becomes (x, y, 1), and an affine map becomes a 3x3 matrix with last row (0, 0, 1).
  • The top-left 2x2 block is the linear part; the last column holds the translation.
  • Chains of transformations multiply into one matrix, first step on the right; rotating about a center is T(c) @ R @ T(-c).
  • A triple (x, y, w) means the point (x/w, y/w); w = 0 is a direction, and a changing w produces perspective.

Next, explore how the same vector looks in different coordinate systems in Change of Basis.