Rotation Matrix: 2D & 3D Formula, Derivation, Examples

Rotation Matrix: 2D & 3D Formula, Derivation, Examples

TL;DR

A rotation matrix is a square matrix that rotates a vector around the origin while keeping its length unchanged. This article covers the 2D formula [\cos \theta \ -\sin \theta \ \sin \theta \ \cos \theta]\begin{bmatrix} \cos \theta & -\sin \theta \ \sin \theta & \cos \theta \end{bmatrix} [\cos \theta \ -\sin \theta \sin \theta \ \cos \theta], the 3D axis rotations, where the formula comes from, the orthogonal-with-determinant-1 properties, and the sign mistakes that cost marks.

When a Robot Arm Misses by a Hand's Width

A robot arm that misses its target by a hand's width usually has one sign wrong. Inside that arm, a rotation matrix decides where the gripper ends up, and a single flipped sign rotates it the wrong way, missing the target by the full sweep of its reach. The same matrix steadies the camera in your phone, spins a character in a video game, and points a satellite dish. It is a small grid of sines and cosines doing a precise, repeatable job: turning a direction into a new direction.

What a Rotation Matrix Is

A rotation matrix is a square matrix that, when multiplied with the coordinates of a vector, rotates that vector by a fixed angle about the origin without changing its length or shape. In two dimensions, it is a 2×2 matrix; in three dimensions, it is a 3×3 matrix. The standard counterclockwise rotation matrix in the plane is

[R(\theta) = \begin{bmatrix} \cos \theta & -\sin \theta \ \sin \theta & \cos \theta \end{bmatrix}]

To rotate a vector, you place its coordinates in a column and multiply on the left by (R(\theta)). If (\mathbf{v} = \begin{bmatrix} x \ y \end{bmatrix}), then the rotated vector is

[R(\theta), \mathbf{v} = \begin{bmatrix} \cos \theta & -\sin \theta \ \sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} x \ y \end{bmatrix} = \begin{bmatrix} x \cos \theta - y \sin \theta \ x \sin \theta + y \cos \theta \end{bmatrix}]

Here (\theta) is the angle of rotation, measured counterclockwise from the positive x-axis, and the origin stays fixed. A rotation matrix is one specific kind of transformation matrix — the kind that turns without stretching.

How Do You Derive the 2D Rotation Matrix?

The formula is not handed down from nowhere. It comes from one idea: write a point in polar form, add the rotation angle, and expand.

Start with a point at distance (r) from the origin, sitting at angle (\varphi). Its coordinates are

[\begin{align*} x & = r \cos \varphi, \ y & = r \sin \varphi \end{align*}]

Now rotate it counterclockwise by (\theta). The distance does not change; only the angle does, becoming (\varphi + \theta). So the new coordinates are:

[\begin{align*} x' & = r \cos(\varphi + \theta), \ y' & = r \sin(\varphi + \theta) \end{align*}]

Apply the angle-addition identities:

[\begin{align*} x' & = r (\cos \varphi \cos \theta - \sin \varphi \sin \theta), \ y' & = r (\sin \varphi \cos \theta + \cos \varphi \sin \theta) \end{align*}]

Substitute back (x = r \cos \varphi) and (y = r \sin \varphi):

[\begin{align*} x' & = x \cos \theta - y \sin \theta, \ y' & = x \sin \theta + y \cos \theta \end{align*}]

This results in the rotation matrix:[R(\theta)]. The derivation demonstrates why (r) never appears in the final matrix: rotation preserves length, so the radius drops out.

Variable glossary. (\theta) is the rotation angle (counterclockwise positive); (\varphi) is the point's original angle from the positive x-axis; (r) is its distance from the origin; (x, y) are the original coordinates; (x', y') are the rotated coordinates.

The 3D Rotation Matrices

In three dimensions, a single rotation happens about an axis. Each coordinate axis gives its own matrix, and the axis you rotate about stays put while the other two coordinates mix exactly like the 2D case.

Rotation about the x-axis (by angle (\alpha)):

[R_x(\alpha) = \begin{bmatrix} 1 & 0 & 0 \ 0 & \cos \alpha & -\sin \alpha \ 0 & \sin \alpha & \cos \alpha \end{bmatrix}]

Rotation about the y-axis (by angle (\beta)):

[R_y(\beta) = \begin{bmatrix} \cos \beta & 0 & \sin \beta \ 0 & 1 & 0 \ -\sin \beta & 0 & \cos \beta \end{bmatrix}]

Rotation about the z-axis (by angle (\gamma)):

[R_z(\gamma) = \begin{bmatrix} \cos \gamma & -\sin \gamma & 0 \ \sin \gamma & \cos \gamma & 0 \ 0 & 0 & 1 \end{bmatrix}]

Notice the y-axis matrix has its signs arranged differently. That is not a typo. The way the axes are ordered (x, then y, then z, cycling back) forces the sine signs to flip for the middle axis. A general 3D orientation is built by multiplying these three together, and because matrix multiplication is not commutative, the order in which you apply them matters.

What Are the Properties of a Rotation Matrix?

Two properties define every rotation matrix, and together they are what separate a rotation from a general transformation.

A useful consequence: rotating by (\theta) and then by (-\theta) returns you to the start, because (R(-\theta) = R(\theta)^{-1} = R(\theta)^{T}). The inverse of a rotation is just the rotation backward.

Examples of Rotation Matrix

Example 1

Rotate the point (1,0) counterclockwise by 90°: At (\theta=90°), (\cos 90°=0) and (\sin 90°=1), so [R(90°) = \begin{bmatrix} 0 & -1 \ 1 & 0 \end{bmatrix}] Multiply: [\begin{bmatrix} 0 & -1 \ 1 & 0 \end{bmatrix} \begin{bmatrix} 1 \ 0 \end{bmatrix} = \begin{bmatrix} 0 \ 1 \end{bmatrix}] The point on the positive x-axis lands on the positive y-axis. Final answer:(0,1).

Example 2

Rotate the point (3,1) clockwise by 30°: Wrong attempt. A student reaches for the standard matrix with (\theta=30°): This rotates counterclockwise, the wrong direction. The result drifts up and to the left when the point should swing down and to the right. Correct. Use (\theta=-30°):[R(-30°) = \begin{bmatrix} \frac{\sqrt{3}}{2} & \frac{1}{2} \ -\frac{1}{2} & \frac{\sqrt{3}}{2} \end{bmatrix}] Multiply: [\begin{bmatrix} \frac{\sqrt{3}}{2} & \frac{1}{2} \ -\frac{1}{2} & \frac{\sqrt{3}}{2} \end{bmatrix} \begin{bmatrix} 3 \ 1 \end{bmatrix}\approx \begin{bmatrix} 3.10 \ -0.63 \end{bmatrix}] Final answer: approximately (3.10,-0.63).

Example 3

Rotate (2,2) counterclockwise by 45°: With (\cos 45°=\sin 45°=\frac{\sqrt{2}}{2}): [R(45°) = \begin{bmatrix} \frac{\sqrt{2}}{2} & -\frac{\sqrt{2}}{2} \ \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} \end{bmatrix}] Multiply: [\begin{bmatrix} \frac{\sqrt{2}}{2} & -\frac{\sqrt{2}}{2} \ \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} \end{bmatrix} \begin{bmatrix} 2 \ 2 \end{bmatrix} = \begin{bmatrix} 0 \ 2\sqrt{2} \end{bmatrix}] Final answer:(0,2√2).

Example 4

Rotate the 3D point (5,2,6) by 180° about the x-axis: At (\alpha=180°):
[R_x(180°) = \begin{bmatrix} 1 & 0 & 0 \ 0 & -1 & 0 \ 0 & 0 & -1 \end{bmatrix}] Multiply: [\begin{bmatrix} 1 & 0 & 0 \ 0 & -1 & 0 \ 0 & 0 & -1 \end{bmatrix} \begin{bmatrix} 5 \ 2 \ 6 \end{bmatrix} = \begin{bmatrix} 5 \ -2 \ -6 \end{bmatrix}] Final answer:(5,-2,-6).

Example 5

Show two successive rotations add:[R(\theta_1)R(\theta_2) = R(\theta_1 + \theta_2)] using (\theta_1=\theta_2=30°):[R(30°)R(30°)=R(60°)] Final answer: the matrices are equal; rotation angles add under multiplication.

Example 6

A vector of length 4 sits at 50°. Where does it point after a 40° counterclockwise rotation? A rotation never changes length, so the magnitude stays at 4. The angle becomes 90°, so the vector points straight up the y-axis. Final answer:(0,4).

Why Rotation Matrices Matter — "turning a direction into a direction"

The reason rotation matrices exist is older and more practical than any classroom: people needed a repeatable, exact way to describe turning. A drawing of an arrow swinging through an angle is fine for one case, but a machine, a renderer, or a navigation system needs to turn thousands of points the same way, instantly. The matrix packages that turn into arithmetic.

Tripping Points to Avoid

Mistake 1: Flipping the sign of the wrong sine

Don't do this: Putting the negative sign on the bottom-left (\sin \theta) instead of the top-right one. The correct way: Anchor on one cell. The standard counterclockwise matrix has (-\sin \theta) in the top-right. Once that is right, the other sine is positive.

Mistake 2: Confusing clockwise and counterclockwise

Don't do this: Plugging a positive angle into the standard matrix for a clockwise turn. The correct way: Clockwise means a negative angle.

Mistake 3: Multiplying 3D rotations in the wrong order

Don't do this: Assuming (R_x R_y = R_y R_x). It does not — rotation matrices do not commute. The correct way: Apply rotations in the stated order, right to left.

Key Takeaways

Practice Questions on Rotation Matrix

  1. Rotate the point (0,5) counterclockwise by 90°.
  2. Rotate the point (1,1) by -45° (clockwise by 45°).
  3. Confirm that (R(60°)R(-60°) = I).
  4. Rotate the 3D point (4,0,1) by 90° about the z-axis.
  5. A vector of length 3 sits at 20°. Where does it point after a 70° counterclockwise rotation?

Frequently Asked Questions

  1. Is a rotation matrix always a square matrix? Yes.
  2. Why is the determinant of a rotation matrix equal to 1? Because a rotation preserves area and orientation.
  3. What is the inverse of a rotation matrix? It is the rotation by the opposite angle.
  4. Does the order matter when combining 3D rotations? Yes.
  5. Can a rotation matrix change the size of a vector? No.