2201NSC
Affine Transformations
An affine transformation maps points, straight lines and planes while preserving parallelism β parallel lines stay parallel.
Widely used in geometry, computer graphics and vision to map coordinates while keeping geometric relations.
Unlike linear transformations, affine transforms can also translate (shift) the space.
This generalisation makes affine transformations particularly useful for:
Geometry correction
for
satellite images by S. Ban & T. Kim (2024).
Source: doi.org/10.3390/rs16162890
Visualizing the hidden 3D geometry behind LLMs by Sifal Klioui (2026).
Source: sifal.social/posts/Why-Modern-LLMs-Dropped-Mean-Centering-(And-Got-Away-With-It)
Visualizing the hidden 3D geometry behind LLMs by Sifal Klioui (2026).
Source: sifal.social/posts/Why-Modern-LLMs-Dropped-Mean-Centering-(And-Got-Away-With-It)
Consider a vector $\mathbf x = (x,y)^T\in \R^2.$ A transformation $T: \mathbb R^2 \to \mathbb R^2$ of the form
\( T\left( \mathbf x\right) = \begin{pmatrix} ax + by + e \\ cx + dy + f \end{pmatrix} \)
where $a,b,c,d,e,$ and $f$ are real numbers, is called a two-dimensional affine transformation.
|
\( T\left( \mathbf x\right) = \begin{pmatrix} ax + by + e \\ cx + dy + f \end{pmatrix} \) For example, if $a=d = 1,$ and $b=c=0,$ then we have a pure translation \[ T\left( \mathbf x\right) = \begin{pmatrix} x + e \\ y + f \end{pmatrix} \] |
|
\( T\left( \mathbf x\right) = \begin{pmatrix} ax + by + e \\ cx + dy + f \end{pmatrix} \) If $b=c=0$ and $e=f=0,$ then we have a pure scaling \[ T\left( \mathbf x\right) = \begin{pmatrix} ax \\ dy \end{pmatrix} \] |
|
\( T\left( \mathbf x\right) = \begin{pmatrix} ax + by + e \\ cx + dy + f \end{pmatrix} \) If $a=d = \cos \theta,$ $b = -\sin \theta,$ $c = \sin \theta,$ and $e=f=0,$ then we have a pure rotation about the origin \[ \small T\left( \mathbf x\right) = \begin{pmatrix} x\cos\theta -y \sin \theta \\ x\sin \theta + y \cos \theta \end{pmatrix} \] |
|
\( T\left( \mathbf x\right) = \begin{pmatrix} ax + by + e \\ cx + dy + f \end{pmatrix} \) Finally, if $a=d=1,$ and $e=f=0,$ we have the shear transformations \[ T\left( \mathbf x\right) = \begin{pmatrix} x+ by \\ y+ cx \end{pmatrix} \] |
We know a linear transformation can be written in matrix form
\(T(\mathbf{x}) = \begin{pmatrix} ax + by \\ cx + dy \end{pmatrix} \) \( = \begin{pmatrix} a & b \\ c & d \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix}, \)
Can we do the same with affine transformations?
No, the problem is the translationβοΈ
To enable affine transformations, such as translation, to be represented using matrix multiplication, we embed these 2D vectors into 3D space using homogeneous coordinates by appending a third coordinate, conventionally set to 1:
\( \begin{pmatrix} x \\ y \end{pmatrix} \) \( \quad \Rightarrow \quad \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}. \)
This third coordinate is typically called \(w\) to distinguish it from the usual \(z\)-coordinate in 3D geometry.
Therefore, affine transformations can be written as
For example, suppose we have $2\times 2$ square centred at the origin. First, we want to rotate the square by $45^{\circ}$ about its centre; and then move it so its centre is at $(2, 3).$
For example, suppose we have $2\times 2$ square centred at the origin. First, we want to rotate the square by $45^{\circ}$ about its centre; and then move it so its centre is at $(2, 3).$
In matrix form we have
${\large M}=$ $ \overbrace{\begin{pmatrix} 1 & 0 & \color{blue}2 \\ 0 & 1 & \color{blue}3 \\ 0 & 0 & 1 \end{pmatrix}}^{\text{Translate at } \color{blue}{(2,3)}} $ $ \overbrace{\begin{pmatrix} \color{red}{\cos 45^{\circ}} & \color{red}{-\sin 45^{\circ}}& 0 \\ \color{red}{\sin 45^{\circ}} & \color{red}{\cos 45^{\circ}} & 0 \\ 0 & 0 & 1 \end{pmatrix}}^{\text{Rotate } 45^\circ} \qquad\qquad\quad\;$
$= \begin{pmatrix} \cos 45^{\circ} & -\sin 45^{\circ}& 3 \\ \sin 45^{\circ} & \cos 45^{\circ} & 2 \\ 0 & 0 & 1 \end{pmatrix}$ $= \begin{pmatrix} \sqrt{2}/2 & -\sqrt{2}/2 & 3 \\ \sqrt{2}/2 & \sqrt{2}/2 & 2 \\ 0 & 0 & 1 \end{pmatrix}$
For example, suppose we have $2\times 2$ square centred at the origin. First, we want to rotate the square by $45^{\circ}$ about its centre; and then move it so its centre is at $(2, 3).$
Now consider the vertices of the square using homogeneous coordinates
\( v_1 = \begin{pmatrix} 1 \\ 1 \\ \end{pmatrix}, \;\; v_2 = \begin{pmatrix} -1 \\ 1 \\ \end{pmatrix}, \;\; v_3 = \begin{pmatrix} 1 \\ -1 \\ \end{pmatrix},\;\; v_4 = \begin{pmatrix} -1 \\ -1 \\ \end{pmatrix}. \)
\( v_1 = \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix}, \;\; v_2 = \begin{pmatrix} -1 \\ 1 \\ 1 \end{pmatrix}, \;\; v_3 = \begin{pmatrix} 1 \\ -1 \\ 1 \end{pmatrix},\;\; v_4 = \begin{pmatrix} -1 \\ -1 \\ 1 \end{pmatrix}. \)
Multiplying by the matrix $M$ we obtain the new vertices in 3D space:
\( M \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 2+\sqrt{2} \\ 1 \end{pmatrix},\quad M \begin{pmatrix} -1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3-\sqrt{2} \\ 2\\ 1 \end{pmatrix}, \)
\( M \begin{pmatrix} 1 \\ -1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 + \sqrt{2} \\ 2 \\ 1 \end{pmatrix}, \quad M \begin{pmatrix} -1 \\ -1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 2-\sqrt{2} \\ 1 \end{pmatrix}. \)
For example, suppose we have $2\times 2$ square centred at the origin. First, we want to rotate the square by $45^{\circ}$ about its centre; and then move it so its centre is at $(2, 3).$
Multiplying by the matrix $M$ we obtain the new vertices in 3D space:
\( M \begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 2+\sqrt{2} \\ 1 \end{pmatrix},\quad M \begin{pmatrix} -1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3-\sqrt{2} \\ 2\\ 1 \end{pmatrix}, \)
\( M \begin{pmatrix} 1 \\ -1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 + \sqrt{2} \\ 2 \\ 1 \end{pmatrix}, \quad M \begin{pmatrix} -1 \\ -1 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 2-\sqrt{2} \\ 1 \end{pmatrix}. \)
Finally, we remove the third component to obtain the new vertices in 2D:
\( v_1' = \begin{pmatrix} 3 \\ 2+\sqrt{2} \\ \end{pmatrix},\;\; v_2'= \begin{pmatrix} 3-\sqrt{2} \\ 2\\ \end{pmatrix},\;\; v_3'= \begin{pmatrix} 3 + \sqrt{2} \\ 2 \\ \end{pmatrix}, \;\; v_4' = \begin{pmatrix} 3 \\ 2-\sqrt{2} \\ \end{pmatrix}. \)
For example, suppose we have $2\times 2$ square centred at the origin. First, we want to rotate the square by $45^{\circ}$ about its centre; and then move it so its centre is at $(2, 3).$
\( v_1' = \begin{pmatrix} 3 \\ 2+\sqrt{2} \\ \end{pmatrix},\;\; v_2'= \begin{pmatrix} 3-\sqrt{2} \\ 2\\ \end{pmatrix},\;\; v_3'= \begin{pmatrix} 3 + \sqrt{2} \\ 2 \\ \end{pmatrix}, \;\; v_4' = \begin{pmatrix} 3 \\ 2-\sqrt{2} \\ \end{pmatrix}. \)
An Iterated Function System (IFS) consists of a finite set of affine transformations \( \{T_1, T_2, \dots, T_n\},\) each of the form:
\( T_i(\mathbf{x}) = A_i \mathbf{x} + \mathbf{t}_i, \)
where \( A_i \) is a \(2 \times 2\) matrix representing a linear transformation (scaling, rotation, or shearing), and \( \mathbf{t}_i \) is a translation vector.
IFS: \( \;T_i(\mathbf{x}) = A_i \mathbf{x} + \mathbf{t}_i \)
There are two common approaches for plotting fractals using IFS:
Let's start with a simple shape: A circle centred at the origin. At each iteration, we apply three affine transformations that scale the circle by a factor of $1/2$ and translate it to a new location.
Using homogeneous coordinates, the transformations are defined as follows:
\( \begin{aligned} T_1(\mathbf x) &= \begin{pmatrix} 0.5 & 0 & 0\\ 0 & 0.5 & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_2(\mathbf{x}) &= \begin{pmatrix} 0.5 & 0 & 0 \\ 0 & 0.5 & 100 \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_3(\mathbf{x}) &= \begin{pmatrix} 0.5 & 0 & 100 \\ 0 & 0.5 & 100 \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}. \end{aligned} \)
\( \begin{aligned} T_1(\mathbf x) &= \begin{pmatrix} \color{red}{0.5} & 0 & 0\\ 0 & \color{red}{0.5} & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_2(\mathbf{x}) &= \begin{pmatrix} \color{red}{0.5} & 0 & 0 \\ 0 & \color{red}{0.5} & 100 \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_3(\mathbf{x}) &= \begin{pmatrix} \color{red}{0.5} & 0 & 100 \\ 0 & \color{red}{0.5} & 100 \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}. \end{aligned} \)
\( \begin{aligned} T_1(\mathbf x) &= \begin{pmatrix} 0.5 & 0 & \color{blue}{0}\\ 0 & 0.5 & \color{blue}{0} \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_2(\mathbf{x}) &= \begin{pmatrix} 0.5 & 0 & \color{blue}{0} \\ 0 & 0.5 & \color{blue}{100} \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_3(\mathbf{x}) &= \begin{pmatrix} 0.5 & 0 & \color{blue}{100} \\ 0 & 0.5 & \color{blue}{100} \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}. \end{aligned} \)
π» Pseudocode →Deterministic IFS: \( \begin{aligned} T_1(\mathbf x) &= \begin{pmatrix} 0.5 & 0 & 0\\ 0 & 0.5 & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \end{aligned} \) \( \begin{aligned} T_2(\mathbf{x}) &= \begin{pmatrix} 0.5 & 0 & 0 \\ 0 & 0.5 & 100 \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \end{aligned} \) \( \begin{aligned} T_3(\mathbf{x}) &= \begin{pmatrix} 0.5 & 0 & 100 \\ 0 & 0.5 & 100 \\ 0 & 0 & 1\end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}. \end{aligned} \) |
|
The Barnsley fern is a well-known example of a natural-looking fractal generated using a probabilistic IFS.
It consists of four affine transformations, each applied with a specified probability.
These transformations are iteratively applied to a single point, and the accumulation of these points produces the characteristic fern shape.
The transformations are defined as follows, with a given probability for each case:
|
$ \begin{aligned} T_1(\mathbf{x}) &= \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0.16 & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_2(\mathbf{x}) &= \begin{pmatrix} 0.85 & 0.04 & 0 \\ -0.04 & 0.85 & 1.6 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_3(\mathbf{x}) &= \begin{pmatrix} 0.2 & -0.26 & 0 \\ 0.23 & 0.22 & 1.6 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}, \\[1ex] T_4(\mathbf{x}) &= \begin{pmatrix} -0.15 & 0.28 & 0 \\ 0.26 & 0.24 & 0.44 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}. \end{aligned} $ |
|
IFS values for a fern
| T | a | b | c | d | e | f | p |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0.16 | 0 | 0 | 0.01 |
| 2 | 0.85 | 0.04 | -0.04 | 0.85 | 0 | 1.6 | 0.85 |
| 3 | 0.2 | -0.26 | 0.23 | 0.22 | 0 | 1.6 | 0.07 |
| 4 | -0.15 | 0.28 | 0.26 | 0.24 | 0 | 0.44 | 0.07 |
π $\;T_i(\mathbf x) = \begin{pmatrix} a & b & e \\ c & d & f \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix}$
|
Probabilistic IFS: $ \begin{aligned} T_1(\mathbf{x}) &= \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0.16 & 0 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} \end{aligned} \mapsto 0.01 $ $ \begin{aligned} T_2(\mathbf{x}) &= \begin{pmatrix} 0.85 & 0.04 & 0 \\ -0.04 & 0.85 & 1.6 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} \end{aligned} \mapsto 0.85 $ $ \begin{aligned} T_3(\mathbf{x}) &= \begin{pmatrix} 0.2 & -0.26 & 0 \\ 0.23 & 0.22 & 1.6 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} \end{aligned} \mapsto 0.07 $ $ \begin{aligned} T_4(\mathbf{x}) &= \begin{pmatrix} -0.15 & 0.28 & 0 \\ 0.26 & 0.24 & 0.44 \\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ 1 \end{pmatrix} \end{aligned} \mapsto 0.07 $ |
π» Pseudocode ↓
|
Deterministic IFS
Probabilistic IFS