!!html_class cgfs !!html_title Linear Algebra - Computer Graphics from Scratch
This appendix serves as a cheat sheet for linear algebra. The subject is presented as a set of tools, their properties, and what you can use them for. If you're interested in the theory behind all this, you can pick up any introductory linear algebra textbook.
The focus here is exclusively on 2D and 3D algebra, as that's what's required in this book.
A point represents a position within a coordinate system.
We represent a point as a sequence of numbers between parentheses---for example,
Each of the numbers in the point's sequence is called a coordinate. The number of coordinates is the point's dimension. A point with two coordinates is called two-dimensional, or 2D.
The order of the numbers is important;
We can also refer to specific coordinates of a point using a subscript, like
A vector represents the difference between two points. Intuitively, imagine a vector as an arrow that connects a point to another point; alternatively, think of it as the instructions to get from one point to another.
We represent a vector as a set of numbers between parentheses, and refer to them using a capital letter. This is the same representation we use for points, so we add a small arrow on top to remember they're vectors and not points. For example,
Despite sharing their representation with points, vectors don't represent or have a position; they are, after all, the difference between two positions. When you have a diagram like Figure A-2, you have to draw vectors somewhere; but the vectors
In addition, the point
Vectors are characterized by their direction (the angle in which they point) and their magnitude (how long they are).
The direction can be further decomposed into orientation (the slope of the line they're on) and sense (which of the possible two ways along that line they point). For example, a vector pointing right and a vector pointing left both have the same horizontal orientation, but they have the opposite sense. However, we don't make this distinction anywhere in this book.
You can compute the magnitude of a vector from its coordinates. The magnitude is also called the length or norm of the vector. It's denoted by putting the vector between vertical pipes, as in
A vector with a magnitude equal to
Now that we've defined points and vectors, let's explore what we can do with them.
A vector is the difference between two points. In other words, you can subtract two points and get a vector:
In this case, you can think of
Algebraically, you subtract each of the coordinates separately:
We can rewrite the equation above coordinate by coordinate:
These are just numbers, so all the usual rules apply. This means you can do this:
And grouping the coordinates again,
In other words, you can add a vector to a point and get a new point. This makes intuitive and geometric sense; given a starting position (a point) and a displacement (a vector), you end up in a new position (another point). Figure A-4 presents an example.
You can add two vectors. Geometrically, imagine putting one vector "after" another, as in Figure A-5.
As you can see, vector addition is commutative---that is, the order of the operands doesn't matter. In the diagram, we can see that
Algebraically, you add the coordinates individually:
You can multiply a vector by a number. This is called the scalar product. This makes the vector shorter or longer, as you can see in Figure A-6.
If the number is negative, the vector will point the other way; this means it changes its sense and therefore its direction. But multiplying a vector by a number never changes its orientation---that is, it will remain along the same line.
Algebraically, you multiply the coordinates individually:
You can also divide a vector by a number. Just like with numbers, dividing by
One of the applications of vector multiplication and division is to normalize a vector---that is, to turn it into a unit vector. This changes the magnitude of the vector to
You can multiply a vector by another vector. Interestingly, there are many ways in which you can define an operation like this. We're going to focus on two kinds of multiplication that are useful to us: the dot product and the cross product.
The dot product between two vectors (also called the inner product) gives you a number. It's expressed using the dot operator, as in
Algebraically, you multiply the coordinates individually and add them:
Geometrically, the dot product of
Either of these formulas help us see that the dot product is commutative (that is,
An interesting consequence of the second formula is that if
The second formula also suggests the dot product can be used to calculate the angle between two vectors:
Note that the dot product of a vector with itself,
This suggests another way to compute the length of a vector, as the square root of its dot product with itself:
The cross product between two vectors gives you another vector. It's expressed using the cross operator, as in
The cross product of two vectors is a vector perpendicular to both of them. In this book we only use the cross product on 3D vectors, shown in Figure A-7.
{#fig:aa-vector-cross-product}
The computation is a bit more involved than the dot product. If
The cross product is not commutative. Specifically,
We use the cross product to compute the normal vector of a surface---that is, a unit vector perpendicular to the surface. To do this, we take two vectors on the surface, calculate their cross product, and normalize the result.
A matrix is a rectangular array of numbers. For the purposes of this book, matrices represent transformations that can be applied to points or vectors, and we refer to them with a capital letter, such as
A matrix is characterized by its size in terms of rows and columns. For example, this is a
Let's see what we can do with matrices and vectors.
You can add two matrices, as long as they have the same size. The addition is done element by element:
$$\begin{pmatrix} a & b & c \ d & e & f \ g & h & i \end{pmatrix} + \begin{pmatrix} j & k & l \ m & n & o \ p & q & r \end{pmatrix}
\begin{pmatrix} a+j & b+k & c+l \ d+m & e+n & f+o \ g+p & h+q & i+r \end{pmatrix}$$
You can multiply a matrix by a number. You just multiply every element of the matrix by the number:
\begin{pmatrix} n \cdot a & n \cdot b & n \cdot c \ n \cdot d & n \cdot e & n \cdot f \ n \cdot g & n \cdot h & n \cdot i \end{pmatrix}$$
You can multiply two matrices together, as long as their sizes are compatible: the number of columns in the first matrix must be the same as the number of rows in the second matrix. For example, you can multiply a
The result of multiplying two matrices together is another matrix, with the same number of rows as the left-hand side matrix, and the same number of columns as the right-hand side matrix. Continuing with our example above, the result of multiplying a
Let's see how to multiply two matrices, A and B:
To make things more clear, let's group the values in A and B into vectors: let's write A as a column of row (horizontal) vectors and B as a row of column (vertical) vectors. For example, the first row of A is the vector
Let's give names to these vectors:
$$A = \begin{pmatrix}
- \vec{A_0} - \
- \vec{A_1} - \end{pmatrix}$$ $$B = \begin{pmatrix} | & | & | & | \ \vec{B_0} & \vec{B_1} & \vec{B_2} & \vec{B_3} \ | & | & | & | \end{pmatrix}$$
We know that A is
$$\begin{pmatrix}
- \vec{A_0} - \
- \vec{A_1} - \end{pmatrix} \cdot \begin{pmatrix} | & | & | & | \ \vec{B_0} & \vec{B_1} & \vec{B_2} & \vec{B_3} \ | & | & | & | \end{pmatrix} = \begin{pmatrix} c_{00} & c_{01} & c_{02} & c_{03} \ c_{10} & c_{11} & c_{12} & c_{13} \end{pmatrix}$$
Now we can use a simple formulation for the elements of the resulting matrix: the value of the element in row
$$\begin{pmatrix}
- \vec{A_0} - \
- \vec{A_1} - \end{pmatrix} \cdot \begin{pmatrix} | & | & | & | \ \vec{B_0} & \vec{B_1} & \vec{B_2} & \vec{B_3} \ | & | & | & | \end{pmatrix} = \begin{pmatrix} \langle \vec{A_0}, \vec{B_0} \rangle & \langle \vec{A_0}, \vec{B_1} \rangle & \langle \vec{A_0}, \vec{B_2} \rangle & \langle \vec{A_0}, \vec{B_3} \rangle \ \langle \vec{A_1}, \vec{B_0} \rangle & \langle \vec{A_1}, \vec{B_1} \rangle & \langle \vec{A_1}, \vec{B_2} \rangle & \langle \vec{A_1}, \vec{B_3} \rangle \ \end{pmatrix}$$
For example
You can think of an n-dimensional vector as either an
$$\begin{pmatrix} a & b & c \ d & e & f \ \end{pmatrix} \cdot \begin{pmatrix} x \ y \ z \end{pmatrix}
\begin{pmatrix} a \cdot x + b \cdot y + c \cdot z \ d \cdot x + e \cdot y + f \cdot z \end{pmatrix}$$
Since the result of multiplying a matrix and a vector (or a vector and a matrix) is also a vector and, in our case, matrices represent transformations, we can say that the matrix transforms the vector.