2D transformations
2D transformations: how points and shapes are moved, scaled, rotated, reflected and sheared in a plane using linear algebra and homogeneous coordinates, and how to compose and invert these operations.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
2D transformations are the basic tools for placing, animating and manipulating objects in computer graphics, UI design, CAD and games. Engineers use these operations to move models between coordinate systems (model, world, view, screen), to perform geometric effects (scaling, rotation, reflection) and to combine transformations efficiently using matrices.
Key ideas
Start from a point in the plane. Let (x, y) be its coordinates in some Cartesian coordinate system. A 2D transformation maps each point (x, y) to a new point (x', y'). We build transformations from first principles as follows.
- A point is an ordered pair (x, y). Units may be metres, centimetres, or pixels; specify consistently.
- A vector is an ordered pair (vx, vy) representing displacement; vectors and points use the same coordinates but conceptually differ (vectors have no fixed origin).
- A linear transformation is a map T: R^2 -> R^2 with T(a u + b v) = a T(u) + b T(v) for all scalars a,b and vectors u,v. Linear transformations must map the origin (0,0) to (0,0).
- A translation is not linear (it moves the origin). To represent translations in a matrix form we use homogeneous coordinates.
- Homogeneous coordinates: represent a point (x, y) by a 3-element column vector [x y 1]^T. Then any affine transformation (linear + translation) is a 3×3 matrix acting on homogeneous coordinates. Represent a direction vector v by [vx vy 0]^T so that translations do not affect it.
Common elementary transformations (in homogeneous 3×3 matrix form). Describe the matrix to draw in an exam: draw a 3×3 matrix with rows and columns indexed (1..3) and label entries as below.
Translation by (tx, ty): moves every point by tx in x and ty in y. Matrix (write rows left-to-right): [1 0 tx; 0 1 ty; 0 0 1]. Translation does not change angles or lengths.
Scaling by factors (sx, sy) about the origin: stretches coordinates. Matrix: [sx 0 0; 0 sy 0; 0 0 1]. If sx = sy = s it's uniform scaling.
Rotation by angle θ about the origin (counter-clockwise positive in a standard right-handed xy system where +y is up): Matrix (top-left 2×2 is orthogonal): [cosθ -sinθ 0; sinθ cosθ 0; 0 0 1]. Rotation preserves lengths and orientation; determinant = +1.
Reflection about the x-axis, y-axis or a line through origin: reflection matrices are linear with determinant -1. For reflection about x-axis: [1 0 0; 0 -1 0; 0 0 1]. About y-axis: [-1 0 0; 0 1 0; 0 0 1]. About a line through the origin making angle φ with the x-axis: [cos2φ sin2φ 0; sin2φ -cos2φ 0; 0 0 1].
Shear (also called skew) in x by factor shx: [1 shx 0; 0 1 0; 0 0 1]. Shear in y by shy: [1 0 0; shy 1 0; 0 0 1]. Shear preserves area only if determinant = 1.
Properties and composition:
- To apply transformations T1 then T2 to a point p (in homogeneous form), compute p' = M2 (M1 p) = (M2·M1) p. Thus matrices multiply in reverse order of application.
- Matrix multiplication for transformations is generally not commutative: M1·M2 ≠ M2·M1 in general. Order matters; remember “last matrix is applied first” if you write p' = M p.
- The inverse of an affine transformation exists when its upper-left 2×2 linear part is invertible (non-zero determinant). The inverse undoing translation T(tx,ty) is T(-tx,-ty); inverse of scaling is scaling by 1/sx,1/sy (if sx,sy ≠ 0); inverse of rotation by θ is rotation by -θ. In general, for M = [[A, t], [0, 1]] with 2×2 A invertible and t a 2×1 vector, M^{-1} = [[A^{-1}, -A^{-1} t], [0, 1]].
Rotation about an arbitrary point (xr, yr): perform translation to origin, rotate, then translate back. In matrices: M = T(xr,yr)·R(θ)·T(-xr,-yr). In an exam draw three arrows: translate (arrow labelled -xr,-yr), rotate about origin by θ (arc arrow), translate back (arrow labelled +xr,+yr).
Determinant and geometric meaning:
- For a linear 2×2 transform A (upper-left of homogeneous matrix), determinant det(A) gives area scaling factor and sign indicates orientation: det>0 preserves orientation, det<0 reverses it.
Coordinate system note:
- In many screen systems y increases downward. For those systems the sign of sinθ in the rotation matrix may be reversed for “visually correct” clockwise positive rotation. Always state which coordinate convention you use. For y-down with clockwise-positive rotation, one common choice is M_R(θ) = [[cosθ, sinθ, 0], [-sinθ, cosθ, 0], [0, 0, 1]].
Formulas
Translation: M_T(tx,ty) = [[1, 0, tx], [0, 1, ty], [0, 0, 1]]
- tx, ty: translation distances in metres (m) or pixels (px). Applies to homogeneous point [x y 1]^T.
Scaling: M_S(sx,sy) = [[sx, 0, 0], [0, sy, 0], [0, 0, 1]]
Scaling about point (xc,yc): M = M_T(xc,yc) · M_S(sx,sy) · M_T(-xc,-yc)
- sx, sy: scale factors (unitless). Applies about origin.
Rotation: M_R(θ) = [[cosθ, -sinθ, 0], [sinθ, cosθ, 0], [0, 0, 1]]
- θ: rotation angle in radians (rad). cosθ, sinθ unitless. Positive θ is counter-clockwise in standard right-handed xy.
Shear (x): M_shx = [[1, shx, 0], [0, 1, 0], [0, 0, 1]]
Shear (y): M_shy = [[1, 0, 0], [shy, 1, 0], [0, 0, 1]]
- shx, shy: shear factors (unitless).
Composite: M = M2 · M1 (apply M1 then M2)
- M1,M2: 3×3 homogeneous matrices; multiplication order matters.
Inverse translation: M_T^{-1}(tx,ty) = M_T(-tx,-ty)
- Inverse of translation undoes movement.
Inverse scaling: M_S^{-1}(sx,sy) = M_S(1/sx, 1/sy) (requires sx,sy ≠ 0)
Inverse rotation: M_R^{-1}(θ) = M_R(-θ)
Area scale: area' = det(A) · area where A is the 2×2 linear part (upper-left) of the homogeneous matrix
- det(A) unitless; if det(A)<0 orientation reverses.
Apply to point: [x' y' 1]^T = M · [x y 1]^T
- x,y,x',y' coordinates in metres or pixels; M is 3×3 matrix.
Worked examples
- Rotate point (2.0 cm, 3.0 cm) by 45° about origin.
- Given: p = [2.0, 3.0]. θ = 45° = π/4 rad.
- Formula: [x' y' 1]^T = M_R(θ) · [x y 1]^T. Use θ = π/4; cosθ = √2/2 ≈ 0.70710678, sinθ = 0.70710678.
- Compute x' = x cosθ - y sinθ = 2·0.70710678 - 3·0.70710678 = (2-3)·0.70710678 = -0.70710678 cm.
- Compute y' = x sinθ + y cosθ = 2·0.70710678 + 3·0.70710678 = 5·0.70710678 = 3.5355339 cm.
- Final: (-0.7071 cm, 3.5355 cm) (rounded to 4 decimal places).
- Scale then translate: point (1,2) in metres; scale sx=2.0, sy=0.5 about origin then translate by (tx=5.0 m, ty=-3.0 m). Compute final coordinates and show non-commutativity by reversing order.
- Step A (scale then translate): M = M_T(5,-3) · M_S(2,0.5).
- Apply to p = [1,2,1]^T.
- First scale: p_s = [2·1, 0.5·2] = [2,1]. Then translate: p' = [2+5, 1-3] = [7, -2] m. So answer: (7.0 m, -2.0 m).
- Step B (translate then scale): M' = M_S(2,0.5) · M_T(5,-3). Apply to p: first translate: [1+5, 2-3] = [6,-1]; then scale: [2·6, 0.5·(-1)] = [12, -0.5] m. Different: (12.0 m, -0.5 m). This shows order matters.
- Rotate point (4,1) by 90° (π/2 rad) about pivot (2,2). Use homogeneous composition.
- Steps: Translate by (-2,-2), rotate by 90°, translate back by (+2,+2).
- M = T(2,2)·R(π/2)·T(-2,-2). Compute manually on point: p = (4,1).
- Translate to origin: p1 = (4-2, 1-2) = (2, -1).
- Rotate 90° CCW: (x',y') = (-y, x) so p2 = -(-1), 2 = (1, 2).
- Translate back: p3 = (1+2, 2+2) = (3, 4).
- Final: (3, 4) (units same as input, e.g., m or px).
Common mistakes
- Applying matrices in the wrong order (mixing up pre-multiplication vs post-multiplication). Remember: if p' = M2·M1·p, M1 is applied first.
- Using degrees where radians are required (for cos, sin) without conversion.
- Not carrying units for translations (mixing px and metres).
- Assuming matrix multiplication is commutative — giving same result when swapping transforms.
- Using rotation sign convention for screen coordinates (y down) without noting it.
- Forgetting to invert the 2×2 top-left for affine inverse; trying to invert a singular scale (sx or sy = 0).
For your exam
Typical question types:
- Definitions: define homogeneous coordinates, affine transform, linear transform.
- Short note: list and give matrices of elementary transforms; state geometric effects.
- Derivation: show rotation about arbitrary point = translate to origin, rotate, translate back; show composite matrix.
- Numerical: apply a sequence of transforms to given points (carry units, show each step, final coordinate rounded as asked).
- Diagram question: draw axes, mark original and transformed points, show pivot for rotation about arbitrary point, or show a rectangle before/after shear.
- Proof/true-false: show when transforms commute (e.g., two translations commute; uniform scalings commute with rotations about origin) and give counterexamples.
What full-marks answers include:
- Exact matrix forms (3×3 homogeneous) and correct order of multiplication.
- Clear statement of coordinate system convention (y up or y down) if rotation direction matters.
- For numericals, show intermediate steps, units, and final values with correct rounding.
- For diagram problems, label axes, points, pivots, and show vector displacements.
Quick check
- Give the 3×3 homogeneous matrix for scaling by 3 in x and 0.5 in y.
- If a rotation matrix R has θ = 30° and a point (√3,1), what is R applied to the point? (Use cos30=√3/2, sin30=1/2.)
- Does translation commute with rotation about the origin? Briefly justify.
Answers: 1) [[3,0,0],[0,0.5,0],[0,0,1]]. 2) Compute x' = x cos30 - y sin30 = √3·(√3/2) - 1·(1/2) = (3/2) - 1/2 = 1; y' = x sin30 + y cos30 = √3·(1/2) + 1·(√3/2) = √3. So result (1, √3). 3) No — translation moves the origin so TR ≠ RT in general; they commute only if the translation is zero or the rotation angle is 0° (mod 360°).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?