Equal Matrices

 < Day Day Up > 

Matrices can be used to organize a set of numbers in rows and columns in your game. You might be familiar with the term array , which is a grid-like system for organizing any type of information (numbers, variables , text, even arrays). A matrix is just an array that is limited to storing numbers. We discussed an example of a matrix in the preceding chapter. In some places we used a single-row matrix to represent a vector. A matrix can have as many rows or columns as you need. We used a single row for one vector. You could also use a matrix with many rows to store all the vertices of an object in your game. In that case, each row would represent an individual point. You, as the programmer, can give any meaning you like to each row and column. We'll discuss more applications in Chapter 6, but we must first look at all the mathematical operations again in terms of matrices.

On paper, it's important to always use brackets ([]) around each matrix. If you get lazy and use straight lines, that indicates something completely different, so make sure it's brackets. Within the brackets you arrange numbers in a grid of rows and columns. Each number is called an entry , and each entry is denoted by its row and column location. Look at matrix A in Figure 5.1.

Figure 5.1. Matrix A.

graphics/05fig01.gif

If you needed to access the 3, you would ask the computer to get the number located in row 0, column 2. In C++, rows and columns start at 0, not 1. Figure 5.2 shows the location of each entry.

Figure 5.2. Entry locations.

graphics/05fig02.gif

To create code to manipulate matrices, there are several things to consider. Does your application require fixed size or resizable matrices for calculation? If you choose to take the former perspective, there are certain ways that you can organize matrix definitions for ease of use rather than unneeded flexibility. Here is an example of a fixed sized matrix:

 typedef struct  {    float x[3][3];  }   Matrix3X3; 

If you wanted to access the 3 in matrix A using this method, it would look like this:

 Matrix3X3 A; A.index[0][2]; 

The row is always given first, and then the column.

The other contemplated option would involve creating a class that would allow the programmer to create matrices of any size. This is a much more elegant and reusable solution to the problem, but also can cause potential overhead when making large numbers of calculations.

If you decide to create a matrix class, you'll then need to decide how the matrix is to be represented within the class. Will it hold pointers to floats? Will you use the Standard Template Library to make a vector of vectors? A Linked List? Any one of these types will work, but each one has its own expectations regarding the implementation. A fully implemented matrix class will also have overloaded operators that will allow for easy use of the math operations which are defined for matrices as well as for ease of access to the matrix data.

Another thing to consider regarding game development is the fact that most graphics APIs like DirectX and OpenGL already contain their own matrix types that are used when programming graphics. That doesn't mean that a good programmer can get by without understanding matrix math. Because the focus in this book is on the mathematics, our discussion will stick with the more straightforward matrix presented in the first example. When evaluating a solution, always try to find shortcuts that can be used to speed up the process. The example code in this chapter emphasizes understanding over speed, so don't settle for the examples as anything more than a starting point. All matrix examples in this chapter as well as in Chapter 6 will use the fixed defined matrix types. Refer to the accompanying programs to see the complete list of matrix definitions that are used. Most of them can be distilled from the type name .

Example 5.1: Defining Matrices

What are the dimensions of matrix B, and how would you locate the entry that has a value of 5?

Solution
  1. Look at matrix B in Figure 5.3. It has two rows and three columns, so its dimensions are 2x3.

    Figure 5.3. Matrix B.

    graphics/05fig03.gif

  2. Look for the 5. It's in the second row and first column, but remember that rows and columns are numbered starting with 0. This means that the 5 is in row 1, column 0.

For two matrices of the same dimensions, entries in the same row and column location are called corresponding entries . Two matrices are equal if and only if they have the same dimensions and all their corresponding entries are equal. If two matrices have different dimensions, they can never be equal. Also, if two matrices have the same dimensions, and all the corresponding entries are equal except one, they are not equal.

Two matrices are equal if

  • Both matrices have equal dimensions.

  • All corresponding entries are equal.


Example 5.2: Equal Matrices?

In Figure 5.4, are matrices C and D equal?

Figure 5.4. Matrices C and D.

graphics/05fig04.gif

Solution
  1. The first thing you need to check is the dimensions of each matrix. Matrix C is a 2x3 matrix, and D is a 3x3 matrix.

  2. Because the dimensions are not the same, these two matrices cannot be equal. Even though some of the corresponding entries are equal, the matrices must also be the same size.

Example 5.3: Equal Matrices?

In Figure 5.5, are matrices F and G equal?

Figure 5.5. Matrices F and G.

graphics/05fig05.gif

Solution
  1. The first thing you need to check is the dimensions of each matrix. Matrix F is a 3x3 matrix, and G is a 3x3 matrix. This is a good sign.

  2. Now you must check corresponding entries. Most of them are equal. However, look at the entries in row 2, column 0. Because they are not equal, matrices F and G are not equal.

How can we test equality of matrices using code? Most accesses to matrix will center on the use of for loops. The for loops will be sure to step through each index, performing the needed operations. This method is fundamental to working with all advanced matrix math, so make sure that it becomes ingrained.

Here is an example of a way to test two 3x3 matrices for equality:

 bool areMatricesEqual(Matrix3X3 a, Matrix3X3 b)     {         int errorFlag = 0;         for(int i = 0;i<3;i++)           {            for(int j=0;j<3;j++)             {               if((a.index[i][j]) != (b.index[i][j]))               errorFlag = 1;             }           }         //test for an error in equality.         if(errorFlag == 1)             return false;         else             return true;     } 

This function will access each location in the first matrix and compare it against the second. Should an error be found, the errorFlag is set and will cause the function to return false . Check the Chapter 5 programming examples to see this equality test in action. Can you think of a way to do this faster? (Hint: If any entry is found to be wrong the matrices will be considered not equal.)

This section began with a discussion of what matrices are and how to describe them. After we established some standards for working with matrices, we discussed checking for equality. Now that you have a working vocabulary for matrices, the next section moves to adding and subtracting matrices.

Self-Assessment

1.

What are the dimensions of matrix H, shown in Figure 5.6?

Figure 5.6. Matrix H.

graphics/05fig06.gif

2.

How would you describe the entry in matrix H (see Figure 5.6) that has a 2 in it?

3.

What are the dimensions of matrix J, shown in Figure 5.7?

Figure 5.7. Matrix J.

graphics/05fig07.gif

4.

In Figure 5.7, what number is stored in row 2, column 3 of matrix J?

5.

In Figure 5.8, are matrices K and L equal?

Figure 5.8. Matrices K and L.

graphics/05fig08.gif

6.

In Figure 5.9, are matrices M and N equal?

Figure 5.9. Matrices M and N.

graphics/05fig09.gif


 < Day Day Up > 


Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net