Programming
 Home -> Programming

```#ifndef __D3DMATRIX3X3_H__
#define __D3DMATRIX3X3_H__
#pragma once
#include "d3dobject.h"
class CD3DMatrix3x3 : public CD3DObject
{
public:
CD3DMatrix3x3(void);
virtual ~CD3DMatrix3x3(void);

// Attributes
public:
float m_e11;
float m_e12;
float m_e13;

float m_e21;
float m_e22;
float m_e23;

float m_e31;
float m_e32;
float m_e33;

private:

// Operations
public:
CD3DMatrix3x3(float r1c1, float r1c2, float r1c3,
float r2c1, float r2c2, float r2c3,
float r3c1, float r3c2, float r3c3);

// Gets the determinant of the matrix
float Determinant(void);

CD3DMatrix3x3 Transpose(void);

CD3DMatrix3x3 Inverse(void);

// Matrix manipulation
CD3DMatrix3x3& operator += (CD3DMatrix3x3 matrix);

CD3DMatrix3x3& operator -= (CD3DMatrix3x3 matrix);

// Scalar manipulation
CD3DMatrix3x3& operator *= (float scalar);

CD3DMatrix3x3& operator /= (float scalar);
};

#include "D3DMatrix3x3.inl"

#endif //__D3DMATRIX3X3_H__
```

### Source:

```//
// | m_e11  m_e12  m_e13 |		element<row><column>
// | m_e21  m_e22  m_e23 |
// | m_e31  m_e32  m_e33 |
//
inline CD3DMatrix3x3::CD3DMatrix3x3(void)
{
m_e11 = 0;
m_e12 = 0;
m_e13 = 0;

m_e21 = 0;
m_e22 = 0;
m_e23 = 0;

m_e31 = 0;
m_e32 = 0;
m_e33 = 0;
}

inline CD3DMatrix3x3::CD3DMatrix3x3(float r1c1, float r1c2, float r1c3,
float r2c1, float r2c2, float r2c3,
float r3c1, float r3c2, float r3c3)
{
m_e11 = r1c1;
m_e12 = r1c2;
m_e13 = r1c3;

m_e21 = r2c1;
m_e22 = r2c2;
m_e23 = r2c3;

m_e31 = r3c1;
m_e32 = r3c2;
m_e33 = r3c3;
}

//
// | m_e11 m_e21 m_e31 | | + - + |
// | m_e12 m_e22 m_e32 | | - + - |
// | m_e13 m_e23 m_e33 | | + - + |
//
inline float CD3DMatrix3x3::Determinant(void)
{
float part1 = m_e11 * ((m_e22 * m_e33) - (m_e32 * m_e23));
float part2 = m_e21 * ((m_e12 * m_e33) - (m_e32 * m_e13));
float part3 = m_e31 * ((m_e12 * m_e23) - (m_e22 * m_e13));

return (part1 - part2 + part3);
}

inline CD3DMatrix3x3 CD3DMatrix3x3::Transpose(void)
{
return CD3DMatrix3x3(m_e11, m_e21, m_e31,
m_e12, m_e22, m_e32,
m_e13, m_e23, m_e33);
}

inline CD3DMatrix3x3 CD3DMatrix3x3::Inverse(void)
{
float det = Determinant();

if (0 == det)
det = 1;

float r1c1 = ((m_e22 * m_e33) - (m_e23 * m_e32)) / det;
float r1c2 = -((m_e12 * m_e33) - (m_e13 * m_e32)) / det;
float r1c3 = ((m_e12 * m_e23) - (m_e13 * m_e22)) / det;

float r2c1 = -((m_e21 * m_e33) - (m_e23 * m_e31)) / det;
float r2c2 = ((m_e11 * m_e33) - (m_e13 * m_e31)) / det;
float r2c3 = -((m_e11 * m_e23) - (m_e13 * m_e21)) / det;

float r3c1 = ((m_e21 * m_e32) - (m_e22 * m_e31)) / det;
float r3c2 = -((m_e11 * m_e32) - (m_e12 * m_e31)) / det;
float r3c3 = ((m_e11 * m_e22) - (m_e12 * m_e21)) / det;

// Set the inverse
return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}

inline CD3DMatrix3x3& CD3DMatrix3x3::operator += (CD3DMatrix3x3 matrix)
{
m_e11 += matrix.m_e11;
m_e12 += matrix.m_e12;
m_e13 += matrix.m_e13;

m_e21 += matrix.m_e21;
m_e22 += matrix.m_e22;
m_e23 += matrix.m_e23;

m_e31 += matrix.m_e31;
m_e32 += matrix.m_e32;
m_e33 += matrix.m_e33;

return *this;
}

inline CD3DMatrix3x3& CD3DMatrix3x3::operator -= (CD3DMatrix3x3 matrix)
{
m_e11 -= matrix.m_e11;
m_e12 -= matrix.m_e12;
m_e13 -= matrix.m_e13;

m_e21 -= matrix.m_e21;
m_e22 -= matrix.m_e22;
m_e23 -= matrix.m_e23;

m_e31 -= matrix.m_e31;
m_e32 -= matrix.m_e32;
m_e33 -= matrix.m_e33;

return *this;
}

inline CD3DMatrix3x3& CD3DMatrix3x3::operator *= (float scalar)
{
m_e11 *= scalar;
m_e12 *= scalar;
m_e13 *= scalar;

m_e21 *= scalar;
m_e22 *= scalar;
m_e23 *= scalar;

m_e31 *= scalar;
m_e32 *= scalar;
m_e33 *= scalar;

return *this;
}

inline CD3DMatrix3x3& CD3DMatrix3x3::operator /= (float scalar)
{
m_e11 /= scalar;
m_e12 /= scalar;
m_e13 /= scalar;

m_e21 /= scalar;
m_e22 /= scalar;
m_e23 /= scalar;

m_e31 /= scalar;
m_e32 /= scalar;
m_e33 /= scalar;

return *this;
}

// Matrix Functions and Operators
//////////////////////////////////////////////////////////////////////////////////////////
inline CD3DMatrix3x3 operator * (CD3DMatrix3x3 matrix1, CD3DMatrix3x3 matrix2)
{
float r1c1 = (matrix1.m_e11 * matrix2.m_e11) + (matrix1.m_e12 + matrix2.m_e21)
+ (matrix1.m_e13 + matrix2.m_e31);
float r1c2 = (matrix1.m_e11 * matrix2.m_e12) + (matrix1.m_e12 + matrix2.m_e22)
+ (matrix1.m_e13 + matrix2.m_e32);
float r1c3 = (matrix1.m_e11 * matrix2.m_e13) + (matrix1.m_e12 + matrix2.m_e23)
+ matrix1.m_e13 + matrix2.m_e33);

float r2c1 = (matrix1.m_e21 * matrix2.m_e11) + (matrix1.m_e22 + matrix2.m_e21)
+ (matrix1.m_e23 + matrix2.m_e31);
float r2c2 = (matrix1.m_e21 * matrix2.m_e12) + (matrix1.m_e22 + matrix2.m_e22)
+ (matrix1.m_e23 + matrix2.m_e32);
float r2c3 = (matrix1.m_e21 * matrix2.m_e13) + (matrix1.m_e22 + matrix2.m_e23)
+ (matrix1.m_e23 + matrix2.m_e33);

float r3c1 = (matrix1.m_e31 * matrix2.m_e11) + (matrix1.m_e32 + matrix2.m_e21)
+ (matrix1.m_e33 + matrix2.m_e31);
float r3c2 = (matrix1.m_e31 * matrix2.m_e12) + (matrix1.m_e32 + matrix2.m_e22)
+ (matrix1.m_e33 + matrix2.m_e32);
float r3c3 = (matrix1.m_e31 * matrix2.m_e13) + (matrix1.m_e32 + matrix2.m_e23)
+ (matrix1.m_e33 + matrix2.m_e33);

return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}

inline CD3DMatrix3x3 operator + (CD3DMatrix3x3 matrix1, CD3DMatrix3x3 matrix2)
{
float r1c1 = (matrix1.m_e11 + matrix2.m_e11);
float r1c2 = (matrix1.m_e12 + matrix2.m_e12);
float r1c3 = (matrix1.m_e13 + matrix2.m_e13);

float r2c1 = (matrix1.m_e21 + matrix2.m_e21);
float r2c2 = (matrix1.m_e22 + matrix2.m_e22);
float r2c3 = (matrix1.m_e23 + matrix2.m_e23);

float r3c1 = (matrix1.m_e31 + matrix2.m_e31);
float r3c2 = (matrix1.m_e32 + matrix2.m_e32);
float r3c3 = (matrix1.m_e33 + matrix2.m_e33);

return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}

inline CD3DMatrix3x3 operator - (CD3DMatrix3x3 matrix1, CD3DMatrix3x3 matrix2)
{
float r1c1 = (matrix1.m_e11 - matrix2.m_e11);
float r1c2 = (matrix1.m_e12 - matrix2.m_e12);
float r1c3 = (matrix1.m_e13 - matrix2.m_e13);

float r2c1 = (matrix1.m_e21 - matrix2.m_e21);
float r2c2 = (matrix1.m_e22 - matrix2.m_e22);
float r2c3 = (matrix1.m_e23 - matrix2.m_e23);

float r3c1 = (matrix1.m_e31 - matrix2.m_e31);
float r3c2 = (matrix1.m_e32 - matrix2.m_e32);
float r3c3 = (matrix1.m_e33 - matrix2.m_e33);

return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}

inline CD3DMatrix3x3 operator * (CD3DMatrix3x3 matrix, float scalar)
{
float r1c1 = (matrix.m_e11 * scalar);
float r1c2 = (matrix.m_e12 * scalar);
float r1c3 = (matrix.m_e13 * scalar);

float r2c1 = (matrix.m_e21 * scalar);
float r2c2 = (matrix.m_e22 * scalar);
float r2c3 = (matrix.m_e23 * scalar);

float r3c1 = (matrix.m_e31 * scalar);
float r3c2 = (matrix.m_e32 * scalar);
float r3c3 = (matrix.m_e33 * scalar);

return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}

inline CD3DMatrix3x3 operator * (float scalar, CD3DMatrix3x3 matrix)
{
float r1c1 = (matrix.m_e11 * scalar);
float r1c2 = (matrix.m_e12 * scalar);
float r1c3 = (matrix.m_e13 * scalar);

float r2c1 = (matrix.m_e21 * scalar);
float r2c2 = (matrix.m_e22 * scalar);
float r2c3 = (matrix.m_e23 * scalar);

float r3c1 = (matrix.m_e31 * scalar);
float r3c2 = (matrix.m_e32 * scalar);
float r3c3 = (matrix.m_e33 * scalar);

return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}

inline CD3DMatrix3x3 operator / (CD3DMatrix3x3 matrix, float scalar)
{
float r1c1 = (matrix.m_e11 / scalar);
float r1c2 = (matrix.m_e12 / scalar);
float r1c3 = (matrix.m_e13 / scalar);

float r2c1 = (matrix.m_e21 / scalar);
float r2c2 = (matrix.m_e22 / scalar);
float r2c3 = (matrix.m_e23 / scalar);

float r3c1 = (matrix.m_e31 / scalar);
float r3c2 = (matrix.m_e32 / scalar);
float r3c3 = (matrix.m_e33 / scalar);

return CD3DMatrix3x3(r1c1, r1c2, r1c3,
r2c1, r2c2, r2c3,
r3c1, r3c2, r3c3);
}
```