Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 62 ยท Linear Transformations

Rotation, Scaling, Shear and Reflection

Build the standard 2D matrices for rotation, scaling, shear and reflection, compose them in the right order, extend to 3D rotations and plot with matplotlib.

Four families of linear maps do most of the geometric work in graphics, games and data visualization: rotation, scaling, shear and reflection. Each has a short, memorable matrix. If you know where the basis vectors e1 = (1, 0) and e2 = (0, 1) go, you can write any of them from scratch, exactly as you learned in Linear Transformations.

This lesson builds each matrix, shows how to chain them (and why the order matters), extends rotation to three dimensions and plots the results. All matrices here act on column vectors, so a point is written (x, y) as a column and the matrix goes on the left.

Four building blocks

Rotation Turns space by an angle. Lengths stay.
Scaling Stretches or shrinks along the axes.
Shear Slides layers sideways. Area stays.
Reflection Flips across a line through the origin.

Every one fixes the origin.

1 / 2

Scaling

A scaling matrix is diagonal. The entry sx stretches the x-direction and sy the y-direction:

TEXT
S = [ sx   0 ]
    [  0  sy ]

The first column is (sx, 0), the new home of e1; the second is (0, sy). With sx = sy the scaling is uniform and keeps shapes similar. A negative entry flips that axis, which makes it a reflection too. Setting an entry to 0 collapses a whole direction, and the matrix loses rank (see Rank).

Rotation

A counterclockwise rotation by angle theta sends e1 to (cos theta, sin theta) and e2 to (-sin theta, cos theta). Those are the columns:

TEXT
R(theta) = [ cos theta  -sin theta ]
           [ sin theta   cos theta ]

Rotations preserve lengths and angles, and their determinant is 1. They are orthogonal matrices: R^T R = I, so the inverse of a rotation is simply its transpose, which equals a rotation by -theta.

Shear

A shear slides each point sideways by an amount proportional to its other coordinate. The horizontal shear by factor k is

TEXT
H = [ 1  k ]
    [ 0  1 ]

It keeps e1 where it is and tilts e2 to (k, 1). Rectangles become parallelograms, but areas do not change (the determinant is 1). Italic text is a shear of upright text.

Reflection

A reflection flips space across a line through the origin. The simplest ones use the axes:

  • across the x-axis: [[1, 0], [0, -1]],
  • across the y-axis: [[-1, 0], [0, 1]],
  • across the line y = x: [[0, 1], [1, 0]], which swaps the coordinates.

A reflection has determinant -1: it reverses orientation (a clockwise shape becomes counterclockwise) while keeping lengths. Reflecting across a line at angle phi has matrix [[cos 2phi, sin 2phi], [sin 2phi, -cos 2phi]].

Python Four matrices, one function each

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The determinants tell you what each family does to area: rotation and shear have 1 (area kept), scaling by (2, 3) gives 6 (area times six), and the reflection gives -1 (area kept, orientation flipped). The 90-degree rotation of e1 lands on (0, 1), as the column picture predicts, after rounding away tiny floating-point leftovers.

Seeing all four on one shape

A shape that is not symmetric shows rotations, flips and shears clearly. The letter F is a classic: you can immediately tell whether it was turned or mirrored.

Python The letter F under four transformations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

One matrix product M @ F moves every point at once, because the points are stored as columns. Notice how the reflected panels are mirror images that no rotation could produce, while the sheared panel keeps the height of the letter but leans it to the right.

Composition and order

To apply several transformations in sequence, multiply their matrices. The matrix closest to the vector acts first. So B @ A @ v applies A, then B.

Order matters because matrix multiplication is not commutative. Stretching a shape along x and then rotating it is not the same as rotating it first and then stretching along x: the stretch always acts along the fixed x-axis of the screen, so it hits a turned shape from a different side than an upright one.

Python Rotate then scale is not scale then rotate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Rotating (1, 0) by 90 degrees gives (0, 1), and scaling x by 3 then leaves it alone. Scaling first gives (3, 0), which the rotation turns into (0, 3). Different answers from the same two ingredients. There are special cases where order does not matter, such as two rotations about the same point in the plane, whose angles simply add.

Rotations in 3D

In three dimensions you rotate about an axis. The rotation about the z-axis leaves e3 fixed and rotates the xy-plane:

TEXT
Rz(a) = [ cos a  -sin a  0 ]
        [ sin a   cos a  0 ]
        [   0       0    1 ]

The matrices for the x-axis and y-axis follow the same pattern, with the "1" in the axis's own position. Every 3D rotation is orthogonal with determinant 1. Unlike 2D, 3D rotations about different axes do not commute: tilting a book forward and then turning it left is not the same as turning it left and then tilting it forward.

Python Rotations about axes in 3D

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first two lines show the rule "the axis you rotate about stays fixed, the others turn". The product M of three rotations is still a rotation: orthogonal, with determinant 1. And the last lines show that 3D order matters: the vector e1 ends up in two different places depending on the order. This is why graphics code keeps careful track of the order of its rotation matrices.

Rotations, scalings, shears and reflections all leave the origin fixed. A plain shift of the whole picture, called a translation, is not linear. The next lesson, Homogeneous Coordinates and Translation, shows how an extra coordinate lets matrices handle it too.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the matrix of a counterclockwise rotation by 90 degrees in the plane?

    The columns are the images of e1 and e2. A quarter turn sends e1 to (0, 1) and e2 to (-1, 0), giving columns (0, 1) and (-1, 0), which is [[0, -1], [1, 0]].
  2. 2. Which transformation has determinant -1 and keeps lengths?

    A reflection keeps lengths but flips orientation, which shows up as determinant -1. Rotations and shears have determinant 1, and scaling by 2 multiplies area by 4.
  3. 3. In the product C @ B @ A @ v, which matrix acts on v first?

    The matrix next to the vector is applied first, so A acts first, then B, then C.
  4. 4. Which pair of transformations always gives the same result in either order?

    Rotations about the same point in the plane commute because their angles just add. The other pairs generally give different results when swapped.

Summary

  • Scaling is diagonal; rotation is [[cos, -sin], [sin, cos]]; shear is [[1, k], [0, 1]]; reflections have determinant -1.
  • The columns of each matrix show where e1 and e2 land, so you can rebuild any of them quickly.
  • Rotations are orthogonal: R^T R = I, determinant 1, and the inverse is the transpose.
  • Compose by multiplying, with the first transformation on the right; the order changes the result.
  • In 3D, rotations about different axes do not commute.

Next, learn how to include translations in the matrix framework with Homogeneous Coordinates and Translation.