//--------------------------------------------------------- // Lab 13 // // Implement and test a class to perform some matrix // multiplication operations useful in drawing a model of // a three-dimensional object. // // D. Searls // REPLACE THIS LINE WITH YOUR NAME!! // Asbury University //--------------------------------------------------------- #include #include #include using namespace std; const double PI = 3.14159265358979; struct point3D { double x, y, z; // Coordinates of point }; class matrixType { private: double matrix [4][4]; //----------------------------------------------------- // multiply // // Replace this matrix with the product of m and this // matrix: // // matrix = m * matrix // // In parameter: m //----------------------------------------------------- void multiply(const double m[4][4]) { } public: //----------------------------------------------------- // The default matrix is the identity matrix. //----------------------------------------------------- matrixType() { } //----------------------------------------------------- // reset // // Reset the current matrix to the identity matrix. //----------------------------------------------------- void reset() { } //----------------------------------------------------- // display // // Display the current matrix. //----------------------------------------------------- void display() { } //----------------------------------------------------- // scale // // Replace this matrix with the product of the scaling // matrix and this matrix: // // s 0 0 0 // S = 0 s 0 0 // 0 0 s 0 // 0 0 0 1 // // matrix = S * matrix // // In Parameter: s //----------------------------------------------------- void scale(double s) { } //----------------------------------------------------- // translate // // Replace this matrix with the product of the // translation matrix and this matrix: // // 1 0 0 dx // T = 0 1 0 dy // 0 0 1 dz // 0 0 0 1 // // matrix = T * matrix // // In Parameters: dx, dy, dz //----------------------------------------------------- void translate(double dx, double dy, double dz) { } //----------------------------------------------------- // rotate // // Replace this matrix with the product of the rotation // matrix and this matrix: // // If the rotation axis is x: // // 1 0 0 0 // R = 0 cos(theta) -sin(theta) 0 // 0 sin(theta) cos(theta) 0 // 0 0 0 1 // // If the rotation axis is y: // // cos(theta) 0 sin(theta) 0 // R = 0 1 0 0 // -sin(theta) 0 cos(theta) 0 // 0 0 0 1 // // If the rotation axis is z: // // cos(theta) -sin(theta) 0 0 // R = sin(theta) cos(theta) 0 0 // 0 0 1 0 // 0 0 0 1 // // matrix = R * matrix // // In Parameters: axis, theta (in degrees) //----------------------------------------------------- void rotate(char axis, double theta) { } //----------------------------------------------------- // applyTo // // Apply this matrix transform to the specifed point // and return the result: // // result = matrix * pt // // In Parameter: pt //----------------------------------------------------- point3D applyTo(const point3D pt) { } }; //********************************************************* // M A I N D R I V E R //********************************************************* int main() { matrixType t; point3D pt; cout << setprecision(4) << fixed; // Test the constructor. You should see the // identity matrix displayed. cout << "Default Matrix:\n\n"; t.display(); cout << "Tap Enter to continue..."; cin.get(); // Test the scale function. You should see 2.0 down the // diagonal except at the bottom-right which should // still be a 1.0. cout << "\n\nScaling matrix times identity matrix:\n\n"; t.scale(2.0); t.display(); cout << "Tap Enter to continue..."; cin.get(); // Test the translate function. You see the identity // matrix but with 1.0, 2.0, and 3.0 in the first // three rows of the fourth column. t.reset(); t.translate(1.0, 2.0, 3.0); cout << "\n\nTranslation matrix times identity matrix:\n\n"; t.display(); cout << "Tap Enter to continue..."; cin.get(); // Test rotation about x-axis by 30 degrees. You should // get: // // 1.0000 0.0000 0.0000 0.0000 // 0.0000 0.8660 -0.5000 0.0000 // 0.0000 0.5000 0.8660 0.0000 // 0.0000 0.0000 0.0000 1.0000 t.reset(); t.rotate('x', 30); cout << "\n\nx-axis rotation matrix times identity matrix:\n\n"; t.display(); cout << "Tap Enter to continue..."; cin.get(); // Test rotation about y-axis by 30 degrees. You should // get: // // 0.8660 0.0000 0.5000 0.0000 // 0.0000 1.0000 0.0000 0.0000 // -0.5000 0.0000 0.8660 0.0000 // 0.0000 0.0000 0.0000 1.0000 t.reset(); t.rotate('y', 30); cout << "\n\ny-axis rotation matrix times identity matrix:\n\n"; t.display(); cout << "Tap Enter to continue..."; cin.get(); // Test rotation about z-axis by 30 degrees. You should // get: // // 0.8660 -0.5000 0.0000 0.0000 // 0.5000 0.8660 0.0000 0.0000 // 0.0000 0.0000 1.0000 0.0000 // 0.0000 0.0000 0.0000 1.0000 t.reset(); t.rotate('z', 30); cout << "\n\nz-axis rotation matrix times identity matrix:\n\n"; t.display(); cout << "Tap Enter to continue..."; cin.get(); // Now we'll put it all together and perform a scale, // move, and several rotates on the identity matrix. // You should get: // // 0.7660 -1.4077 1.1964 0.7699 // 1.3268 1.3205 0.7042 3.0402 // -1.2856 0.5240 1.4397 2.0408 // 0.0000 0.0000 0.0000 1.0000 t.reset(); t.scale(2); t.translate(1, 2, 3); t.rotate('x', 20); t.rotate('y', 40); t.rotate('z', 60); cout << "\n\nComposite transformation:\n\n"; t.display(); cout << "Tap Enter to continue..."; cin.get(); // Apply some scaling transformations to a point. cout << setprecision(0); cout << "\n\nApplying scaling transformations to a point:\n\n"; pt.x = 1.0; pt.y = 2.0; pt.z = 3.0; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "scaled by 2.0 is "; t.reset(); t.scale(2.0); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n"; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "scaled by 0.5 is "; t.reset(); t.scale(0.5); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n"; // Apply some move transformations to a point. cout << "\nApplying translation transformations to a point:\n\n"; pt.x = 1.0; pt.y = 2.0; pt.z = 3.0; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "moved by dx = 1, dy = 2, and dz = 3 is "; t.reset(); t.translate(1.0, 2.0, 3.0); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n"; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "moved by dx = -1, dy = -2, and dz = -3 is "; t.reset(); t.translate(-1.0, -2.0, -3.0); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n"; // Apply some rotation transformations to a point. cout << setprecision(3); cout << "\nApplying rotation transformations to a point:\n\n"; pt.x = 1.0; pt.y = 1.0; pt.z = 1.0; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "rotated 45 degrees about x-axis is "; t.reset(); t.rotate('x', 45); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n"; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "rotated -45 degrees about x-axis is "; t.reset(); t.rotate('x', -45); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n\n"; pt.x = 1.0; pt.y = 1.0; pt.z = 1.0; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "rotated 45 degrees about y-axis is "; t.reset(); t.rotate('y', 45); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n"; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "rotated -45 degrees about y-axis is "; t.reset(); t.rotate('y', -45); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n\n"; pt.x = 1.0; pt.y = 1.0; pt.z = 1.0; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "rotated 45 degrees about z-axis is "; t.reset(); t.rotate('z', 45); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ").\n"; cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ") " << "rotated -45 degrees about z-axis is "; t.reset(); t.rotate('z', -45); pt = t.applyTo(pt); cout << "(" << pt.x << ", " << pt.y << ", " << pt.z << ")\n\n"; return 0; }