2D Transformations in Computer Graphics: Matrices, Algorithms and Worked Examples

Build 2D transformation fluency with one matrix convention, a step-by-step composite transform, and a complete window-to-viewport calculation.

KnowledgeGate Team

Exam prep & CS education

Updated 9 Sep 20266 min read

Translation, rotation and scaling become error-prone when matrices are multiplied in the wrong order, a fixed pivot is ignored, or screen coordinates run downward. Use one column-vector convention to verify every intermediate coordinate in a composite transformation and a world-window mapping. The CS Fundamentals for Exams & Placements category places this geometry alongside the coordinate and matrix skills it depends on.

2-D Transformations in Computer Graphics: Matrices, Worked Examples and Exam Patterns teaches the primitives individually, with a standalone pivot-rotation example and inverse recovery. Fixed-point scaling, rotation, translation, viewport mapping, and boundary clipping form a different continuous pipeline: one column-vector convention must survive every stage.

Related reading: Geometrical transformations and Clipping algorithms.

What a 2D transformation changes, and what it preserves

A geometric transformation maps an input point P=(x,y) to an output point P'=(x',y'). Rigid transformations, including translation, rotation and reflection, preserve distances and angles. Non-rigid affine transformations, including non-uniform scaling and shear, can change them. Every affine transformation still maps straight lines to straight lines and preserves parallelism.

An affine map may stretch or slant a shape, but it cannot bend a line into a curve. That observation is a way to reject an impossible result.

Homogeneous coordinates write a point as the column vector [x, y, 1]ᵀ. The third coordinate lets translation join rotation, scaling and the other operations in one 3 × 3 matrix pipeline. It also separates points from directions. A point has w=1, so translation moves it. A direction vector has w=0, so the translation entries contribute nothing to it.

The five matrices to know before composing anything

Fix the convention first: use column vectors, Cartesian axes with positive y upward, and positive angles measured counter-clockwise. In a product, the rightmost matrix acts first.

These are five transformation families: translation adds offsets; scaling multiplies coordinate distances; rotation changes direction around a centre; reflection reverses orientation; shear makes one coordinate depend on the other. The bottom row [0, 0, 1] keeps transformed points in the affine plane. Reading the first two columns also shows where the unit x and y directions land, which is a useful independent sign check for rotation and reflection.

Translation, scaling and rotation are:

T(tx, ty) = [[1, 0, tx], [0, 1, ty], [0, 0, 1]]

S(sx, sy) = [[sx, 0, 0], [0, sy, 0], [0, 0, 1]]

R(θ) = [[cos θ, −sin θ, 0], [sin θ, cos θ, 0], [0, 0, 1]].

Reflection in the x-axis is diag(1,-1,1), reflection in the y-axis is diag(-1,1,1), and reflection in y=x swaps the first two coordinates. The two shear matrices are:

Hx = [[1, shx, 0], [0, 1, 0], [0, 0, 1]]

Hy = [[1, 0, 0], [shy, 1, 0], [0, 0, 1]].

For any transform A about a fixed point F=(f_x,f_y), move the pivot to the origin, apply A, then move back: T(f_x,f_y)A T(-f_x,-f_y).

Worked example: scale, rotate and translate one triangle

Take A=(1,1), B=(4,1) and C=(2,3). First scale by s_x=2 and s_y=0.5 about F=(1,1). Then rotate 90° counter-clockwise about the origin. Finally, translate by (3,-2).

The fixed-point scaling matrix is:

S_F=T(1,1)S(2,0.5)T(-1,-1)

= [[2, 0, −1], [0, 0.5, 0.5], [0, 0, 1]].

Now apply each operation in the stated order:

Point

After scaling about F

After 90° rotation

After translation

A=(1,1)

(1,1)

(-1,1)

A'=(2,-1)

B=(4,1)

(7,1)

(-1,7)

B'=(2,5)

C=(2,3)

(3,2)

(-2,3)

C'=(1,1)

Since A equals the pivot F, fixed-point scaling must leave A unchanged. That is the first sanity check. The scale, rotate and translate columns also isolate an error: if a final answer is wrong, compare them from left to right until the first mismatching stage.

For example, scaling B gives (2(4)-1, 0.5(1)+0.5)=(7,1). Rotation maps (x,y) to (-y,x), so (7,1) becomes (-1,7). Translation then gives (-1+3, 7-2)=(2,5).

One composite matrix checks all three rows:

M=T(3,-2)R(90°)S_F

= [[0, −0.5, 2.5], [2, 0, −3], [0, 0, 1]].

Substituting B=[4, 1, 1]ᵀ gives [0(4)-0.5(1)+2.5, 2(4)+0(1)-3, 1]ᵀ=[2, 5, 1]ᵀ. This direct result matches the three-stage calculation.

Grids tracing triangle ABC through fixed-point scaling, a 90-degree rotation, and translation to A'(2,-1), B'(2,5), C'(1,1).

Why transformation order and pivot choice change the answer

Matrix multiplication is not commutative. Let P=(2,1), R=R(90°) and T=T(3,-2). Rotate first, then translate: (2,1) → (-1,2) → (2,0). Translate first, then rotate: (2,1) → (5,-1) → (1,5). These are TRP and RTP respectively because the rightmost operation acts first.

The pivot matters for the same reason. Rotation around the origin uses R, but rotation around F uses T(F)RT(-F). Mark the pivot before writing the matrix. A correct rotation matrix with the pivot translations missing produces the wrong geometry.

The pivot composite has a clear meaning. T(-F) sends F to the origin, R keeps that origin fixed while rotating the rest, and T(F) restores the original coordinate frame. Omitting either translation changes the centre of motion, even if the angle is correct.

Viewing algorithm: clip, normalise and map a world window

Let the world window be [10,30] × [20,50], the viewport be [100,500] × [200,800], and P=(16,26). The scale factors are:

sx = (500 − 100)/(30 − 10) = 20; sy = (800 − 200)/(50 − 20) = 20.

For a viewport with y upward, u=100+(16-10)20=220 and v=200+(26-20)20=320. If raster coordinate v increases downward, use v=800-(26-20)20=680. Equal scale factors mean that this example introduces no aspect-ratio distortion.

The algorithmic order is: clip primitives to the world window, translate its origin to (0,0), scale to the viewport size, translate to the viewport origin, then flip y only when the display convention requires it.

For a clipping check, take line segment L from (6,26) to (26,46). It crosses the left boundary at x=10. The parameter is t=(10-6)/(26-6)=0.2, giving the intersection (10, 26+0.2(46-26))=(10,30). The visible segment is therefore (10,30) to (26,46). This is the arithmetic a line-clipping algorithm must perform.

Diagram mapping world window point P(16,26) to viewport P'=(220,320) for y-up or (220,680) for y-down, with the segment clipped at (10,30).

How exams turn the concept into questions

Question forms include computing the image of a point or polygon, identifying a matrix, recovering a missing parameter, comparing composition orders, transforming about a fixed point, finding an inverse, mapping a world coordinate into a viewport, and diagnosing a sign or axis-convention error. KnowledgeGate offers over 20 practice questions on this subtopic to rehearse those forms.

NET learners can place Computer Graphics inside a wider revision map with UGC NET Computer Science Syllabus Areas and Course Map.

Traps that produce a plausible-looking wrong answer

  • Mixed conventions: choose row or column vectors once.

  • Reversed order: for column vectors, read ABC right to left.

  • Wrong pivot: translate through a non-origin pivot.

  • Wrong angle mode: match calculator mode to degree or radian input.

  • Screen confusion: counter-clockwise in y-up can look different in y-down.

  • Wrong homogeneous type: directions with w=0 do not translate.

  • Double flip: invert viewport y once, if needed.

  • Early rounding: keep exact coordinates to the raster step.

Before applying a matrix to a full shape, write the convention, mark the pivot, and test one easy point such as the origin or pivot.

The short version and the next study step

Represent points homogeneously. Write each elementary matrix. Move to and from a non-origin pivot. Multiply in right-to-left order. Handle the viewport's y direction explicitly. For the worked triangle, the answer check is A'=(2,-1), B'=(2,5) and C'=(1,1).

Use Zero to Hero: Complete CS Course for a broader, structured Computer Science fundamentals route, or NTA-UGC-NET Paper - 2 if that exam track is your goal. Next, redo the triangle with R(-90°) and verify it with one composite matrix before continuing with the course that matches your plan.