2D Transformations
Transformations modify the position, orientation, or size of objects. In 2D graphics, the basic transformations are translation, rotation, and scaling. These can be combined using matrix multiplication.
Translation
Moves an object by adding offset values to its coordinates.
Translation matrix (homogeneous):
[ 1 0 tx ] [ x ] [ x + tx ]
[ 0 1 ty ] [ y ] = [ y + ty ]
[ 0 0 1 ] [ 1 ] [ 1 ]
Example: Move (3, 5) by (2, -1)
[ 1 0 2 ] [ 3 ] [ 5 ]
[ 0 1 -1 ] [ 5 ] = [ 4 ]
[ 0 0 1 ] [ 1 ] [ 1 ]
Scaling
Resizes an object by multiplying coordinates by scale factors.
Scaling matrix:
[ sx 0 0 ] [ x ] [ sx * x ]
[ 0 sy 0 ] [ y ] = [ sy * y ]
[ 0 0 1 ] [ 1 ] [ 1 ]
sx, sy > 1 → enlargement
sx, sy < 1 → shrink
sx ≠ sy → non-uniform scaling
Example: Scale (3, 4) by (2, 0.5)
[ 2 0 0 ] [ 3 ] [ 6 ]
[ 0 .5 0 ] [ 4 ] = [ 2 ]
[ 0 0 1 ] [ 1 ] [ 1 ]
Rotation
Rotates an object around the origin by angle θ.
Rotation matrix (counterclockwise):
[ cos(θ) -sin(θ) 0 ] [ x ] [ x*cos(θ) - y*sin(θ) ]
[ sin(θ) cos(θ) 0 ] [ y ] = [ x*sin(θ) + y*cos(θ) ]
[ 0 0 1 ] [ 1 ] [ 1 ]
Example: Rotate (1, 0) by 90°
[ 0 -1 0 ] [ 1 ] [ 0 ]
[ 1 0 0 ] [ 0 ] = [ 1 ]
[ 0 0 1 ] [ 1 ] [ 1 ]
Rotate (1, 0) by 45°:
x' = cos(45°) = 0.707
y' = sin(45°) = 0.707
Composite Transformations
Multiple transformations are combined by multiplying their matrices. Order matters — matrix multiplication is not commutative.
Scale then Translate:
T * S ≠ S * T
[ 1 0 tx ] [ sx 0 0 ] [ sx 0 tx ]
[ 0 1 ty ] [ 0 sy 0 ] = [ 0 sy ty ]
[ 0 0 1 ] [ 0 0 1 ] [ 0 0 1 ]
Rotate about a point (px, py):
1. Translate to origin: T(-px, -py)
2. Rotate: R(θ)
3. Translate back: T(px, py)
M = T(px,py) * R(θ) * T(-px,-py)
Shear
Distorts an object by shifting one axis proportional to another.
X-shear:
[ 1 shx 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
Y-shear:
[ 1 0 0 ]
[ shy 1 0 ]
[ 0 0 1 ]