The rotation matrix, built from scratch
Every robot, game engine and spacecraft turns things with the same four entries: cos, −sin, sin, cos. Where does that minus sign come from?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
Rotations are the most important matrices in robotics. Every joint, every turn of a vehicle and every camera orientation is a rotation. Deriving R(θ) yourself, once, means you'll never have to memorise where the minus sign goes: you can rebuild it in ten seconds from a sketch.
- ◇ Derive the 2D rotation matrix from where it sends (1, 0) and (0, 1).
- ◇ Rotate a point by a given angle about the origin.
- ◇ Show that a rotation keeps lengths and has determinant 1.
2.Predict
Rotate (1, 0) by 90° anticlockwise. Where does it go?
Guess first. Being wrong here is how the surprise works.
3.Discover
Choose Rotate. Set θ = 90°, then 30°, then −45°. Each time, read where the orange (1, 0) arrow lands and check it is (cos θ, sin θ). Where does the green (0, 1) arrow land? Check |P| = |P′| and det = 1 every time.
Loading the interactive…
Can't use the interactive? Read a description instead
With the Rotate preset the grid turns rigidly by θ. At 90° the orange arrow (image of (1, 0)) points straight up to (0, 1) and the green arrow (image of (0, 1)) points left to (−1, 0). The readout shows |P| equal to |P′| and determinant 1 for every angle.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Which matrix is R(90°)?
5.Watch
→ Watch for the trick: follow just the two basis arrows, then read off the matrix. Pause when the rotation example appears and predict its entries.
Shows a matrix as a description of where the basis vectors land, so you can read a transformation straight off its columns.
10 min 58 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
6.Understand
Using R(θ)
To rotate a point about the origin by θ (anticlockwise positive): x′ = x cos θ − y sin θ, y′ = x sin θ + y cos θ. For a clockwise rotation, use a negative θ: cos stays the same, sin changes sign. To rotate about some other pivot, shift the pivot to the origin, rotate, then shift back.
What makes it a rotation
Its columns are unit length and perpendicular to each other, so lengths and angles are kept (it is 'orthogonal'), and det = 1, so it doesn't mirror. A matrix with orthonormal columns but det = −1 is a reflection, not a rotation.
Same matrix, two stories
R(θ)·p can mean 'turn the point p by θ' or 'p is given in a frame turned by θ; here it is in the outer frame'. Both are the same calculation. Robotics uses the second story constantly: a link frame rotated by θ1 turns the link's own coordinates into the base frame's.
Explain it another way
Stick a drawing pin through the origin of a sheet of tracing paper and turn the sheet. Everything moves together, nothing stretches. The rotation matrix is the recipe for where each mark ends up.
R(θ) ∈ SO(2), the group of 2×2 orthogonal matrices with determinant 1. Rᵀ = R⁻¹ = R(−θ), and R(α)R(β) = R(α + β): planar rotations commute.
Rotate (2, −5) by 135°: cos = −0.7071, sin = 0.7071. x′ = 2(−0.7071) − (−5)(0.7071) = −1.414 + 3.536 = 2.121. y′ = 2(0.7071) + (−5)(−0.7071) = 1.414 + 3.536 = 4.950.
A camera on a robot's wrist sees a part at (0.1, 0) m in its own frame. With the wrist turned 30°, the part is at R(30°)·(0.1, 0) = (0.0866, 0.05) m relative to the wrist in the arm's frame.
Derivation: Deriving R(θ)
A matrix is fixed by where it sends (1, 0) and (0, 1): those images are its columns.
Rotating (1, 0) by θ lands on the unit circle at angle θ: (cos θ, sin θ). That's column 1.
(0, 1) starts at 90°, so after rotating it is at θ + 90°: (cos(θ + 90°), sin(θ + 90°)) = (−sin θ, cos θ). That's column 2, and the source of the minus sign.
So R(θ) = [cos θ −sin θ; sin θ cos θ], and R(θ)·(x, y) = (x cos θ − y sin θ, x sin θ + y cos θ).
Check lengths: (x cos θ − y sin θ)² + (x sin θ + y cos θ)² = x²(cos² + sin²) + y²(sin² + cos²) + (cross terms that cancel) = x² + y². Lengths are kept.
Check area: det R = cos θ · cos θ − (−sin θ) · sin θ = cos²θ + sin²θ = 1. No stretching, no flipping.
Worked example 1
Rotate (3, 4) by 90° clockwise.
θ = −90°: cos = 0, sin = −1. x′ = 3×0 − 4×(−1) = 4, y′ = 3×(−1) + 4×0 = −3. (4, −3): length still 5.
Common mix-ups
- The minus sign in R(θ) goes on the bottom-left entry.
✓ It goes top-right: column 2 is where (0, 1) lands, (−sin θ, cos θ). Rebuild it from a sketch if unsure. - A rotation matrix rotates about any point you like.
✓ It rotates about the origin. For another pivot, translate, rotate, translate back (or use a homogeneous transform).
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Rotate the point (−4, 4) by 60° about the origin (positive = anticlockwise). What is the new x coordinate? (3 decimal places)
Practice 3
The point (5, 12) is rotated by 73°. What is its distance from the origin afterwards?
Practice 4
[1 0; 0 −1] has unit, perpendicular columns. Why isn't it a rotation?
Practice 5
Rotate (3, 1) by 90° about the pivot (2, 1) (not the origin). What is the new y coordinate?
8.Apply it
Write a Python function rotate(x, y, deg) using the formula. Test that rotating by 30° twelve times returns (very nearly) the starting point, and explain the tiny difference you see.
9.Reflect
10.Go further
- Rotation matrix (Wikipedia) ↗2D and 3D rotation matrices and their properties.
Concepts: Matrices and linear transformations · Angles and trigonometry