Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 50 ยท 2D Transformations

Homogeneous Coordinates and Matrix Transformations

Learn why graphics uses homogeneous coordinates, how 3x3 matrices represent translation, scaling and rotation, and how to compose them in order.

In the previous lesson you met three formulas. Scaling and rotation multiply the coordinates, but translation adds an offset. That difference is annoying. If you want to rotate, then scale, then move a polygon with 10,000 vertices, you would run three separate formulas on every vertex, and you could not easily describe the whole chain as one object, store it, reverse it or hand it to a GPU.

Matrices solve this. A matrix is a table of numbers that represents a transformation, and multiplying matrices combines transformations into a single matrix. To fit translation into the same scheme, we add one extra coordinate to every point. This trick is called homogeneous coordinates, and it is used in every graphics library you will ever touch.

Matrices in 2D

Write a point as a column of two numbers and apply a 2 by 2 matrix by multiplying:

TEXT
| x' |   | a  b |   | x |
| y' | = | c  d | * | y |      which means  x' = a*x + b*y,  y' = c*x + d*y

Rotation and scaling fit this shape perfectly:

TEXT
Scaling                 Rotation by ฮธ
| sx  0 |               | cos ฮธ  -sin ฮธ |
|  0 sy |               | sin ฮธ   cos ฮธ |

Multiplying the vector by these matrices gives exactly the formulas from the last lesson. But translation wants x' = x + tx, an addition with a constant that does not depend on x or y. No 2 by 2 matrix can produce that: every term a 2 by 2 matrix produces is proportional to x or y. In particular the origin would always map to the origin.

Homogeneous coordinates

The fix is to give each 2D point a third coordinate, w, and write it as (x, y, 1). For ordinary points w is always 1. Now a 3 by 3 matrix can include the offsets in its last column:

TEXT
| x' |   | 1  0  tx |   | x |
| y' | = | 0  1  ty | * | y |      x' = x + tx,  y' = y + ty,  w stays 1
| 1  |   | 0  0  1  |   | 1 |

Multiply the first row by the point: 1*x + 0*y + tx*1 = x + tx. The constant 1 in the third slot is what lets a multiplication produce an addition. All three basic transformations are now 3 by 3 matrices:

TEXT
Translation            Scaling                Rotation
| 1  0  tx |           | sx  0   0 |          | cos ฮธ  -sin ฮธ  0 |
| 0  1  ty |           | 0   sy  0 |          | sin ฮธ   cos ฮธ  0 |
| 0  0  1  |           | 0   0   1 |          |   0       0    1 |

The bottom row is 0 0 1 for all of them, so w stays 1. These are called affine transformations: they keep straight lines straight and parallel lines parallel.

There is a deeper meaning too. In general, the homogeneous point (x, y, w) stands for the 2D point (x/w, y/w). So (4, 2, 2) and (2, 1, 1) are the same point. When w = 0 the "point" is a direction at infinity, which is why translation (which depends on w = 1) moves points but does not change directions. In 3D graphics the perspective divide uses exactly this idea, as you will see in orthographic and perspective projection.

flowchart LR
  A["2D point<br/>(x, y)"] -->|"append w = 1"| B["Homogeneous<br/>(x, y, 1)"]
  B -->|"multiply by 3x3 matrix"| C["(x', y', w')"]
  C -->|"divide by w'"| D["2D point<br/>(x'/w', y'/w')"]

Every transformation follows this round trip. For affine matrices w' is 1, so the divide changes nothing.

Composition and order

Suppose you apply matrix A first and then matrix B to a point p. With column vectors you write:

TEXT
p' = B * (A * p) = (B * A) * p

The matrix that acts first is the rightmost one next to the point, and you read the product from right to left. We can multiply B * A once into a single matrix M and then use M on all 10,000 vertices. Matrix multiplication is associative, so grouping does not matter, but it is not commutative, so B * A is generally different from A * B. This is the matrix form of "order matters".

A quick check with numbers. Let T move by (5, 0) and R rotate by 90ยฐ. The product T * R means "rotate first, then translate". The product R * T means "translate first, then rotate". Multiplying them out yields two different matrices, with the same rotation part but different last columns: (5, 0) versus (0, 5). These are the same positions we found in the last lesson.

Many libraries use row vectors and write the matrices transposed, so the order reads left to right (p * A * B). Both are correct. Just keep to one convention. This tutorial uses column vectors, as OpenGL and most mathematics textbooks do.

Pivot-point rotation as one matrix

To rotate by ฮธ around a pivot (px, py) you move the pivot to the origin, rotate, and move back. In matrix form, read right to left:

TEXT
M = T(px, py) * R(ฮธ) * T(-px, -py)

The same pattern gives scaling about a fixed point: T(fx, fy) * S(sx, sy) * T(-fx, -fy). Once multiplied, M is just one 3 by 3 matrix. You never need to remember the long formulas for pivot rotation: build them from the three parts.

The last column of M for a pivot rotation works out to (px - px cos ฮธ + py sin ฮธ, py - px sin ฮธ - py cos ฮธ). You can verify that the pivot is a fixed point: multiplying M by (px, py, 1) returns (px, py, 1).

A matrix toolkit in code

Here is the toolkit used in the rest of the tutorial, written for 3 by 3 matrices stored as arrays of 9 numbers in row-major order. The console example below builds matrices, multiplies them in both orders and applies the result to a point.

JS Matrices in the console

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

No output.

Read the output: T * R has last column (5, 0) and R * T has (0, 5), and applying them to (1, 0) gives (5, 1) and (0, 6). Rotating (3, 1) a quarter turn about the pivot (2, 1) moves it to (2, 2).

Seeing a composed matrix

The next demo lets you build a chain of operations, shows the final 3 by 3 matrix and draws the result. Pick a list of steps in the order you would describe them ("rotate, then scale, then move"). The code multiplies the matrices so that the first step ends up rightmost.

A live matrix visualizer

Compare the first two presets: the same operations in a different order produce visibly different matrices and pictures. Preset 3 shows a pivot rotation: the center at (1, 1) stays put and the square spins around it.

Why this is worth it

Because a single matrix can stand for an arbitrarily long chain, you build the matrix once per object and apply it to every vertex. Scene graphs multiply a parent's matrix by its children's matrices, so rotating an arm moves the hand and the fingers. Canvas, SVG, CSS and OpenGL all expose matrices directly. You will also reuse these ideas in 3D, where the matrices become 4 by 4. See 3D points and transformation matrices.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can translation not be written as a 2x2 matrix?

    A 2x2 matrix always maps the origin to the origin. Adding a third coordinate equal to 1 lets the matrix carry the offset in its last column.
  2. 2. With column vectors, what does the product B * A * p mean?

    The matrix closest to the point acts first. Matrix multiplication is not commutative, so B * A differs from A * B in general.
  3. 3. Which product rotates by ฮธ about the pivot (px, py)?

    Reading right to left: move the pivot to the origin, rotate, then move back.
  4. 4. What 2D point does the homogeneous point (6, 9, 3) represent?

    Divide x and y by w: (6/3, 9/3) = (2, 3).

Summary

  • Adding a third coordinate w = 1 lets translation become a matrix, so all affine transformations are 3x3 matrices.
  • The homogeneous point (x, y, w) stands for (x/w, y/w); affine matrices keep w = 1.
  • Composition is matrix multiplication: with column vectors the rightmost matrix acts first, and the order matters.
  • Pivot rotation and fixed-point scaling are T(p) * M * T(-p).
  • One composed matrix can be applied to any number of vertices, which is why graphics systems are built on matrices.

Next, we add mirroring, slanting and going backwards: Reflection, Shear and Inverse Transforms.