Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### The Identity Matrix

The first thing you need to define in any mathematical system is 1 and 0. In matrix mathematics, there are analogs of both of the values. The analog of 1 is called the identity matrix and is created by placing all 1s in the main diagonal of the matrix and 0s everywhere else. Furthermore, because matrices can be any size, there are obviously an infinite number of identity matrices. However, there is one constraint: All identity matrices must be square, or in other words m×m, where m >= 1. Here are a couple of examples:

    I2 = |1 0|

         |0 1|

    Dimension 2x2

         |1 0 0 |

    I3 = |0 1 0 |

         |0 0 1 |

    Dimension 3x3

Ironically, the identity matrix isn’t exactly the analog of 1, but is under matrix multiplication (which we’ll get to in a second).

The second type of fundamental matrix is called the zero matrix, and it’s 0 under both addition and multiplication. It’s nothing more than a matrix of dimension m×n with all entries 0. Other than that, there are no special constraints:

          |0 0 0|

    Z3x3 = |0 0 0|

          |0 0 0|

    Z1x2 = |0 0|

The only interesting thing about the zero matrix is that it has the standard properties of scalar 0 for both matrix addition and multiplication. Other than that, it’s pretty useless.

#### Matrix Addition

Addition and subtraction of matrices is performed by adding or subtracting each element in two matrices and coming up with a result for each entry. The only rule to addition and subtraction is that the matrices that the operation is being performed on must be of the same dimension. Here are two examples:

    A = |1  5|          B = |13 7 |

        |-2 0|              |5 –10|

    A + B = |1  5| + |13 7 | = |(1+13) (5+7)| = |14  12|

            |-2 0|   |5 –10|   |(-2+5) (0-10|   |3  -10|

    A – B = |1  5| + |13 7 | = |(1-13) (5-7)    | = |-12 –2|

            |-2 0|   |5 –10|   |(-2-5) (0-(-10))|   |-7  10|

Note that both addition and subtraction are associative; that is, A + (B + C) = (A + B) + C. However, they’re not commutative under subtraction. (A – B) may not equal (B – A).

#### Matrix Multiplication

There are two forms of matrix multiplication: scalar and matrix. Scalar matrix multiplication is simply the multiplication of a matrix by a scalar number. You simply multiply each element of the matrix by the number. The matrix can be m×n at any size.

Here’s a general description for a 3×3 matrix. Let k be any real constant:

            |a00 a01 a02|

    Let A = |a10 a11 a12|

            |a20 a21 a22|

                 |a00 a01 a02|   |k*a00 k*a01 k*a02|

    Then k*A = k*|a10 a11 a12| = |k*a10 k*a11 k*a12|

                 |a20 a21 a22|   |k*a20 k*a21 k*a22|

Math Scalar multiplication is also valid for matrix equations, as long as you perform the multiplication on both sides. This is true because you can always multiply the coefficients of any system by a constant as long as you do so to both the RHS (right hand side) and LHS (left hand side) of the system.

Here’s an example:

    3*| 1 4| = |(3*1)    (3*4)| = |3  12|

      |-2 6|   |(3*(-2)) (3*6)|   |-6 18|

The second type of multiplication is true matrix multiplication. Its mathematical basis is a bit complex, but you can think of a matrix as an “operator” that operates on another matrix. Given two matrices, A and B, that you want to multiply, they must have the same inner dimension. In other words, if A is m×n, B must be n×r. m and r may or may not be equal, but the inner dimension must be. For example, you can multiply a 2×2 by a 2×2, a 3×2 by a 2×3, and a 4×4 by a 4×5, but you can’t multiply a 3×3 by a 2×4 because the inner dimensions aren’t equal. The resulting matrix will have a size that is equal to the outer dimension of the multiplier and multiplicand matrix. For example, a 2×3 multiplying a 3×4 would have dimension 2×4.

Matrix multiplication is one of those things that’s very hard to describe with words. I always end up waving my hands a lot to show what I’m saying, so take a look at Figure 8.25 while I give you the technical description of the multiplication algorithm.

Figure 8.25 The mechanics of matrix multiplication.

Given a matrix A and B, or A×B, and to multiply them together to compute each element of the result matrix C, you must take a row of A and multiply it by a column in B. To perform the multiplication, you sum the products of each element, which is also called the dot product. Here’s an example for a 2×2 multiplying a 2×3—order counts!

    Let A = |1 2|      B = |1 3 5|

            |3 4|          |6 0 4|

    C = A x B = |(1*1 + 2*6) (1*3 + 2*0) (1*5 +2*4)|

                |(3*1 + 4*6) (3*3 + 4*0) (3*5 +4*4)|

              = |13 3 13|

                |27 9 31|

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.