Matrices as transformations

Multiplying a point (x, y) by a 2×2 matrix moves it to a new point. Doing this to every point transforms the whole plane.

Read the columns

The first column of the matrix is where (1, 0) lands; the second column is where (0, 1) lands. Every other point follows, because (x, y) = x·(1, 0) + y·(0, 1).

MoveMatrixEffect
Scale[[2, 0], [0, 3]]2× wider, 3× taller
Rotate by θ[[cos θ, −sin θ], [sin θ, cos θ]]turns around the origin
Shear[[1, 1], [0, 1]]slides each row sideways by its height
Reflect[[1, 0], [0, −1]]mirror in the x-axis

The example moves the corners of the unit square with each one.

from math import cos, sin, radians

def apply(m, p):
    x = m[0][0] * p[0] + m[0][1] * p[1]
    y = m[1][0] * p[0] + m[1][1] * p[1]
    return (round(x, 3) + 0.0, round(y, 3) + 0.0)

square = [(0, 0), (1, 0), (1, 1), (0, 1)]
t = radians(90)
moves = [("rotate 90°", [[cos(t), -sin(t)], [sin(t), cos(t)]]),
         ("scale", [[2, 0], [0, 3]]),
         ("shear", [[1, 1], [0, 1]])]
for name, m in moves:
    print(name, [apply(m, p) for p in square])
Open in compiler

Combining moves

To rotate first and then scale, compute S·(R·p) = (S·R)·p. The matrix of the combined move is the product, written right to left, and order matters: rotating then scaling differs from scaling then rotating.

Where it is used

Every 3-D game and drawing program stores positions as vectors and moves them with matrices. Image processing, robotics arms and computer vision use the same idea.

Common mistakes

  • Multiplying in reading order. The move applied first goes on the right.
  • Forgetting that sin and cos in Python take radians: use math.radians(90), not 90.

Quick check

1. The first column of a 2×2 matrix is…
2. To rotate first (R) and then scale (S), the combined matrix is…
3. Which matrix doubles every length?
My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.