(VS2010) Inverse Matrix

Hello,

I am a novice/newcomer to C++ and am taking classes for game programming.

One of our problems is to find the inverse of a 3x3 matrix.

The problem specifically reads "Make the Inverse() method by using the Determinant method covered in the book to implement this method."
I have separated each method by asterisks even though they are in the same .cpp file for clarity. I believe the first method is correct, but the second one is sort of ticking me off.
*************************************************************************

Scalar Matrix3D:: Determinant() const
{
Scalar result = 0 ;

result = m[0][0]*m[1][1]*m[2][2] + m[0][1]*m[1][2]*m[2][0] + m[0][2]*m[1][0]*m[2][1] - m[0][0]*m[1][2]*m[2][1] - m[0][1]*m[1][0]*m[2][2] - m[0][2]*m[1][1]*m[2][0];



// Compute the determinant of a 3x3 matrix, the return value is called result. // Each entry is of the form m[i][j]

return result ;
}

****************************************************************************


Matrix3D Matrix3D::Inverse()
{
Matrix3D result ;

Scalar one_over_determinant = 1.0f / Determinant() ;

for ( int row = 0 ; row < dimension ; row++ )
for ( int col = 0 ; col < dimension ; col++ )
if ( (row+col)%2 ==0)
m[row][col] = one_over_determinant * m[row][col];

//I wrote only the line that begins with m[row] directly above and the one below the else statement. The rest in this method are from the school.

else

m[row][col] = -one_over_determinant * m[row][col];
//Write the proper code here
return result ;
}


I feel like this is not the way that an Inverse should be calculated.

Anyway...
//Here's an example of a inverse calculation taken from http://www.dr-lex.be/random/matrix_inv.html .//


So my logic, for the non-diamond (that is non-negating elements) in the Inverse() method above was to just multiply the one_over_determinant with each element of the matrix (both Scalar quantities), and for the diamond elements just put a minus sign in front of the one_over_determinant to make is negation. Will this work? Am I missing something else?

I realize that in these assignment we have a handful of other header and cpp files that have dozens of classes and function therein, but this seems to operator pretty independently. Both methods above are in the Matrix3D class. Should these two methods be combined somehow, or is that stretching the necessity of the assignment?

Note: if you don't understand what I mean by the diamond elements i'll try to illustrate it by showing a 3x3 matrix below.

+ | - | +
- | +| -
+ | - | +

I think this is called the adjugate matrix.
Last edited on
Topic archived. No new replies allowed.