Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 50 · 2D Transformations

Translation, Scaling and Rotation

Learn the three basic 2D transformations with formulas and code: move, scale and rotate points, fixed-point scaling, and why order matters.

A drawing that never moves is not very interesting. Characters walk, windows slide, icons grow and wheels turn. In computer graphics all of these effects come from geometric transformations: rules that take every point of an object and compute a new position for it. Because an object is described by its vertices, transforming the object means transforming its vertices, and the lines and fills follow along.

Three basic transformations are enough to build almost any 2D motion: translation (move), scaling (resize) and rotation (turn). This lesson derives each formula, shows how to scale or rotate about a point other than the origin, and demonstrates a trap that catches nearly every beginner: the order in which you combine them matters.

Points and conventions

We write a 2D point as (x, y) and its transformed version as (x', y'), read "x prime, y prime". In the formulas and demos of this lesson the y-axis points up and the origin is the middle of the canvas, the Cartesian convention from coordinate systems. The demos flip y only at the very last moment, when they draw.

Angles are measured in radians in code (Math.sin and Math.cos expect radians). Positive angles rotate counter-clockwise when y points up. If you work directly in screen coordinates with y pointing down, the same formula turns shapes clockwise on the screen. Decide on one convention and keep to it.

Translation

Translation slides every point by the same offset (tx, ty):

TEXT
x' = x + tx
y' = y + ty

Nothing is stretched or turned. Distances, angles, sizes and orientation are all preserved, so we call it a rigid transformation. Translating a polygon by (3, -2) means adding 3 to every x and subtracting 2 from every y.

Scaling

Scaling multiplies each coordinate by a scale factor:

TEXT
x' = x * sx
y' = y * sy

If sx = sy the scaling is uniform and the shape keeps its proportions. If they differ, the shape is stretched in one direction. A factor greater than 1 enlarges, a factor between 0 and 1 shrinks, and a negative factor also mirrors the shape (more on that in reflection, shear and inverse transforms).

There is a catch hidden in the formula. Scaling is relative to the origin. A point 10 units from the origin moves to 20 units when you double the size, so the whole object moves away from the origin as well as growing. Usually you want an object to grow in place instead.

Fixed-point scaling

To scale around a chosen point (xf, yf), called the fixed point, you scale the distance from that point:

TEXT
x' = xf + (x - xf) * sx
y' = yf + (y - yf) * sy

Read it from the inside out: subtract the fixed point (so it becomes the origin), scale, then add the fixed point back. The fixed point itself maps to itself, because x = xf gives x' = xf. Choosing the center of an object as the fixed point makes it grow or shrink in place. You can expand the formula to x' = x*sx + xf*(1 - sx), which shows that fixed-point scaling is plain scaling followed by a translation.

Rotation

Rotation about the origin by an angle θ (theta) turns each point along a circle centered on the origin. Take a point at distance r and angle φ from the x-axis, so x = r cos φ and y = r sin φ. After the rotation it sits at angle φ + θ:

TEXT
x' = r cos(φ + θ) = r cos φ cos θ - r sin φ sin θ
y' = r sin(φ + θ) = r sin φ cos θ + r cos φ sin θ

Replace r cos φ by x and r sin φ by y, and you get the standard rotation formula:

TEXT
x' = x cos θ - y sin θ
y' = x sin θ + y cos θ

Test it with the point (1, 0) and a 90° turn: cos 90° = 0 and sin 90° = 1, so x' = 0 and y' = 1. The point on the x-axis moved to the y-axis, a counter-clockwise quarter turn. Rotation preserves distance from the origin, so it is also rigid.

flowchart LR
  P["Point (x, y)"] --> T["Translate<br/>x + tx, y + ty"]
  P --> S["Scale<br/>x * sx, y * sy"]
  P --> R["Rotate<br/>x cos - y sin, x sin + y cos"]
  T --> Q["New point (x', y')"]
  S --> Q
  R --> Q

Each basic transformation maps a point to a new point; they can be chained one after another.

Rotation about a pivot point

Like scaling, rotation is around the origin. To rotate around a pivot (xp, yp), use the same trick as for fixed-point scaling: move the pivot to the origin, rotate, move back.

TEXT
x' = xp + (x - xp) cos θ - (y - yp) sin θ
y' = yp + (x - xp) sin θ + (y - yp) cos θ

The next demo lets you play with all three transformations on the letter "F" (chosen because it is not symmetric, so you can always tell how it was turned or mirrored). The grey outline is the original. The blue shape is the result of applying, in order, scale, then rotate, then translate, all relative to the origin (the center of the canvas). The log shows how one vertex travels through each stage.

A transformation playground

Try a negative scale factor: the F flips. Set the angle to 90° and watch the F lie on its side. Move the sliders back to the defaults to see what a mild rotate-and-move looks like.

Why order matters

Combining transformations means applying them one after another, feeding the output of one into the next. Unlike adding numbers, this is not commutative: rotating and then translating gives a different result from translating and then rotating.

Think about a small car icon at the origin that should move 5 units to the right and turn 90°.

  • Rotate, then translate: the car turns on the spot, and then drives 5 units to the right. It ends up at (5, 0) facing up.
  • Translate, then rotate: the car first drives to (5, 0), then the whole picture swings around the origin, carrying the car along a circle of radius 5. It ends up at (0, 5).

Same two operations, different place. The same applies to scaling and translation: scaling after a translation also scales the offset.

flowchart TB
  O["Car at the origin"] --> A["Rotate 90°<br/>still at (0, 0)"]
  A --> B["Translate (5, 0)<br/>final: (5, 0)"]
  O --> C["Translate (5, 0)<br/>now at (5, 0)"]
  C --> D["Rotate 90° about origin<br/>final: (0, 5)"]

The two orders end in different places.

The second demo draws the same small triangle both ways, plus a pivot rotation. The grey triangle is the original, the blue one is "rotate then translate", the red one is "translate then rotate", and the green one rotates around its own center by combining a move to the origin, a rotation and a move back.

Order matters, and pivot rotation

The blue triangle points up from (3, 0). The red triangle has swung to the top of the canvas around the origin. The green triangle turned in place, so its center stays where it was before the shift.

Rules of thumb

  • Scaling and rotation happen about the origin. To use another center, move that center to the origin first and move it back afterwards.
  • When you combine steps, write the order down. The next lesson shows how matrices make that order explicit and let you collapse a long chain into one operation.
  • Translation is the odd one out: it adds, while scaling and rotation multiply. That is why translation cannot be written as a 2 by 2 matrix, and why homogeneous coordinates exist.
  • Rotating by -θ undoes a rotation by θ, and scaling by 1/s undoes scaling by s. Translation is undone by -tx, -ty. These inverses are explored in reflection, shear and inverse transforms.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. Which formula rotates the point (x, y) by an angle θ about the origin?

    This is the standard rotation formula. Check with (1, 0) and 90 degrees: it gives (0, 1).
  2. 2. A square centered at (10, 10) is scaled by 2 about the origin. Where does its center end up?

    Scaling about the origin multiplies every coordinate, so the center moves away from the origin as well as the shape growing. To grow in place, use a fixed point at the center.
  3. 3. How do you scale an object about its own center (xf, yf)?

    Moving the center to the origin, scaling, and moving back leaves the center fixed.
  4. 4. A shape is translated by (5, 0) and then rotated 90 degrees about the origin. How does this compare to rotating first and then translating?

    Rotation swings the translated shape around the origin, so the final position differs from rotating on the spot and then moving.
  5. 5. Which of these transformations is NOT rigid, meaning it can change sizes or distances?

    Translation and rotation preserve distances. Scaling by a factor other than 1 (or -1) changes them.

Summary

  • Translation adds an offset, scaling multiplies coordinates, and rotation mixes x and y with sine and cosine.
  • Scaling and rotation act about the origin; for a fixed point or pivot, translate it to the origin, transform, then translate back.
  • Translation and rotation are rigid; scaling is not (unless the factor is 1 or -1).
  • Combining transformations is order-dependent: rotate-then-translate differs from translate-then-rotate.
  • Translation is addition while the others are multiplication, which motivates a unified matrix form.

Next, we unify all three into matrices: Homogeneous Coordinates and Matrix Transformations.