## Saturday, January 21, 2017

### Orbital Aero Model: Matrix Class

This is one of a series of posts for how orientations and rotations are handled in the orbital aero model (original post in series). This Matrix class ties in with the Vector, Euler, and Quaternion classes also posted.

### Matrix.h

#pragma once

class Matrix
{
private:

public:
double value; // [Row][Column]

// Create an empty matrix.
Matrix();

// Multiplies the matrix by another matrix.
Matrix operator * (const Matrix &matrixToMultiply) const;

// Get an inverted version of the matrix.
Matrix inverted() const;
};

### Matrix.cpp

#include "math.h"
#include "Matrix.h"

// Create an empty matrix.
Matrix::Matrix()
{
for (int rowIndex = 0; rowIndex < 3; rowIndex++)
{
for (int columnIndex = 0; columnIndex < 3; columnIndex++)
{
this->value[rowIndex][columnIndex] = 0.0;
}
}
return;
}

// Multiplies this matrix by another matrix.
Matrix Matrix::operator * (const Matrix &matrixToMultiply) const
{
Matrix calculatedMatrix;

for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
double sum = 0.0;
for (int k = 0; k <= 2; k++)
{
sum = sum + this->value[i][k] * matrixToMultiply.value[k][j];
}
calculatedMatrix.value[i][j] = sum;
}
}

return calculatedMatrix;
}

// Get an inverted version of the matrix.
Matrix Matrix::inverted() const
{
Matrix invertedMatrix;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
invertedMatrix.value[j][i] = this->value[i][j];
}
}

return invertedMatrix;
}