## An easy derivation of 3D rotation matrices

Posted by Diego Assencio on 2016.09.23 under Mathematics (Linear algebra)

In this post, we will derive the components of a rotation matrix in three dimensions. Our derivation favors geometrical arguments over a purely algebraic approach and therefore requires only basic knowledge of analytic geometry.

Given a vector ${\bf x} = (x,y,z)$, our goal is to rotate it by an angle $\theta \gt 0$ around a fixed axis represented by a unit vector $\hat{\bf n} = (n_x, n_y, n_z)$; we call ${\bf x}'$ the result of rotating ${\bf x}$ around $\hat{\bf n}$. The rotation is such that if we look into $\hat{\bf n}$, the vector ${\bf x}$ will be rotated along the counter-clockwise direction (see figure 1).

 Fig. 1: The vector ${\bf x}$ is rotated by an angle $\theta$ around $\hat{\bf n}$. Figure (a) shows the components ${\bf x}_{\parallel}$ and ${\bf x}_{\perp}$ of ${\bf x}$ which are parallel and perpendicular to $\hat{\bf n}$ respectively. Figure (b) shows the rotation as seen from top to bottom, i.e., from the perspective of an observer looking into the head of $\hat{\bf n}$: ${\bf x}_{\parallel}$ remains unchanged after the rotation; it is only ${\bf x}_{\perp}$ which changes. The unit vector $\hat{\bf q}$ is parallel to $\hat{\bf n} \times {\bf x} = \hat{\bf n} \times {\bf x}_{\perp}$. The rotation is in the counterclockwise direction for $\theta \gt 0$.

Even though we already anticipated the fact that the transformation which rotates ${\bf x}$ into ${\bf x}'$ can be represented as a matrix, we will prove this explicitly by showing that ${\bf x}' = R(\hat{\bf n},\theta){\bf x}$ for a $3 \times 3$ matrix $R(\hat{\bf n},\theta)$ whose components depend only on $\hat{\bf n}$ and $\theta$.

As a first step, notice that ${\bf x}$ can be decomposed into two components ${\bf x}_{\parallel}$ and ${\bf x}_{\perp}$ which are parallel and perpendicular to $\hat{\bf n}$ respectively as shown in figure 1a. This means: $${\bf x} = {\bf x}_{\parallel} + {\bf x}_{\perp}$$ Since $\hat{\bf n}$ is a unit vector, then: $$\begin{eqnarray} {\bf x}_{\parallel} &=& (\hat{\bf n}\cdot{\bf x})\hat{\bf n} \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_parallel} \\[5pt] {\bf x}_{\perp} &=& {\bf x} - {\bf x}_{\parallel} = {\bf x} - (\hat{\bf n}\cdot{\bf x})\hat{\bf n} \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_perp} \end{eqnarray}$$ When we rotate ${\bf x}$ around $\hat{\bf n}$, its parallel component ${\bf x}_{\parallel}$ remains unchanged; it is only the perpendicular component ${\bf x}_{\perp}$ that actually rotates around $\hat{\bf n}$. This gives us: $${\bf x}_{\parallel}' = {\bf x}_{\parallel} = (\hat{\bf n}\cdot{\bf x})\hat{\bf n} \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_parallel}$$

Let us now define a unit vector $\hat{\bf q}$ which is orthogonal to both $\hat{\bf n}$ and ${\bf x}$ as shown in figure 1b. We can do this by computing and normalizing the cross product of $\hat{\bf n}$ and ${\bf x}$ (below, we implicitly assume that $\hat{\bf n}$ and ${\bf x}$ are not parallel to each other, but if they are, we have trivially that ${\bf x}' = {\bf x} = {\bf x}_{\parallel}$; our final results will be compatible with this corner case as well): $$\displaystyle\hat{\bf q} = \frac{\hat{\bf n} \times {\bf x}}{\|\hat{\bf n} \times {\bf x}\|} \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_def_q}$$ Since a rotation does not change the length of a vector, we have that $\|{\bf x}'\| = \|{\bf x}\|$; in particular, $\|{\bf x}_{\perp}'\| = \|{\bf x}_{\perp}\|$ as shown in figure 1b. When we rotate ${\bf x}_{\perp}$ by an angle $\theta$ around $\hat{\bf n}$, a component proportional to $\|{\bf x}_{\perp}\|\cos\theta$ remains parallel to ${\bf x}_{\perp}$, and a component proportional to $\|{\bf x}_{\perp}\|\sin\theta$ which is parallel to $\hat{\bf q}$ is generated. Therefore: $${\bf x}_{\perp}' = \cos\theta\,{\bf x}_{\perp} + \|{\bf x}_{\perp}\|\sin\theta\,\hat{\bf q} = \cos\theta\,{\bf x}_{\perp} + \sin\theta\,(\hat{\bf n}\times{\bf x}) \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_perp}$$ where above we used the definition of $\hat{\bf q}$ from equation \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_def_q} as well as the fact that: $$\|\hat{\bf n} \times {\bf x}\| = \|\hat{\bf n}\|\|{\bf x}\|\sin\alpha = \|{\bf x}_{\perp}\|$$ with $\alpha$ being the angle between $\hat{\bf n}$ and ${\bf x}$ as shown in figure 1a.

We can now obtain an expression relating ${\bf x}'$ and ${\bf x}$ in terms of $\hat{\bf n}$ and $\theta$. Since ${\bf x}' = {\bf x}_{\parallel}' + {\bf x}_{\perp}'$, and using equations \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_parallel} and \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_perp}, we obtain: $${\bf x}' = (\hat{\bf n}\cdot{\bf x})\hat{\bf n} + \cos\theta\,{\bf x}_{\perp} + \sin\theta\,(\hat{\bf n}\times{\bf x})$$ and now using equation \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_perp}, we get: $${\bf x}' = (\hat{\bf n}\cdot{\bf x})\hat{\bf n} + \cos\theta\,({\bf x} - (\hat{\bf n}\cdot{\bf x})\hat{\bf n}) + \sin\theta\,(\hat{\bf n}\times{\bf x})$$ Rearranging terms, we obtain a very useful expression for computing ${\bf x}'$: $$\boxed{ {\bf x}' = \cos\theta\,{\bf x} + (1 - \cos\theta)(\hat{\bf n}\cdot{\bf x})\hat{\bf n} + \sin\theta\,(\hat{\bf n}\times{\bf x}) } \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_x}$$

As we claimed earlier, equation \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_x} can be expressed as ${\bf x}' = R(\hat{\bf n},\theta){\bf x}$, where $R(\hat{\bf n},\theta)$ is a $3 \times 3$ matrix. To see that this is true, notice that: $$\cos\theta\,{\bf x} = \cos\theta \left( \begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix} \right) \left( \begin{matrix} x \\ y \\ z \end{matrix} \right)$$ and that: $$(\hat{\bf n}\cdot{\bf x})\hat{\bf n} = \left( \begin{matrix} (\hat{\bf n}\cdot{\bf x})n_x \\ (\hat{\bf n}\cdot{\bf x})n_y \\ (\hat{\bf n}\cdot{\bf x})n_z \end{matrix} \right) = \left( \begin{matrix} n_x^2 & n_y n_x & n_z n_x \\ n_x n_y & n_y^2 & n_z n_y \\ n_x n_z & n_y n_z & n_z^2 \end{matrix} \right) \left( \begin{matrix} x \\ y \\ z \end{matrix} \right) \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_mat1}$$ and that: $$\hat{\bf n}\times{\bf x} = \left( \begin{matrix} n_y z - n_z y \\ n_z x - n_x z \\ n_x y - n_y x \end{matrix} \right) = \left( \begin{matrix} 0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0 \end{matrix} \right) \left( \begin{matrix} x \\ y \\ z \end{matrix} \right) \label{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_mat2}$$ Therefore ${\bf x'} = R(\hat{\bf n},\theta){\bf x}$, with: $$\boxed{ \begin{eqnarray} R(\hat{\bf n},\theta) &=& \cos\theta \left( \begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix} \right) + (1 - \cos\theta) \left( \begin{matrix} n_x^2 & n_y n_x & n_z n_x \\ n_x n_y & n_y^2 & n_z n_y \\ n_x n_z & n_y n_z & n_z^2 \end{matrix} \right) \nonumber \\[5pt] &+& \sin\theta \left( \begin{matrix} 0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0 \end{matrix} \right) \nonumber \end{eqnarray} }$$

Whenever $\hat{\bf n}$ and ${\bf x}$ are parallel, we have ${\bf x} = {\bf x}_{\parallel}$ and $\hat{\bf n} \times {\bf x} = {\bf 0}$, so equation \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_prime_x} together with equation \eqref{post_b155574a293a5cbfdd0fbe82a9b8bf28_eq_x_parallel} gives us ${\bf x}' = {\bf x}$, as expected. Additionally, our derivation did not actually rely on the assumption that $\theta \gt 0$, so it is valid for arbitrary values of $\theta$. Finally, notice that by changing $\hat{\bf n} \rightarrow -\hat{\bf n}$ and $\theta \rightarrow -\theta$, $R(\hat{\bf n},\theta)$ does not change, i.e., $R(\hat{\bf n},\theta) = R(-\hat{\bf n},-\theta)$, so we can always convert a rotation with $\theta \lt 0$ into an equivalent one having $\theta \gt 0$ by inverting the direction of $\hat{\bf n}$ and negating $\theta$.