# Rotations and rotation matrices#

## Rotations in two dimensions#

In two dimensions, rotating a vector $$\theta$$ around the origin can be expressed as a 2 by 2 transformation matrix:

$\begin{split} R(\theta) = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \\ \end{bmatrix} \end{split}$

This matrix rotates column vectors by matrix multiplication on the left:

$\begin{split} \begin{bmatrix} x' \\ y' \\ \end{bmatrix} = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \\ \end{bmatrix}\begin{bmatrix} x \\ y \\ \end{bmatrix} \end{split}$

The coordinates $$(x',y')$$ of the point $$(x,y)$$ after rotation are:

$\begin{split} x' = x \cos \theta - y \sin \theta \\ y' = x \sin \theta + y \cos \theta \end{split}$

See [rotation in 2D] for a visual proof.

## Rotations in three dimensions#

Rotations in three dimensions extend simply from two dimensions.

Consider a [right-handed] set of x, y, z axes, maybe forming the x axis with your right thumb, the y axis with your index finger, and the z axis with your middle finger. Now look down the z axis, from positive z toward negative z. You see the x and y axes pointing right and up respectively, on a plane in front of you. A rotation around z leaves z unchanged, but changes x and y according to the 2D rotation formula above:

$\begin{split} R_z(\theta) = \begin{bmatrix} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} \end{split}$

For a rotation around x, we look down from positive x to the y and z axes, pointing right and up, respectively. y replaces x in the 2D formula, and z replaces y, to give:

$\begin{split} y' = y \cos \theta - z \sin \theta \\ z' = y \sin \theta + z \cos \theta \end{split}$
$\begin{split} R_x(\theta) = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta \\[3pt] 0 & \sin \theta & \cos \theta \\[3pt] \end{bmatrix} \end{split}$

Now consider a rotation around the y axis. We look from positive y down the y axis to the z and x axes, pointing right and up respectively. $$z$$ replaces $$x$$ in the 2D formula, and $$x$$ replaces $$y$$:

$\begin{split} z' = z \cos \theta - x \sin \theta \\ x' = z \sin \theta + x \cos \theta \end{split}$
$\begin{split} R_y(\theta) = \begin{bmatrix} \cos \theta & 0 & \sin \theta \\[3pt] 0 & 1 & 0 \\[3pt] -\sin \theta & 0 & \cos \theta \\ \end{bmatrix} \end{split}$

We can combine rotations with matrix multiplication. For example, here is an rotation of $$gamma$$ radians around the x axis:

$\begin{split} \begin{bmatrix} x'\\ y'\\ z'\\ \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\gamma) & -\sin(\gamma) \\ 0 & \sin(\gamma) & \cos(\gamma) \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} \end{split}$

We could then apply a rotation of $$phi$$ radians around the y axis:

$\begin{split} \begin{bmatrix} x''\\ y''\\ z''\\ \end{bmatrix} = \begin{bmatrix} \cos(\phi) & 0 & \sin(\phi) \\ 0 & 1 & 0 \\ -\sin(\phi) & 0 & \cos(\phi) \\ \end{bmatrix} \begin{bmatrix} x'\\ j'\\ k'\\ \end{bmatrix} \end{split}$

We could also write the combined rotation as:

$\begin{split} \begin{bmatrix} x''\\ y''\\ z''\\ \end{bmatrix} = \begin{bmatrix} \cos(\phi) & 0 & \sin(\phi) \\ 0 & 1 & 0 \\ -\sin(\phi) & 0 & \cos(\phi) \\ \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\gamma) & -\sin(\gamma) \\ 0 & \sin(\gamma) & \cos(\gamma) \\ \end{bmatrix} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} \end{split}$

Because matrix multiplication is associative:

$\begin{split} \mathbf{Q} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\gamma) & -\sin(\gamma) \\ 0 & \sin(\gamma) & \cos(\gamma) \\ \end{bmatrix} \end{split}$
$\begin{split} \mathbf{P} = \begin{bmatrix} \cos(\phi) & 0 & \sin(\phi) \\ 0 & 1 & 0 \\ -\sin(\phi) & 0 & \cos(\phi) \\ \end{bmatrix} \end{split}$
$\mathbf{M} = \mathbf{P} \cdot \mathbf{Q}$
$\begin{split} \begin{bmatrix} x''\\ y''\\ z''\\ \end{bmatrix} = \mathbf{M} \begin{bmatrix} x\\ y\\ z\\ \end{bmatrix} \end{split}$

$$\mathbf{M}$$ is the rotation matrix that encodes a rotation by $$\gamma$$ radians around the x axis followed by a rotation by $$\phi$$ radians around the y axis. We know that the y axis rotation follows the x axis rotation because matrix multiplication operates from right to left.