MatricesWhat is a Matrix anyway?A matrix can be considered a 2D array of numbers. They take the form: Matrices are very powerful, and form the basis of all modern computer graphics, the advantage of them being that they are so fast. We define a matrix with an upper-case bold type letter. Look at the above example. The dimension of a matrix is its height followed by its width, so the above example has dimension 3x3. Matrices can be of any dimensions, but in terms of computer graphics, they are usually kept to 3x3 or 4x4. There are a few types of special matrices; these are the column matrix, row matrix, square matrix, identity matrix and zero matrix. A column matrix is one that has a width of 1, and a height of greater than 1. A row matrix is a matrix that has a width of greater than 1, and a height of 1. A square matrix is when the dimensions are the same. For instance, the above example is a square matrix, because the width equals the height. The identity matrix is a special type of matrix that has values in the diagonal from top left to bottom right as 1 and the rest as 0. The identity matrix is known by the letter I, where The identity matrix can be any dimension, as long as it is also a square matrix. The zero matrix is a matrix that has all its elements set to 0. The elements of a matrix are all the numbers in it. They are numbered by the row/column position so that : Vectors can also be used in column or row matrices. I will use column matrices here so that it is easier to understand. A 3D vector a in matrix form will use a matrix A with dimension 3x1 so that: which you can see is the same layout as using column vectors. Matrix arithmeticI won't go into every matrix manipulation, but instead I'll focus on the ones that are used extensively in computer graphics. Matrix MultiplicationThere are two ways to multiply a matrix: by a scalar, and by another conformable matrix. First, let's deal with the matrix/scalar multiplication. This is pretty easy, all you do is multiply each element by the scalar. So, let A be the original matrix, B be the matrix after multiplication, and k the constant. We perform: where i and j are the positions in the matrix. this can also be written as: Multiplying a matrix by another matrix is more difficult. First, we need to know if the two matrices are conformable. For a matrix to be conformable with another matrix, the number of rows in A needs to equal the number of columns in B. For instance, take matrix A as having dimension 3x3 and matrix B having dimension 3x2. These two matrices are conformable because the number of rows in A is the same as the number of columns in B. This is important as you'll see later. The product of these two matrices is another matrix with dimension 3x2. So, in general terms: Take three matrices A, B and C where C is the product of A and B. A and B have dimension mxn and pxq respectively. They are conformable if n=p. The matrix C has dimension mxq. You perform the multiplication by multiplying each row in A by each column in B. So let A have dimension 3x3 and B have dimension 3x2.
So, with that in mind, let's try an example:
It's as simple as that! Some things to note: A matrix multiplied by the identity matrix is the same, so: The transpose of a matrix is it flipped on the diagonal from the top left to the bottom right, so for example: The transpose of this matrix would be: Simple enough eh? And you thought it was going to be hard! DeterminantsI'm going to talk a little bit about determinants now, as they are useful for solving certain types of equations. I will discuss easy 2x2 determinants first. Take a 2x2 matrix: The determinant of a matrix A is written |A| and is: For higher dimension matrices, the determinant gets more complicated. Let's discuss a 3x3 matrix. You pass along the first row, and at each element, you discount the row and column that intersects it, and calculate the determinant of the resultant 2x2 matrix multiplied by that value. So, for example, take this 3x3 matrix: Ok then, Step 1: move to the first value in the top row, a11 . Take out the row and column that intersects with that value. Step 2: multiply that determinant by a11. So, using diagrams: Step1: Step2: We repeat this all along the top row, with the sign in front of the value of the top row alternating between a "+" and a "-", so the determinant of A would be: Now, how do we use these for equation solving? Good question. I will first show you how to solve a pair of equations with 2 unknowns. Take the two equations: We first push the coefficients of the variables into a determinant, producing: You can see it's laid out in the same way, which makes it easy. Now, to solve the equation in terms of x, we replace the x coefficients in the determinant with the constants k1 and k2, dividing the result by the original determinant. So, that would be: To solve for y we replace the y coefficients with the constants instead. Let's try an example to see this working: We push the coefficients into a determinant, and solve: To find x substitute constants into x co-efficients, and divide by D: To find y substitute constants into y co-efficients and divide by D: See, it's as simple as that! Just for good measure, I'll do an example using 3 unknowns in 3 equations: Solve for x: Solve for y: Solve for z: And there we have it, how to solve a series of simultaneous equations using determinants, something that can be very useful. Matrix InversionEquations can also be solved by inverting a matrix. Take the following equations again: We push these into 3 matrices to solve: Let's give these names such that: We need to solve for B, and since there is no "matrix divide" operation, we need to invert the matrix A and multiply it by D, such that: Now we need to know how to actually do the matrix inversion. There are many ways to do this, and the way I am going to show you is by no means the fastest. To find the inverse of a matrix, we need to first find its co-factor. We use a method similar to what we used when finding the determinant. What you do is this: at every element, eliminate the row and column that intersects it, and make it equal the determinant of the remaining part of the matrix. Let's find the first element in a 3x3 matrix. Let's call it c11. We need to get rid of the row and column that intersects this, so that: c11 will then take the value of the following determinant: The sign infront of c11 is decided by the expression: Where i and j are the positions of the element in the matrix. That's easy enough isn't it? Thought so J. Just do the same for every element, and build up the co-factor matrix. Done? Good. Now that the co-factor matrix has been found, the inverse matrix can be calculated using the following equation: Taking the previous example and equations, let's find the inverse matrix of A. First, the co-factor matrix C would be: and |A| is: So A-1 is: To solve the equations, we then do: We can then find the values of x,y and z by pulling them out of the last matrix, such that x = -62, y = 39 and z = 3, which is what the other method using determinants found. A matrix is called orthogonal if its inverse equals its transpose. Matrices in computer graphicsAll graphics APIs use a set of matrices to define transformations in space. A transformation is a change, be it translation, rotation, or whatever. Using column a matrix to define a point in space, a vertex, we can define matrices that alter that point in some way. Transformation MatricesMost graphics APIs use 3 different types of primary transformations. These are:
I won't go into the derivation of the matrices for these transformations, as that will take up far too much space. Any good math book that explains affine space transformations will explain their derivations. You have to pre-multiply points by the transformation matrix, as it is impossible to post-multiply because of the dimensions. Therefore, a point p can be transformed to point p' using a transformation matrix T so that: TranslationTo translate a point onto another point, there needs to be a vector of movement, so that where p' is the translated point, p is the original point, and v is the vector along which to translate. In matrix form, this turns into: Where dx, dy and dz are the components of the vector in the respective axis of movement. Note that a 4D vertex is used. These are called homogeneous co-ordinates, BUT I will not discuss them here. ScalingYou can scale a vertex by multiplying it by a scalar value, so that where k is the scalar constant. You can multiply each component of p by a different constant. This will make it so you can scale each axis by a different amount. In matrix form this is: RotationRotation is the most complex transformation. Rotation can be performed around the 3 Cartesian axes. The rotation matrices around these axis are: To find out more about how these matrices are derived, please pick up a good math book, I haven't got the time to write it here. Some things about these matrices though: Any rotation about an axis by q can be undone by a successive rotation by -q. So: Also, notice that the cosine terms are always on the top-left to bottom-right diagonal, and the sine terms are always on the top-right to bottom-left diagonal, we can also say that: Rotations matrices that act about the origin are orthogonal. Note that these transformations are cumulative. That is, if you multiplied a vertex by a translation matrix, then by a scale matrix, it would have the effect of moving the vertex, then scaling it. The order that you multiply becomes very important when you multiply rotation and translation matrices together, as RT does NOT equal TR! Projection MatricesThese are also complicated matrices. They come in two flavours, perspective correct and orthographic. There are some very good books that derive these matrices in an understandable way, so I won't cover it here. Since I don't work with projection matrices very often, I had to look a lot of this material up using the book Interactive Computer Graphics by Edward Angel. A very good book that I suggest you buy. Anyway, on to the matrices. The orthographic projection matrix: The x, y and z max/min variables define the viewing volume. The perspective correct projection matrix is: |
|