Search

Search papers and lessons

Rotation Matrix

Level 2 · 9 min read

A rotation matrix is a grid of cosines and sines that turns every vector by the same angle while keeping its length and the angles between vectors unchanged.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

TL;DR

A rotation matrix is a small grid of numbers that turns a point or vector around the origin by a fixed angle. Multiply a vector by it and you get the same vector, turned. Lengths stay the same, and so do the angles between any two vectors, so nothing gets stretched or squashed.

Every 2D rotation matrix is built from just two numbers, the cosine and the sine of the angle. Two of them compose by multiplication, and undoing a rotation is as simple as flipping the sign of the angle.

Where you'll see it: rotating sprites and cameras in games, orienting robot arms and drones, and any 3D graphics pipeline that has to spin a model.

Intuition

Suppose you want to spin a flat map by 30 degrees. Every point on the map has to move along a circle around the centre, and the amount it moves depends on how far it started from the centre. A rotation matrix packages that whole motion into one object. You do not track each point separately. You multiply once, and every point in the plane is carried around by the same angle.

The reason it works is that a rotation is determined by where it sends two simple vectors. Send the unit vector along the x-axis to some new spot, and send the unit vector along the y-axis to another spot, and every other vector follows by combining those two. The matrix simply writes down where those two basis vectors land, one per column.

For a counterclockwise rotation by an angle θ\theta, the xx-axis vector lands at (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta) and the yy-axis vector lands at (−sin⁡θ,cos⁡θ)(-\sin\theta, \cos\theta). The plot below shows how those two numbers change as the angle grows. Both are periodic, and they trade off: when one is near its largest value, the other is near zero.

0123456−1−0.500.51angle θ (radians)value
  • cos θ
  • sin θ
The entries of a 2D rotation matrix as functions of the angle. At θ = 0 the matrix is the identity; at θ = 2π it returns to the identity again.

The math

The 2D rotation matrix

For a counterclockwise rotation by angle θ\theta in the plane,

R(θ)=(cos⁡θ−sin⁡θsin⁡θcos⁡θ).R(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}.

Applying it to a vector v=(x,y)\mathbf{v} = (x, y) gives, by the rule for matrix-vector multiplication,

R(θ) v=(xcos⁡θ−ysin⁡θxsin⁡θ+ycos⁡θ).R(\theta)\,\mathbf{v} = \begin{pmatrix} x\cos\theta - y\sin\theta \\ x\sin\theta + y\cos\theta \end{pmatrix}.

The first column of R(θ)R(\theta) is where the x-axis unit vector lands, and the second column is where the y-axis unit vector lands. This is the same column picture described in linear transformations.

Two properties that define a rotation

A rotation matrix RR satisfies

R⊤R=I,det⁡R=1,R^\top R = I, \qquad \det R = 1,

where R⊤R^\top is the transpose (rows and columns swapped) and II is the identity matrix. The first condition says the columns are orthonormal: each has length 1, and they are perpendicular (their dot product is zero). The second, on the Determinant, says the matrix preserves area and orientation, which is what a rotation should do. A reflection also preserves area, but it flips orientation, and its determinant is −1-1. That is why a matrix with det⁡R=−1\det R = -1 is not a rotation.

The first condition gives the inverse for free. Since R⊤R=IR^\top R = I, we have R−1=R⊤R^{-1} = R^\top. Algebra with the cosine and sine shows R(θ)⊤=R(−θ)R(\theta)^\top = R(-\theta), so undoing a rotation is the same as rotating by the negative angle.

Composition adds angles

Rotating by α\alpha and then by β\beta should equal rotating by α+β\alpha + \beta. Matrix multiplication is how we express "one map after another", so

R(β) R(α)=R(α+β).R(\beta)\,R(\alpha) = R(\alpha + \beta).

In 2D the order does not matter: R(α)R(β)=R(β)R(α)R(\alpha)R(\beta) = R(\beta)R(\alpha), because α+β=β+α\alpha + \beta = \beta + \alpha. In three dimensions it is not, which is one of the main surprises when moving from the plane to space.

Rotations in three dimensions

A rotation about the zz-axis by angle θ\theta keeps the zz-coordinate fixed and rotates the xx-yy plane:

Rz(θ)=(cos⁡θ−sin⁡θ0sin⁡θcos⁡θ0001).R_z(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{pmatrix}.

Rotations about the xx- and yy-axes have the same form, placed in different rows and columns. A general 3D rotation can be written as a product of three such matrices, which is the idea behind Euler angles.

Worked examples

Example
Rotate a point by 90 degrees

Take θ=90∘\theta = 90^\circ, so cos⁡θ=0\cos\theta = 0 and sin⁡θ=1\sin\theta = 1.

  1. R(90∘)=(0−110)R(90^\circ) = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}.
  2. Apply it to v=(1,0)\mathbf{v} = (1, 0): the first entry is 0⋅1+(−1)⋅0=00 \cdot 1 + (-1) \cdot 0 = 0, and the second is 1⋅1+0⋅0=11 \cdot 1 + 0 \cdot 0 = 1.
  3. The result is (0,1)(0, 1). The point on the positive xx-axis moved to the positive yy-axis, a quarter turn counterclockwise.
Example
A realistic rotation that keeps the length

Rotate v=(3,1)\mathbf{v} = (3, 1) by θ=30∘\theta = 30^\circ. Here cos⁡30∘≈0.8660\cos 30^\circ \approx 0.8660 and sin⁡30∘=0.5\sin 30^\circ = 0.5.

  1. The rotated x-entry is 3(0.8660)−1(0.5)=2.09813(0.8660) - 1(0.5) = 2.0981.
  2. The rotated y-entry is 3(0.5)+1(0.8660)=2.36603(0.5) + 1(0.8660) = 2.3660.
  3. So R(30∘) v≈(2.0981,2.3660)R(30^\circ)\,\mathbf{v} \approx (2.0981, 2.3660).
  4. Check the length. Before: 32+12=10≈3.1623\sqrt{3^2 + 1^2} = \sqrt{10} \approx 3.1623. After: 2.09812+2.36602≈10.000≈3.1623\sqrt{2.0981^2 + 2.3660^2} \approx \sqrt{10.000} \approx 3.1623. The length is unchanged, as a rotation requires.
  5. Undo it: apply R(−30∘)R(-30^\circ) to (2.0981,2.3660)(2.0981, 2.3660) and you get back (3,1)(3, 1) up to rounding. That is the inverse property R(−θ)=R(θ)−1R(-\theta) = R(\theta)^{-1} at work.
Example
Composing two rotations

Show that two rotations by 45∘45^\circ make one rotation by 90∘90^\circ.

  1. R(45∘)=(0.7071−0.70710.70710.7071)R(45^\circ) = \begin{pmatrix} 0.7071 & -0.7071 \\ 0.7071 & 0.7071 \end{pmatrix}.
  2. Multiply R(45∘) R(45∘)R(45^\circ)\,R(45^\circ). The top-left entry is 0.70712−0.70712=00.7071^2 - 0.7071^2 = 0.
  3. The top-right entry is −0.70712−0.70712=−1-0.7071^2 - 0.7071^2 = -1. The bottom-left is 0.70712+0.70712=10.7071^2 + 0.7071^2 = 1, and the bottom-right is −0.70712+0.70712=0-0.7071^2 + 0.7071^2 = 0.
  4. The product is (0−110)\begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}, which is exactly R(90∘)R(90^\circ) from the first example. Two 45∘45^\circ turns in the same direction give a quarter turn in total, as the formula R(β)R(α)=R(α+β)R(\beta)R(\alpha) = R(\alpha+\beta) predicts.

Tradeoffs and failure modes

A rotation matrix is the right tool when you need to apply one fixed rotation to many points, or to chain rotations with a single product. It is not always the best representation for storing or interpolating rotations.

SituationProblemBetter choice
Repeatedly multiplying many rotationsRounding error builds up, and the matrix slowly stops being orthonormalPeriodically re-orthogonalise the matrix, or store the rotation as a quaternion
Smoothly animating a 3D rotation between two posesInterpolating matrix entries does not give a true rotation in betweenQuaternion slerp (spherical linear interpolation)
Describing 3D orientation with three anglesGimbal lock: at certain orientations two axes align and one degree of freedom is lostQuaternions, or a full rotation matrix
Rotating around a point other than the originA plain 2×22 \times 2 rotation always pivots around (0,0)(0,0)Translate to the origin, rotate, translate back; or use a homogeneous 3×33 \times 3 matrix
Screen coordinates where yy points downPositive angles look clockwise on the screenKeep the same matrix but know the convention, or flip the sign of θ\theta

Two practical warnings. First, a matrix with determinant −1-1 looks like a rotation but reflects, so check det⁡R=1\det R = 1 when you load matrices from outside. Second, in code, angles are almost always in radians, while people often think in degrees. Passing 3030 where the function expects radians rotates by about 1719 degrees (30 radians), which is a common bug.

History

Leonhard Euler showed in the 1770s that any rotation of three-dimensional space is a rotation about some single axis, and he developed the angle-based description that now bears his name. The matrix form came later, once matrix algebra was being formalised. Arthur Cayley and others in the 1850s treated matrices as objects in their own right, which made it natural to write a rotation as a single array of numbers.

William Rowan Hamilton's quaternions, introduced in 1843, gave a different and very compact way to describe 3D rotations. Quaternions avoid gimbal lock and interpolate smoothly, and they are still standard in aerospace and computer graphics. Rotation matrices stayed the everyday tool for physics and engineering, and they became ubiquitous when computer graphics and robotics needed to move objects in space many times per second.

Check yourself

QuestionWrite the 2D rotation matrix for an angle θ\theta, and say which way it turns vectors when θ\theta is positive.Show answer

R(θ)=(cos⁡θ−sin⁡θsin⁡θcos⁡θ)R(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}. With θ\theta positive, it turns vectors counterclockwise in the standard xx-yy orientation.

QuestionRotate the vector (2,0)(2, 0) by 60∘60^\circ counterclockwise. What do you get?Show answer

Using cos⁡60∘=0.5\cos 60^\circ = 0.5 and sin⁡60∘≈0.8660\sin 60^\circ \approx 0.8660: the x-entry is 2(0.5)−0(0.8660)=12(0.5) - 0(0.8660) = 1, and the y-entry is 2(0.8660)+0(0.5)=1.73212(0.8660) + 0(0.5) = 1.7321. The result is (1,3)(1, \sqrt{3}). The length is 2, as expected.

QuestionWhy is the inverse of a rotation matrix equal to its transpose, and what property of the columns guarantees this?Show answer

The columns of a rotation matrix are orthonormal: each has length 1 and they are perpendicular. That means R⊤R=IR^\top R = I, because the dot product of column ii with column jj is 1 when i=ji = j and 0 otherwise. Multiplying R⊤R=IR^\top R = I on the right by R−1R^{-1} gives R⊤=R−1R^\top = R^{-1}.

QuestionA sprite on a game screen should turn to face a new direction by a positive angle θ\theta. The screen's yy-axis points down. Will a standard rotation matrix with that θ\theta turn the sprite clockwise or counterclockwise as seen on screen, and what would you change?Show answer

The matrix is the same, and the mathematical rotation is counterclockwise in xx-yy coordinates. Because yy points down, that appears clockwise on screen. To get the visual direction you want, either negate the angle or flip the sign of the yy-axis when you convert between world and screen coordinates. Pick one convention and keep it consistent across the code.

Sources