Rotating points with a matrix
Read these first if they are new to you
- Multiplying a matrix by a vector Basic
- Sine waves Basic
The idea in one paragraph
To turn a shape around the origin, multiply each of its points by a rotation matrix. The matrix for a turn by an angle θ is built from the cosine and sine of that angle. Its columns are where the two axis arrows land after the turn. A rotation keeps every distance and every angle, so the shape keeps its size and form. In 3D the same idea works around an axis, and the axis stays fixed.
The columns are the turned axis arrows
The matrix unit showed that a matrix's columns tell you where (1, 0) and (0, 1) land. So to build a turn, ask where a turn sends those two arrows.
Turn the arrow (1, 0) counterclockwise by an angle θ (the Greek letter theta). Its tip moves along a circle of radius 1 and stops at (cos θ, sin θ). That is what cosine and sine mean: the x and y of a point on that circle. The sine-waves unit explains them. The arrow (0, 1) starts a quarter turn further on than (1, 0), so it ends a quarter turn further on than (cos θ, sin θ). A quarter turn counterclockwise sends any arrow (a, b) to (−b, a): multiply it by the quarter-turn matrix [[0, −1], [1, 0]] to see this. So (0, 1) lands at (−sin θ, cos θ). Put the two landing spots in as columns:
R(θ) = [[cos θ, −sin θ], [sin θ, cos θ]]
The inner brackets are the rows. This is the rotation matrix. Multiply any point by it and the point turns by θ around the origin. A positive angle turns counterclockwise.
The orange tip (0, 2) lands at .
Turn the point (3, 4) by 90°.
- cos 90° = 0 and sin 90° = 1, so R = [[0, −1], [1, 0]].
- Row 1 dotted with (3, 4): 0 × 3 + (−1) × 4 = −4.
- Row 2 dotted with (3, 4): 1 × 3 + 0 × 4 = 3.
- The point lands at (−4, 3). Its distance from the origin was √(9 + 16) = 5, and it is still √(16 + 9) = 5.
Your turn. Turn (2, 0) by 60°. Use cos 60° = 0.5 and sin 60° ≈ 0.866.
Answer
Row 1: 0.5 × 2 + (−0.866) × 0 = 1. Row 2: 0.866 × 2 + 0.5 × 0 = 1.732. The point lands at (1, 1.732), still 2 from the origin.
In 3D, a rotation turns around an axis
In 3D, a turn happens around a line called the axis. Points on the axis stay put. To turn around the z-axis, which points straight up, use the 2D matrix for x and y and leave z alone:
Rz(θ) = [[cos θ, −sin θ, 0], [sin θ, cos θ, 0], [0, 0, 1]]
A point's height does not change; only its position around the axis does. Matrices for turns around the x- and y-axes have the same shape, with the 1 in a different place. For the y-axis the two sines also swap signs:
Ry(θ) = [[cos θ, 0, sin θ], [0, 1, 0], [−sin θ, 0, cos θ]]
Programs that need a shape in many random orientations make one of these matrices from a random angle and multiply every point by it.
Three details are hidden here. First, the sign convention: some fields measure angles clockwise, which swaps
the signs of the sines. Second, in 3D the order of turns matters: turning around x then z
gives a different result from z then x. Third, code that stores points as rows of a
table writes points @ R.T rather than R @ point.
rot.py: NumPy's sin and cos take radians, so convert degrees first
import numpy as np
def rotation(degrees):
t = np.radians(degrees)
return np.array([[np.cos(t), -np.sin(t)],
[np.sin(t), np.cos(t)]])
R = rotation(90)
p = np.array([3.0, 4.0])
q = R @ p
print(np.round(q, 6))
print(np.linalg.norm(p), np.linalg.norm(q)) # same length
print(np.round(rotation(30) @ np.array([1.0, 0.0]), 3))
[-4. 3.] 5.0 5.0 [0.866 0.5 ]
Check yourself
-
Write the rotation matrix for 180°. What does it do to (2, 5)?
Answer
cos 180° = −1 and sin 180° = 0, so R = [[−1, 0], [0, −1]]. It sends (2, 5) to (−2, −5): every point goes to the opposite side of the origin.
-
You turn a shape by 40° and then by −40°. Where does it end up?
Answer
Where it started. The second turn undoes the first.
-
You turn the 3D point (1, 0, 7) by 90° around the z-axis. Where does it land?
Answer
(0, 1, 7). The x and y turn as in 2D, and the height 7 stays the same.
Go further
Optional background, for readers who want more than this page.
-
The 2D formula with its sign conventions; the next section, "In three dimensions," lists the three basic 3D turns.
-
Three-dimensional linear transformations 3Blue1Brown, Essence of linear algebra, chapter 5 Video Basic
A short animation of matrices that turn 3D space, with the "columns are the landing spots" picture.
Where this shows up
Units that build on this one
- Data augmentation: new views of an example Intermediate