Nov 10 '15
This is an older version of the article. Click here to see the current one.


Matrices are 2D arrays of numbers, grouped as such to enable higher level operations, just like vectors. In fact the columns and rows in matrices are frequently thought of as vectors and constructed as such. They have strong roots in linear algebra, representing linear transformations and solving systems of linear equations.

Matrices in computer graphics, in particular 4×4 homogeneous matrices, are frequently used to represent transformations between different vector spaces. An important one is the projection matrix, used to define virtual cameras. A transformation is a mapping of one coordinate system into another, or finding the coordinates to a point in space from another perspective. For example finding a vertex position in pixels within a rendered image from its object space position in a triangle mesh.

This page introduces matrices used for linear transformations, beginning with rotations and relying heavily on knowledge of vectors, vector spaces, basis vectors and the scalar/dot product.


The matrix multiply is the most used operation and is summarized here. Each new element is the dot product of its position’s row in $A$ with its position’s column in $B$. The operation is non-commutative, i.e. $AB \not= BA$.

$$A \times B = \begin{bmatrix} A_{1, 1} & A_{1, 2} \\ A_{2, 1} & A_{2, 2} \end{bmatrix} \begin{bmatrix} B_{1, 1} & B_{1, 2} \\ B_{2, 1} & B_{2, 2} \end{bmatrix} = \begin{bmatrix} A_{1, 1} B_{1, 1} + A_{1, 2} B_{2, 1} & A_{1, 1} B_{1, 2} + A_{1, 2} B_{2, 2} \\ A_{2, 1} B_{1, 1} + A_{2, 2} B_{2, 1} & A_{2, 1} B_{1, 2} + A_{2, 2} B_{2, 2} \end{bmatrix} $$

The transpose of a matrix flips it along the diagonal, i.e. $A_{x,y}$ becomes $A_{y,x}$:

$$A^\intercal = \begin{bmatrix} A_{1, 1} & A_{2, 1} \\ A_{1, 2} & A_{2, 2} \end{bmatrix}$$

When multiplying a vector by a matrix, it is implicitly transposed to match the matrix multiply operation. Also $v \times A \equiv $A^\top v$. $$A \times v = \begin{bmatrix} A_{1, 1} & A_{1, 2} \\ A_{2, 1} & A_{2, 2} \end{bmatrix} \begin{bmatrix} v_x \\ v_y \end{bmatrix}$$ Others, particularly the [matrix inverse]( $A^{-1}$ are important, but beyond the scope of this page. The inverse of an *orthonormal matrix* (discussed later) is its transpose, which is often a particularly helpful shortcut for avoiding expensive computation. #Rotation Matrices Multiplying a point by a rotation matrix computes its rotated coordinates. The original coordinates are in the point's local space. Then visualizing from the perspective of the new coordinates, the original space is now rotated. Again, rather than imagining a sweeping animated rotation, think of this purely as computing the result --- finding coordinates of points in a new space. A simple example is a 2D 180 degree rotation, as shown below. The different spaces are visualized by drawing their axes, or the basis vectors. ![180 degree rotation example][1] The new coordinates $b$ for a point $a$ are simply $b=(-a_x, -a_y)$. This is easy to see, ignoring the path points follow during the rotation. $b$ is made from a combination of $a$, specifically $-1$ and $-1$ amounts of $a_x$ and $a_y$ respectively. This transformation can be written in matrix form: $$\begin{bmatrix}b_x \\ b_y\end{bmatrix} =\begin{bmatrix}-1 & 0\\0 & -1\end{bmatrix}\begin{bmatrix}a_x \\ a_y\end{bmatrix}$$ Now for a more complex example with an arbitrary rotation. Below, a transform is applied to rotate a vector space $O$ to give $W$. Initially, $O$ is viewed as the frame of reference. Then with the basis vectors of $W$ are added, still relative to $O$, to provide the relation between the two spaces. Finally, $W$ becomes the frame of reference showing the now-rotated space $O$. ![A vector space is transformed by a 30 degree rotation][2] The basis vectors of $W$ in the space of $O$, $W_{x_O}$ and $W_{y_O}$ are known, discussed shortly. [Scalar projection]( can then be used to find $p$ in $W$. The portion of $p$ along each vector $W_{x_O}$ and $W_{y_O}$ provides $p_{W_x}$ (shown) and $p_{W_y}$ respectively: $$p_{W_x} = W_{x_O} \cdot p_O$$ $$p_{W_y} = W_{y_O} \cdot p_O$$ $$p_W = (p_{W_x}, p_{W_y})$$ These dot products can be written as a single matrix multiply, with $X=W_{x_O}$ and $Y=W_{y_O}$. $\overrightarrow{OW}$ denotes a matrix to transform a point in $O$ to a point in $W$. Its inverse is the reverse: $\overrightarrow{OW}^{-1} = \overrightarrow{WO}$. $$p_W =\overrightarrow{OW} p_O = \begin{bmatrix} X_x & X_y \\ Y_x & Y_y \end{bmatrix} \begin{bmatrix} p_{O_x} \\ p_{O_y} \end{bmatrix} $$ This hinges on knowing $W$'s basis vectors in $O$. These provide the relationship between the spaces and *are* the transformation, becoming vectors in the transformation matrix. To construct a rotation matrix which rotates by $\theta$ radians, the basis vectors are generated as follows by computing the [Cartesian coordinates from polar coordinates]( However this matrix needs to create basis vectors in $O$, i.e. that have been transformed by $\overrightarrow{WO}$, so $-\theta$ is used rather than $\theta$. $$X = (\cos(-\theta), \sin(-\theta)) = (\cos(\theta), -\sin(\theta))$$ $$Y = (\cos(-\theta+\frac{\pi}{2}), \sin(-\theta+\frac{\pi}{2})) = (-\sin(-\theta), \cos(-\theta)) = (\sin(\theta), \cos(\theta))$$ $$\overrightarrow{OW}= \begin{bmatrix} \cos(\theta) & -\sin(\theta) \\ \sin(\theta) & \cos(\theta) \end{bmatrix} $$

A purely rotational matrix is orthonormal, being orthographic, where all basis vectors are perpendicular to one another, and of unit length. It can be inverted by taking the transpose.