Skip to content

Affine Transformations

Published On:
Nov 14, 2017
Last Updated:
Nov 14, 2017

An affine transformation is a function between two different coordinate systems (or affine spaces) which preserves certain properties. These properties include:

  • Points (points that exists in the first coordinate system will exist in the second coordinate system)
  • Straight lines (lines that are straight in the first coordinate system are straight in the second)
  • Parallelism (lines that are parallel in the first coordinate system are parallel in the second)

An affine transformation can encompass the following “basic” modifications:

  • Translation
  • Reflection
  • Scaling
  • Rotation
  • Shearing

The Transformation Matrix

A 2D affine transformation matrix is always in the form:

M=[abtxcdty001]M = \begin{bmatrix}a&b&t_x\\c&d&t_y\\0&0&1\end{bmatrix}

This maps from one affine space to another via the equation:

[xy1]=[abtxcdty001][xy1]\begin{bmatrix}x^\prime\\y^\prime\\1\end{bmatrix} = \begin{bmatrix}a&b&t_x\\c&d&t_y\\0&0&1\end{bmatrix}\begin{bmatrix}x\\y\\1\end{bmatrix}

Note how one more matrix dimension is used than the dimension of the space you wish to apply the affine transformation to. This is so that translation transformations can be performed.

Translation

A 2D translation can be described by the following equation (using homogeneous coordinates):

[xy1]=[10tx01ty001][xy1]\begin{bmatrix}x^\prime\\y^\prime\\1\end{bmatrix} = \begin{bmatrix}1&0&t_x\\0&1&t_y\\0&0&1\end{bmatrix} \begin{bmatrix}x\\y\\1\end{bmatrix}

Scaling

A 2D scaling can be done with:

[xy1]=[sx000sy0001][xy1]\begin{bmatrix}x^\prime\\y^\prime\\1\end{bmatrix} = \begin{bmatrix}s_x&0&0\\0&s_y&0\\0&0&1\end{bmatrix} \begin{bmatrix}x\\y\\1\end{bmatrix}

Rotation

A 2D counter-clockwise rotation can be described by the following equation:

[xy1]=[cosθsinθ0sinθcosθ0001][xy1]\begin{bmatrix}x^\prime\\y^\prime\\1\end{bmatrix} = \begin{bmatrix}cos\theta&-sin\theta&0\\sin\theta&cos\theta&0\\0&0&1\end{bmatrix} \begin{bmatrix}x\\y\\1\end{bmatrix}

Note that the above equation performs rotation around the origin. To perform rotation around an arbitrary point, first translate the point to the origin, perform the rotation, and then translate back.

3D rotation can be divided into 3 rotations around each of the axis:

Rx=[1000cosθsinθ0sinθcosθ]R_x = \begin{bmatrix}1&0&0\\0&cos\theta&-sin\theta\\0&sin\theta&cos\theta\end{bmatrix} Ry=[cosθ0sinθ010sinθ0cosθ]R_y = \begin{bmatrix}cos\theta&0&sin\theta\\0&1&0\\-sin\theta&0&cos\theta\end{bmatrix} Rz=[cosθsinθ0sinθcosθ0001]R_z = \begin{bmatrix}cos\theta&-sin\theta&0\\sin\theta&cos\theta&0\\0&0&1\end{bmatrix}

Shearing

A x direction shear (a shearing along x that is proportional to y):

[xy1]=[100λ10001][xy1]\begin{bmatrix}x^\prime\\y^\prime\\1\end{bmatrix} = \begin{bmatrix}1&0&0\\\lambda&1&0\\0&0&1\end{bmatrix} \begin{bmatrix}x\\y\\1\end{bmatrix}

A y direction shear (a shearing along y that is proportional to x):

[xy1]=[1λ0010001][xy1]\begin{bmatrix}x^\prime\\y^\prime\\1\end{bmatrix} = \begin{bmatrix}1&\lambda&0\\0&1&0\\0&0&1\end{bmatrix} \begin{bmatrix}x\\y\\1\end{bmatrix}

Combining Transformations

Multiple affine transformations (translation, rotation, shear, e.t.c) can be combined into a single transformation matrix.

Remember, we can do this because matrix multiplication is associative! A(Bx)=(AB)xA(Bx) = (AB)x.