Matrix 4x4 programming
Mar 1, 2010 at 5:37am UTC
First of all i would like to thank DrakeMagi for helping me solve my 1st problem from the past thread. Can someone give direction to my work? i am making 3 matrices and the user should input an angle or translation and a limit of 3 transformation only. For example:
1st transformation is by translation
and the user make some input of 1 at x axis, 0 at y axis, 0 at z axis,
Trans(1, 0, 0)
[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
next is a rotation at X axis with 90 degrees
[1 0 0 0 ]
|0 0 -1 0]
|0 1 0 0 ]
[0 0 0 1 ]
next is a rotation at Y axis with 90 degrees
[0 0 1 0 ]
[0 1 0 0 ]
[-1 0 0 0]
[0 0 0 1 ]
There should be a final result and also a 4x4 for the transformation applying the multiplication. The procedure of multiplying is like this
http://www.euclideanspace.com/maths/geometry/affine/matrix4x4/index.htm
my source code as of now is only this and i really don't know anymore what to do.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
const double Pi = 4.0 * atan(1.0); // 3.14159
// a class is a self define object
class xyz
{
public : // made public for outside access
double x , y, z;
xyz() { x = 0; y = 0; z = 0; } // constructor
// void clear() is just a function . void means no return item
void clear() { x = 0; y = 0; z = 0; } // a function of xyz
};
class myobject
{
public :
xyz x, y, z, rot, trans;
void clear() // a function of myobject
{ x.clear(); y.clear(); z.clear(); rot.clear(); atrans.clear();}
// all round does is take low numbers and set it to 0
// numbers like -4.37114e-08
double Round ( double f ) // a function of myobject class
{
// comment if block out to see really small
if ( f > -0.0000001 && f < 0.0000001 ) { f = 0; }
return f;
}
void Rotate( short n ) // a function of myobject class
{
switch (n)
{
case 1:
{
cout <<"Rotate X at angle: " ;
cin >> rot.x;
x.x = Round( sin (rot.x*Pi/180.0) );
y.x = Round( cos (rot.x*Pi/180.0) );
z.x = Round( cos (rot.x*Pi/180.0) );
x.y = Round( cos (rot.x*Pi/180.0) );
y.y = Round( cos (rot.x*Pi/180.0) );
z.y = Round( sin (rot.x*Pi/180.0) );
x.z = Round( cos (rot.x*Pi/180.0) );
y.z = Round( -sin (rot.x*Pi/180.0) );
z.z = Round( cos (rot.x*Pi/180.0) );
cout<<"Angle included" <<endl<<endl;
break ;
}
case 2:
{
cout <<"Rotate Y at angle: " ;
cin >> rot.y;
x.x = Round( cos (rot.y*Pi/180.0) );
y.x = Round( cos (rot.y*Pi/180.0) );
z.x = Round( -sin (rot.y*Pi/180.0) );
x.y = Round( cos (rot.y*Pi/180.0) );
y.y = Round( sin (rot.y*Pi/180.0) );
z.y = Round( cos (rot.y*Pi/180.0) );
x.z = Round( sin (rot.y*Pi/180.0) );
y.z = Round( cos (rot.y*Pi/180.0) );
z.z = Round( cos (rot.y*Pi/180.0) );
cout<<"Angle included" <<endl<<endl;
break ;
}
case 3:
{
cout <<"Rotate Z at angle: " ;
cin >> rot.z;
x.x = Round( cos (rot.z*Pi/180.0) );
y.x = Round( sin (rot.z*Pi/180.0) );
z.x = Round( cos (rot.z*Pi/180.0) );
x.y = Round( -sin (rot.z*Pi/180.0) );
y.y = Round( cos (rot.z*Pi/180.0) );
z.y = Round( cos (rot.z*Pi/180.0) );
x.z = Round( cos (rot.z*Pi/180.0) );
y.z = Round( cos (rot.z*Pi/180.0) );
z.z = Round( sin (rot.z*Pi/180.0) );
cout<<"Angle included" <<endl<<endl;
break ;
}
case 4:
{
cout<<"Add translation along X axis: " ;
cin>>xtransform;
cout<<"Add translation along X axis: " ;
cin>>ytransform;
cout<<"Add translation along X axis: " ;
cin>>ztransform;
trans=xtransform;
trans=ytransform;
trans=ztransform;
x.x = 1;
y.x = 0;
z.x = 0;
x.y = 0;
y.y = 1;
z.y = 0;
x.z = 0;
y.z = 0;
z.z = 1;
default :
{
break ;
}
}
}
void Print()
{
//this should be for the current transformation
string space = " " ;
cout << endl <<" Your Transformation is:" << endl;
cout << space << x.x << " " << x.y << " " << x.z << " " << trans.x << endl;
cout << space << y.x << " " << y.y << " " << y.z << " " << trans.y << endl;
cout << space << z.x << " " << z.y << " " << z.z << " " << trans.z << endl;
cout << endl;
}
void Calculate()
{
final.x.x = (a.x.x * b.x.x) + (a.x.y * b.y.x) + ( a.x.z*b.z.x ) + (a.trans.x *b.c1)
final.x.y = (a.x.x * b.x.y) + (a.x.y * b.y.y) + ( a.x.z*b.z.y ) + (a.trans.x *b.c2)
final.x.z = (a.x.x * b.x.z) + (a.x.y * b.y.z) + ( a.x.z*b.z.z ) + (a.trans.x *b.c3)
final.trans.x = (a.x.x * b.trans.x) + (a.x.y * b.trans.y) + ( a.x.z * b.trans.z ) + (a.trans.x *b.c4)
final.y.x = (a.y.x * b.x.x) + (a.y.y * b.y.x) + ( a.y.z*b.z.x ) + (a.trans.y *b.c1)
final.y.y = (a.y.x * b.x.y) + (a.y.y * b.y.y) + ( a.y.z*b.z.y ) + (a.trans.y *b.c2)
final.y.z = (a.y.x * b.x.z) + (a.y.y * b.y.z) + ( a.y.z*b.z.z) + (a.trans.y *b.c3)
final.trans.y = (a.y.x * b.trans.x) + (a.y.y * b.trans.y) + ( a.y.z * b.trans.z ) + (a.trans.y *b.c4)
final.z.x = (a.z.x * b.x.x) + (a.z.y * b.y.x) + ( a.z.z*b.z.x ) + (a.trans.z *b.c1)
final.z.y = (a.z.x * b.x.y) + (a.z.y * b.y.y) + ( a.z.z*b.z.y ) + (a.trans.z *b.c2)
final.z.z = (a.z.x * b.x.z) + (a.z.y * b.y.z) + ( a.z.z*b.z.z ) + (a.trans.z *b.c3)
final.trans.z = (a.z.x * b.trans.x) + (a.z.y * b.trans.y) + ( a.z.z * b.trans.z ) + (a.trans.z *b.c4)
final.c1 = (a.c1 * b.x.x) + (a.c2 * b.y.x) + ( a.c3*b.z.x ) + (a.c4 *b.c1)
final.c2 = (a.c1 * b.x.y) + (a.c2 * b.y.y) + ( a.c3*b.z.y ) + (a.c4 *b.c2)
final.c3 = (a.c1 * b.x.z) + (a.c2 * b.y.z) + ( a.c3*b.z.z ) + (a.c4 *b.c3)
final.c4 = (a.c1 * b.trans.x) + (a.c2 * b.trans.x) + ( a.c3 * b.trans.x ) + (a.c4 * b.trans.c4)
}
};
int main()
{
myobject obj;
int n;
bool run = true ;
while (run)
{
cout << " (1) Rotate X" << endl;
cout << " (2) Rotate Y" << endl;
cout << " (3) Rotate Z" << endl;
cout << " (4) Add Translation" << endl;
cout << " (5) Solve Final Transformation" << endl;
cout << " (10) Reset Transformation" << endl;
cout << " (11) Show Current Transformation" << endl;
cout << " (12) Quit" << endl;
cout << endl << "Choice :: " ;
cin >> n;
if ( n < 9 ) { obj.Rotate( n ); }
else if (n == 10) { obj.clear(); }
else if (n == 11) { obj.Print(); }
else if (n == 12) { run = false ; }
}
return 0;
}
Can you give me even just few suggestions only? or is it easy if i will use Visual BASIC than C++???
Last edited on Mar 1, 2010 at 5:40am UTC
Mar 1, 2010 at 5:57am UTC
Try to do just one problem first (say translation). Comment out the rest of the code, and test and debug just the translation part.
Topic archived. No new replies allowed.