// matrix_example.cpp // This module demonstrates how to use the matrix11 class to transform a 2-D coordinate // #include #include "matrix.h" #include using namespace std; using namespace math; //using namespace math; int main () { const float RAD=3.14159/180.0; // degrees to radians conversion factor math::matrix a(3,3); // a 3x3 matrix of floats a.Unit(); // sets the a matrix to be a unit matrix; ie: 1's on the diagonal // setup the matrix elements to represent a transformation including 30 degrees of rotation // and a translation of 10, -15 a(0,0) = cos(30*RAD); a(1,0) = sin(30*RAD); a(0,1) = -sin(30*RAD); a(1,1) = cos(30*RAD); a(0,2) = 10.0; // =px a(1,2) = -15.0; // =py // echo the matrix to the console std::cout << std::endl; std::cout << std::setprecision(4) << std::setw(12) << "a" << std::endl<< a << std::endl; std::cout << std::endl; matrix v2(3,1); // a 3x1 column homogeneous vector representing the coordinate v2=15,20 v2(0,0) = 15.0; v2(1,0) = 20.0; v2(2,0) = 1.0; matrix v1(3,1); // another 3x1 column vector - used to represent v1 v1 = a*v2; // operator* is overloaded to allow easy matrix multiplication // At this point, v1 is the transformed coordinate v2 std::cout << std::endl; std::cout << std::setprecision(4) << "v2" << std::endl<< v2 << std::endl; std::cout << std::endl; std::cout << std::setprecision(4) << "v1" << std::endl<< v1 << std::endl; matrix b(3,3); // another matrix to represent the inverse of a b = !a; // operator! is overloaded; !a returns the inverse of a std::cout << std::setprecision(4) << std::setw(12) << "a" << std::endl<< a << std::endl; std::cout << std::endl; std::cout << std::setprecision(4) << std::setw(12) << "b" << std::endl<< b << std::endl; std::cout << std::endl; v2 = b*v1; // the result should be what we started out with for v2 // v2 should have the same values at it did originally (except for roundoff); the diggity! std::cout << std::endl; std::cout << std::setprecision(4) << "v2" << std::endl<< v2 << std::endl; std::cout << std::endl; std::cout << std::setprecision(4) << "v1" << std::endl<< v1 << std::endl; }