*In which you learn how a rotation matrix is structured.*

Earlier you saw that rotation around the z-axis could be expressed with these dot products:

$$ \begin{aligned} p'_x &= \begin{bmatrix}\cos a & -\sin a & 0\end{bmatrix} \cdot \mathbf{p} \\ p'_y &= \begin{bmatrix}\sin a & \cos a & 0\end{bmatrix} \cdot \mathbf{p} \\ p'_z &= \begin{bmatrix}0 & 0 & 1\end{bmatrix} \cdot \mathbf{p} \end{aligned} $$

As with translation and scaling, you add a 0 to the vectors to cancel the homogeneous coordinate of \(\mathbf{p}\), and you also add a fourth dot product to produce the homogeneous coordinate of \(\mathbf{p'}\).

All four dot products will be evaluated simultaneously when you assemble the vectors into this matrix:

\begin{aligned} \begin{bmatrix} \cos a & -\sin a & 0 & 0 \\ \sin a & \cos a & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}

Rotating around the z-axis now becomes a matrix-vector multiplication, just like translation and scaling:

\begin{aligned} \begin{bmatrix}p'_x \\ p'_y \\ p'_z \\ 1\end{bmatrix} &= \begin{bmatrix} \cos a & -\sin a & 0 & 0 \\ \sin a & \cos a & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \times \begin{bmatrix}p_x \\ p_y \\ p_z \\ 1\end{bmatrix} \\ \end{aligned}

Rotating around the z-axis is the only kind of rotation you care about when are operating in just two dimensions. However, when you move into the third dimension, you have many more axes around which you may rotate.

Suppose you want to rotate around the x-axis. The matrix is very similar to the one for rotating around the z-axis:

\begin{aligned} \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos a & -\sin a & 0 \\ 0 & \sin a & \cos a & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}

The matrix for rotating around the y-axis is also similar:

\begin{aligned} \begin{bmatrix} \cos a & 0 & -\sin a & 0 \\ 0 & 1 & 0 & 0 \\ \sin a & 0 & \cos a & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{aligned}

Notice how the sines and cosines follow the same rectangular arrangement in all three matrices. That's because they are constructed using the same polar coordinate logic you applied for rotating around z. The only difference is that you are looking at the problem from a different axis.

In each matrix, one row serves as the identity. When rotating around one of the cardinal axes, only two of the coordinates change:

- When rotating an object around the z-axis, its z-coordinates never change. The matrix's third row is the vector \(\begin{bmatrix}0&0&1&0\end{bmatrix}\). Dotting this with \(\mathbf{p}\) yields \(p_z\).
- When rotating an object around the y-axis, its y-coordinates never change. The matrix's second row is the vector \(\begin{bmatrix}0&1&0&0\end{bmatrix}\). Dotting this with \(\mathbf{p}\) yields \(p_y\).
- When rotating an object around the x-axis, its x-coordinates never change. The matrix's first row is the vector \(\begin{bmatrix}1&0&0&0\end{bmatrix}\). Dotting this with \(\mathbf{p}\) yields \(p_x\).

You can also rotate around non-cardinal axes. That's a more complex operation that you'll get to later.