Linear Algebra › Section B · Matrix algebra › Topic B.7
Matrices in computer graphics
Every time a game character turns, a map zooms or a 3D model spins on screen, a computer is multiplying matrices. One clever trick, adding an extra coordinate, lets a single matrix product move, turn, stretch and even put perspective on a picture.
Pictures as matrices
A computer stores a drawing as a list of points, often the corners of the straight segments or triangles that make it up. Put those points as the columns of a data matrix \(D\). If \(A\) is the standard matrix of a linear transformation, then \(AD\) holds the transformed points, all computed in one product. Because a linear transformation sends straight segments to straight segments, transforming the corners is enough: the computer redraws the segments between the new corners. Graphics hardware is built to do these products at enormous speed.
The basic linear moves
In the plane, scaling by \(s\) uses \(sI\), and scaling by different amounts along the axes uses a diagonal matrix. A horizontal shear, which slants a shape like italic type, sends \((x, y)\) to \((x + ky, y)\). Rotation by an angle \(\theta\) counterclockwise about the origin and reflection in the \(x\)-axis use
These are the standard matrices met in "The matrix of a linear transformation".
Worked example: slanting a rectangle
A rectangle has corners (0, 0), (2, 0), (2, 3) and (0, 3). Shear it so that \(x\) becomes \(x + 0.5y\). 1. Store the corners as columns and multiply:
2. The new corners are (0, 0), (2, 0), (3.5, 3) and (1.5, 3). 3. Check: points on the bottom edge, where \(y = 0\), do not move, and the top edge slides right by \(0.5 \times 3 = 1.5\). The rectangle becomes a parallelogram with the same base and height, so its area is still 6.
The trouble with translation
Moving every point by a fixed vector \(\mathbf{p}\), so \(\mathbf{x} \mapsto \mathbf{x} + \mathbf{p}\), is called translation, and it is the most common move of all. But it is not a linear transformation when \(\mathbf{p} \ne \mathbf{0}\), because it moves the origin, and every linear transformation sends \(\mathbf{0}\) to \(\mathbf{0}\). So no \(2 \times 2\) matrix can translate the plane. Graphics programs want every move to be a matrix product, so that a whole sequence of moves can be combined into one matrix.
Homogeneous coordinates
The fix is to give each point \((x, y)\) of the plane the homogeneous coordinates \((x, y, 1)\). Translation by \((h, k)\) is then a \(3 \times 3\) matrix product:
Any \(2 \times 2\) linear transformation \(A\) also fits, as the block matrix with \(A\) in the top-left corner, zeros beside and below it, and 1 in the bottom-right corner. Now translation, rotation, scaling and shear are all \(3 \times 3\) matrices.
Composite transformations
To apply several moves in turn, multiply their matrices, with the first move on the right: doing \(M_1\), then \(M_2\), then \(M_3\) is the single matrix \(M_3M_2M_1\). Order matters, because matrix multiplication is not commutative. Take the point (1, 1). Translating by (3, 0) and then rotating 90° about the origin gives (4, 1) and then (−1, 4). Rotating first and then translating gives (−1, 1) and then (2, 1). A program computes the composite matrix once and applies it to thousands of points.
Worked example: rotating about a point
Find the matrix that rotates the plane 90° counterclockwise about the point (2, 1), not about the origin. 1. Translate (2, 1) to the origin, by \((-2, -1)\). 2. Rotate 90° about the origin. 3. Translate back by \((2, 1)\). 4. Multiply, first move on the right:
5. Check: \(M(2, 1, 1) = (2, 1, 1)\), so the centre stays put, and \(M(4, 1, 1) = (2, 3, 1)\): the point 2 units right of the centre ends up 2 units above it, as a quarter turn should.
Three dimensions
In space, the point \((x, y, z)\) gets homogeneous coordinates \((x, y, z, 1)\), and every move becomes a \(4 \times 4\) matrix. Translation by \((h, k, m)\) puts \(h\), \(k\), \(m\) in the last column above a 1. Rotation by \(\theta\) about the \(z\)-axis uses the plane rotation in the top-left \(2 \times 2\) block, with 1s for \(z\) and the extra coordinate. More generally, \((X, Y, Z, H)\) with \(H \ne 0\) stands for the point \((X/H, Y/H, Z/H)\), so any non-zero multiple names the same point. That freedom is what makes perspective possible.
Perspective projection
A screen shows a 3D scene as seen from one eye position. Put the eye at \((0, 0, d)\) on the \(z\)-axis and let the screen be the \(xy\)-plane. A point \((x, y, z)\) with \(z < d\) appears where the line to the eye meets the screen: at \(\left(\frac{x}{1 - z/d}, \frac{y}{1 - z/d}, 0\right)\). In homogeneous form,
sends \((x, y, z, 1)\) to \((x, y, 0, 1 - z/d)\), and dividing by the last entry gives the screen point. With \(d = 10\), the point (3, 2, 5) becomes (3, 2, 0, 0.5), so it is drawn at (6, 4).
Common mistakes to avoid
First, multiplying composite matrices in the order the moves happen: the first move goes on the right. Second, trying to translate with a \(2 \times 2\) matrix; translation needs homogeneous coordinates. Third, forgetting the final 1 when writing a point in homogeneous coordinates, or forgetting to divide by the last coordinate after a perspective projection. Fourth, rotating about the origin when the problem asks for rotation about another point; translate there and back.
Key takeaways
A figure is stored as a data matrix whose columns are its points, and AD transforms them all at once. Scaling, shear, rotation and reflection are linear, but translation is not, since it moves the origin. Homogeneous coordinates write (x, y) as (x, y, 1), so translation and every linear move become 3 × 3 matrices, and 4 × 4 matrices in space. A sequence of moves is one product, with the first move on the right. A rotation about a point is translate, rotate, translate back. (X, Y, Z, H) stands for (X/H, Y/H, Z/H), which lets one matrix produce a perspective projection.
Flashcards
Flashcard
A matrix whose columns are the points of a figure
Test your understanding
Question 1 of 5
Why can't a 2 × 2 matrix translate every point of the plane by (3, 1)?
Question 2 of 5
In homogeneous coordinates, what does the matrix with rows (1, 0, −4), (0, 1, 2), (0, 0, 1) do to the point (5, 3)?
Question 3 of 5
A program scales a figure by matrix S, then rotates it by R, then translates it by T. Which single matrix does the whole job?
Question 4 of 5
What is the image of the point (1, 1) under translation by (3, 0) followed by rotation 90° counterclockwise about the origin?
Question 5 of 5
With the eye at (0, 0, 10) and the screen in the xy-plane, where is the point (4, −2, 6) drawn?
