//samantha petro. created 27 sept 2006. last modified 12 oct 2006 //matrix.cpp - defines the matrix class #include "matrix.h" // ------------- trig --------------- // float mySin(float theta){ if (theta == 180.0f || theta == 360.0f) return 0; return sinf(TO_RADS(theta)); } float myCos(float theta){ if (theta == 90.0f || theta == 270.0f) return 0; return cosf(TO_RADS(theta)); } // ---- basic matrix functions ---- // void matrix4x4::zeroOut() { //zero out the matrix for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { values[i][j] = 0; } } //make the diagonal values = 1 for (i = 0; i < row; i++) { values[i][i] = 1; } } void matrix4x1::zeroOut() { //zero out the matrix for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { values[i][j] = 0; } } values[3][0] = 1; } matrix4x1 matrix4x1::midpoint(matrix4x1 m) { //finds the midpoint between two verteces (matrices) //midpoint is the avg of the two corresponding coords matrix4x1 mid; mid.values[0][0] = (values[0][0] + m.values[0][0]) / 2; mid.values[1][0] = (values[1][0] + m.values[1][0]) / 2; mid.values[2][0] = (values[2][0] + m.values[2][0]) / 2; //the 4th value is always 1 return mid; } // ------- TRANSFORMATIONS -------- // TRANSLATE matrix4x4 matrix4x4::transToOrigin(matrix4x1 coords) { matrix4x4 temp; //fills the diagonal with 1s //-- fill the translation to origin matrix /*the translation to origin matrix: [ 1 0 0 -coords[0][0]] [ 0 1 0 -coords[1][0]] [ 0 0 1 -coords[2][1]] [ 0 0 0 1 ]*/ temp.values[0][3] = -coords.values[0][0]; temp.values[1][3] = -coords.values[1][0]; temp.values[2][3] = -coords.values[2][0]; return temp; } matrix4x4 matrix4x4::transToPoint(matrix4x1 coords) { matrix4x4 temp; //fills the diagonal with 1s //-- fill the translation to origin matrix /*the translation to origin matrix: [ 1 0 0 coords[0][0]] [ 0 1 0 coords[1][0]] [ 0 0 1 coords[2][1]] [ 0 0 0 1 ]*/ temp.values[0][3] = coords.values[0][0]; temp.values[1][3] = coords.values[1][0]; temp.values[2][3] = coords.values[2][0]; return temp; } // ------- TRANSFORMATIONS -------- // SCALE matrix4x4 matrix4x4::scale(float xscale, float yscale, float zscale) { matrix4x4 scale; //-- fill scale matrix /*the scale matrix: [ Sx 0 0 0 ] [ 0 Sy 0 0 ] [ 0 0 Sz 0 ] [ 0 0 0 1 ]*/ scale.values[0][0] = xscale; scale.values[1][1] = yscale; scale.values[2][2] = zscale; scale.values[3][3] = 1; return scale; } // ------- TRANSFORMATIONS -------- // ROTATE matrix4x4 matrix4x4::roll(float theta) { matrix4x4 rotate; /* ROLL matrix: [cos -sin 0 0] [sin cos 0 0] [ 0 0 1 0] [ 0 0 0 1] */ //fill in rotation values rotate.values[0][0] = myCos(theta); rotate.values[0][1] = -mySin(theta); rotate.values[1][0] = mySin(theta); rotate.values[1][1] = myCos(theta); return rotate; } matrix4x4 matrix4x4::pitch(float theta) { matrix4x4 rotate; /* PITCH matrix: [1 0 0 0] [0 cos -sin 0] [0 sin cos 0] [0 0 0 1] */ //fill in rotation values rotate.values[1][1] = myCos(theta); rotate.values[1][2] = -mySin(theta); rotate.values[2][1] = mySin(theta); rotate.values[2][2] = myCos(theta); return rotate; } matrix4x4 matrix4x4::yaw(float theta) { matrix4x4 rotate; /* YAW matrix: [ cos 0 sin 0] [ 0 1 0 0] [-sin 1 cos 0] [ 0 0 0 1] */ //fill in rotation values rotate.values[0][0] = myCos(theta); rotate.values[0][2] = mySin(theta); rotate.values[2][0] = -mySin(theta); rotate.values[2][2] = myCos(theta); return rotate; } // ------- operator I/0 - 4x4 matrix ----------- // istream &operator >> (istream &is, matrix4x4 &m) { //take in and store values in matrix m for (int i = 0; i < m.row; i++) { //iterates thru rows for (int j = 0; j < m.col; j++) { is >> m.values[i][j]; is.ignore(); } } return is; } ostream &operator << (ostream &os, matrix4x4 &m) { //Display values for (int i = 0; i < m.row; i++) { //iterates thru rows os << "[ "; for (int j = 0; j < m.col; j++) { os << setw(6) << m.values[i][j] << " "; } os << "]"; if (i < m.row-1) //if not the last row os << "\n"; } return os; } // ------- operator I/O - 4x1 matrix ------ // istream &operator >> (istream &is, matrix4x1 &m) { for (int i = 0; i < m.row-1; i++) { is >> m.values[i][0]; is.ignore(); } //the last row is 1 m.values[3][0] = 1; return is; } ostream &operator << (ostream &os, matrix4x1 &m) { os << "(" << m.values[0][0] << "," //x-coord << m.values[1][0] << "," //y-coord << m.values[2][0] << ")"; //z-coord return os; } // ------- operator arithmetic ------ // matrix4x4 matrix4x4::operator * (const matrix4x4 &m) { //create a new matrix to store and return matrix4x4 n; float total; //stores the total of the current operation for (int i=0; i