matrix2x3 matrix2x3::Ortho ( float left, float right, float bottom, float top ) { float tx = (right + left) / (right - left); float ty = (top + bottom) / (top - bottom); float m11 = 2.0f / (right - left); float m22 = 2.0f / (top - bottom); return matrix2x3(m11, 0.0f, 0.0f, m22, tx, ty); }
matrix2x3 matrix2x3::Inverse () const { float det = (_m11 * _m22) - (_m21 * _m12); float newMatrix[6]; newMatrix[0] = _m22 / det; newMatrix[1] = -_m12 / det; newMatrix[2] = ((_m12 * _tY) - (_tX * _m22)) / det; newMatrix[3] = -_m21 / det; newMatrix[4] = _m11 / det; newMatrix[5] = ((_tX * _m21) - (_m11 * _tY)) / det; return matrix2x3(newMatrix); }
matrix2x3 matrix2x3::operator* ( const matrix2x3& srcm2 ) const { float inMatrix1[] = { _m11, _m12, _tX, _m21, _m22, _tY, 0.0f, 0.0f, 1.0f }; float inMatrix2[] = { srcm2._m11, srcm2._m12, srcm2._tX, srcm2._m21, srcm2._m22, srcm2._tY, 0.0f, 0.0f, 1.0f }; float outMatrix[9]; MatrixMultiply3(inMatrix1, inMatrix2, outMatrix); return matrix2x3 ( outMatrix ); }
matrix2x3 matrix2x3::Rotation ( float angle ) { return matrix2x3(cosf(angle), -sinf(angle), sinf(angle), cosf(angle), 0.0f, 0.0f); }
/** **************************************************************************************************** \fn void UnitTest( void ) \brief The unit test of Matrix class \param NONE \return NONE **************************************************************************************************** */ void GameEngine::Math::Matrix::UnitTest( void ) { FUNCTION_START; Matrix matrix2x2( 2, 2 ); // Check creation for( UINT8 i = 0; i < 2; ++i ) { for( UINT8 j = 0; j < 2; ++j ) assert( matrix2x2(i, j) == 0 ); } // Check matrix identity matrix2x2(0, 0) = 1; matrix2x2(0, 1) = 2; matrix2x2(1, 0) = 3; matrix2x2(1, 1) = 4; Matrix identityMatrix( 2, 2 ); identityMatrix(0, 0) = 1; identityMatrix(1, 1) = 1; Matrix m( 2, 2 ); m = matrix2x2.Identity(); assert( matrix2x2.Identity() == identityMatrix ); // Check matrix transpose Matrix matrix2x3( 2, 3 ); matrix2x3( 0, 0 ) = 1; matrix2x3( 0, 1 ) = 2; matrix2x3( 0, 2 ) = 3; matrix2x3( 1, 0 ) = 4; matrix2x3( 1, 1 ) = 5; matrix2x3( 1, 2 ) = 6; matrix2x3.Transpose(); Matrix matrix3x2( 3, 2 ); matrix3x2( 0, 0 ) = 1; matrix3x2( 0, 1 ) = 4; matrix3x2( 1, 0 ) = 2; matrix3x2( 1, 1 ) = 5; matrix3x2( 2, 0 ) = 3; matrix3x2( 2, 1 ) = 6; assert( matrix2x3 == matrix3x2 ); // Check matrix 3x3 inverse Matrix matrix3x3( 3, 3 ); matrix3x3( 0, 0 ) = 2; matrix3x3( 0, 1 ) = -1; matrix3x3( 0, 2 ) = 3; matrix3x3( 1, 0 ) = 1; matrix3x3( 1, 1 ) = 6; matrix3x3( 1, 2 ) = -4; matrix3x3( 2, 0 ) = 5; matrix3x3( 2, 1 ) = 0; matrix3x3( 2, 2 ) = 8; Matrix matrix3x3Inverse( 3, 3 ); Matrix3Inverse( matrix3x3, matrix3x3Inverse ); assert( (matrix3x3 * matrix3x3Inverse) == matrix3x3.Identity() ); // Check matrix 4x4 inverse Matrix matrix4x4( 4, 4 ); matrix4x4( 0, 0 ) = 2; matrix4x4( 0, 1 ) = 3; matrix4x4( 0, 2 ) = 4; matrix4x4( 0, 3 ) = 5; matrix4x4( 1, 0 ) = 5; matrix4x4( 1, 1 ) = 7; matrix4x4( 1, 2 ) = 9; matrix4x4( 1, 3 ) = 9; matrix4x4( 2, 0 ) = 5; matrix4x4( 2, 1 ) = 8; matrix4x4( 2, 2 ) = 7; matrix4x4( 2, 3 ) = 4; matrix4x4( 3, 0 ) = 4; matrix4x4( 3, 1 ) = 3; matrix4x4( 3, 2 ) = 3; matrix4x4( 3, 3 ) = 2; Matrix matrix4x4Inverse( 4, 4 ); Matrix4Inverse( matrix4x4, matrix4x4Inverse ); Matrix matrix4x4Result( 4, 4 ); matrix4x4Result = matrix4x4 * matrix4x4Inverse; assert( (matrix4x4 * matrix4x4Inverse) == matrix4x4.Identity() ); FUNCTION_FINISH; }