//2DMatrix.cpp #include "2DMatrix.h" #include "globals.h" #include namespace Turbulence { /** * Function: Constructor * Purpose: Initializes Matrix and sets it to Identity **/ Matrix2D::Matrix2D() { mMatrix[0] = 1; mMatrix[1] = 0; mMatrix[2] = 0; mMatrix[3] = 1; } /** * Function: Constructor * Purpose: Initializes Matrix and sets it to desired value **/ Matrix2D::Matrix2D(float x1, float y1, float x2, float y2) { mMatrix[0] = x1; mMatrix[1] = y1; mMatrix[2] = x2; mMatrix[3] = y2; } /** * Function: Copy Constructor * Purpose: **/ Matrix2D::Matrix2D(const Matrix2D& matrix) { for(int i = 0; i < 4; i++) mMatrix[i] = matrix.mMatrix[i]; } /** * Function: Set Matrix * Purpose: Sets Matrix to desired valuse **/ void Matrix2D::SetMatrix(float x1, float y1, float x2, float y2) { mMatrix[0] = x1; mMatrix[1] = y1; mMatrix[2] = x2; mMatrix[3] = y2; } /** * Fucntion: VectorRotation * Purpose: Takes a vector and rotates it by desired degrees **/ hgeVector Matrix2D::VectorRotation(float angle, hgeVector vector) { float _angle = angle * Turbulence::PI/180; // Converts degrees to radians //Set up Rotation Matrix mMatrix[0] = cos(_angle); mMatrix[1] = -sin(_angle); mMatrix[2] = sin(_angle); mMatrix[3] = cos(_angle); //Set up Vector matrix Matrix2D vectorMatrix; vectorMatrix.mMatrix[0] = vector.x; vectorMatrix.mMatrix[1] = vector.y; vectorMatrix.mMatrix[2] = 0; vectorMatrix.mMatrix[3] = 0; vectorMatrix *= *this; // Multiply vector.x = vectorMatrix.mMatrix[0]; vector.y = vectorMatrix.mMatrix[1]; return vector; } /** * Function: ScaleMatrix * Purpose: Scales Matrix by desired value **/ Matrix2D Matrix2D::ScaleMatrix(float scale) { for(int i = 0; i < 4; i++) mMatrix[i] *= scale; return *this; } /** * Fucntion: Overloaded = operator * Purpose: **/ Matrix2D Matrix2D::operator=(const Matrix2D &matrix2d) { for(int i = 0; i < 4; i++) mMatrix[i] = matrix2d.mMatrix[i]; return *this; } /** * Function: Overloaded * operator * Purpose: Multiplies Matrix by a second Matrix **/ Matrix2D Matrix2D::operator*(const Matrix2D &matrix2d) { Matrix2D productMatrix; productMatrix.mMatrix[0] = mMatrix[0] * matrix2d.mMatrix[0] + mMatrix[1] * matrix2d.mMatrix[2]; productMatrix.mMatrix[1] = mMatrix[0] * matrix2d.mMatrix[1] + mMatrix[1] * matrix2d.mMatrix[3]; productMatrix.mMatrix[2] = mMatrix[2] * matrix2d.mMatrix[0] + mMatrix[3] * matrix2d.mMatrix[2]; productMatrix.mMatrix[3] = mMatrix[2] * matrix2d.mMatrix[1] + mMatrix[3] * matrix2d.mMatrix[3]; return productMatrix; } /** * Function: Overloaded + operator * Purpose: **/ Matrix2D Matrix2D::operator+(const Matrix2D &matrix2d) { for(int i = 0; i < 4; i++) mMatrix[i] += matrix2d.mMatrix[i]; return *this; } /** * Function: Overloaded - operator * Purpose: **/ Matrix2D Matrix2D::operator -(const Matrix2D &matrix2d) { for(int i = 0; i < 4; i++) mMatrix[i] -= matrix2d.mMatrix[i]; return *this; } }