Sie sind auf Seite 1von 6

#include<iostream.

h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<process.h>
#include<dos.h>
struct point
{
int x, y, z ;
};
void trans_mat( int arr[4][4] , int tx , int ty , int tz)
{
arr[0][0] = 1;
arr[0][1] = 0;
arr[0][2] = 0;
arr[0][3] = tx;
arr[1][0] = 0;
arr[1][1] = 1;
arr[1][2] = 0;
arr[1][3] = ty;
arr[2][0] = 0;
arr[2][1] = 0;
arr[2][2] = 1;
arr[2][3] = tz;
arr[3][0] = 0;
arr[3][1] = 0;
arr[3][2] = 0;
arr[3][3] = 1;
}
void rotatx_mat( int arr[4][4] , float z)
{
arr[0][0] = 1;
arr[0][1] = 0;
arr[0][2] = 0;
arr[0][3] = 0;
arr[1][0] = 0;
arr[1][1] = cos(z);
arr[1][2] = -sin(z);
arr[1][3] = 0;
arr[2][0] = 0;
arr[2][1] = sin(z);
arr[2][2] = cos(z);
arr[2][3] = 0;
arr[3][0] = 0;
arr[3][1] = 0;
arr[3][2] = 0;
arr[3][3] = 1;
}
void rotaty_mat( int arr[4][4] , float z)
{
arr[0][0] = cos(z);
arr[0][1] = 0;
arr[0][2] = sin(z);
arr[0][3] = 0;
arr[1][0] = 0;
arr[1][1] = 1;

arr[1][2]
arr[1][3]
arr[2][0]
arr[2][1]
arr[2][2]
arr[2][3]
arr[3][0]
arr[3][1]
arr[3][2]
arr[3][3]

=
=
=
=
=
=
=
=
=
=

0;
0;
-sin(z);
0;
cos(z);
0;
0;
0;
0;
1;

}
void rotatz_mat( int arr[4][4] , float z)
{
arr[0][0] = cos(z);
arr[0][1] = -sin(z);
arr[0][2] = 0;
arr[0][3] = 0;
arr[1][0] = sin(z);
arr[1][1] = cos(z);
arr[1][2] = 0;
arr[1][3] = 0;
arr[2][0] = 0;
arr[2][1] = 0;
arr[2][2] = 1;
arr[2][3] = 0;
arr[3][0] = 0;
arr[3][1] = 0;
arr[3][2] = 0;
arr[3][3] = 1;
}
void scale_mat( int
{
arr[0][0] =
arr[0][1] =
arr[0][2] =
arr[0][3] =
arr[1][0] =
arr[1][1] =
arr[1][2] =
arr[1][3] =
arr[2][0] =
arr[2][1] =
arr[2][2] =
arr[2][3] =
arr[3][0] =
arr[3][1] =
arr[3][2] =
arr[3][3] =
}

arr[4][4] , float sx , float sy , float sz)


sx;
0;
0;
0;
0;
sy;
0;
0;
0;
0;
sz;
0;
0;
0;
0;
1;

void mat_mul(int a[4][4] , point *p)


{
int c[4][1] , b[4][1];
c[0][0]=p->x;
c[1][0]=p->y;
c[2][0]=p->z;
c[3][0]=1;
cout<< " i/p " << p->x <<"
" << p->y<< p->z;

for(int i = 0 ; i<4 ; ++i)


{
for(int j = 0 ; j<1 ; ++j)
{
b[i][j] = 0;
for(int k = 0 ; k<4 ; ++k)
{
b[i][j] += a[i][k]*c[k][j];
}
}
}
p->x = b[0][0];
p->y = b[1][0];
p->z = b[2][0];
cout<<"\n"<<p->x <<" " << p->y <<p->z<< "\n";

void main ()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
int t[4][4];
point p[8];
do
{
cout<<"\n -----------------------------------------------------------";
cout<<"\n MAIN MENU \n \t 1. Define Object \n \t 2 . View Object \n \t 3
/ Transform \n \t 4. Exit \n \n ..";
cout<<"\n Enter your choice . . . ";
int choice;
cin>>choice;
p[0].x=0; p[0].y=0; p[0].z=0;
p[1].x=100; p[1].y=0; p[1].z=0;
p[2].x=100; p[2].y=100; p[2].z=0;
p[3].x=0; p[3].y=100; p[3].z=0;
p[4].x=0; p[4].y=0; p[4].z=100;
p[5].x=100; p[5].y=0; p[5].z=100;
p[6].x=100; p[6].y=100; p[6].z=100;
p[7].x=0; p[7].y=100; p[7].z=100;
switch(choice)
{
case 1 :

cout<<"\n Input the vertices of the cube ::";


for( int i=0;i<8;i++)
{
cin>>p[i].x>>p[i].y>>p[i].z;
cout<<"\n";
}

case 2 :

break;
cleardevice();
setcolor(RED);
line(p[0].x,p[0].y,p[1].x,p[1].y);
line(p[1].x,p[1].y,p[2].x,p[2].y);
line(p[2].x,p[2].y,p[3].x,p[3].y);
line(p[3].x,p[3].y,p[0].x,p[0].y);
line(p[4].x,p[4].y,p[5].x,p[5].y);
line(p[5].x,p[5].y,p[6].x,p[6].y);
line(p[6].x,p[6].y,p[7].x,p[7].y);
line(p[7].x,p[7].y,p[4].x,p[4].y);
line(p[0].x,p[0].y,p[4].x,p[4].y);
line(p[1].x,p[1].y,p[5].x,p[5].y);
line(p[2].x,p[2].y,p[6].x,p[6].y);
line(p[3].x,p[3].y,p[7].x,p[7].y);
setcolor(GREEN);
line(p[0].z,p[0].y,p[1].z,p[1].y);
line(p[1].z,p[1].y,p[2].z,p[2].y);
line(p[2].z,p[2].y,p[3].z,p[3].y);
line(p[3].z,p[3].y,p[0].z,p[0].y);
line(p[4].z,p[4].y,p[5].z,p[5].y);
line(p[5].z,p[5].y,p[6].z,p[6].y);
line(p[6].z,p[6].y,p[7].z,p[7].y);
line(p[7].z,p[7].y,p[4].z,p[4].y);
line(p[0].z,p[0].y,p[4].z,p[4].y);
line(p[1].z,p[1].y,p[5].z,p[5].y);
line(p[2].z,p[2].y,p[6].z,p[6].y);
line(p[3].z,p[3].y,p[7].z,p[7].y);
setcolor(BLUE);
line(p[0].x,p[0].z,p[1].x,p[1].z);
line(p[1].x,p[1].z,p[2].x,p[2].z);
line(p[2].x,p[2].z,p[3].x,p[3].z);
line(p[3].x,p[3].z,p[0].x,p[0].z);
line(p[4].x,p[4].z,p[5].x,p[5].z);
line(p[5].x,p[5].z,p[6].x,p[6].z);
line(p[6].x,p[6].z,p[7].x,p[7].z);
line(p[7].x,p[7].z,p[4].x,p[4].z);
line(p[0].x,p[0].z,p[4].x,p[4].z);
line(p[1].x,p[1].z,p[5].x,p[5].z);
line(p[2].x,p[2].z,p[6].x,p[6].z);
line(p[3].x,p[3].z,p[7].x,p[7].z);

case 3 :

getch();
break;
cout<<"\n ---- TRANSFORMATION MENU---- ";
cout<<" \n\t 1. Translate \n \t 2. Rotate \n \t

3. Scale ";
int c;
cin>>c;
switch(c)
{
case 1 :cout<<"Enter the X-translation"
;
int tx,ty,tz;
cin>>tx;
cout<<"Enter the Y - translation

";
cin>>ty;
cout<<"Enter the Z-translation";
cin>>tz;
trans_mat(t , tx , ty , tz);
for(int i = 0 ; i<8 ; ++i)
{
mat_mul(t , &p[i]);
}
break;
case 2 : char ch;
cout<<"Enter axis of rotation: "
;
cin>>ch;
cout<<"Enter the rotation angle
in degrees ( anti-clockwise is + )";
int z;
cin>>z;
float zz = (z*3.14)/180;
switch(ch)
{
case 'x':
case 'X':
rotatx_mat(t , zz);
for(i = 0 ; i<8 ; ++i)
{
mat_mul(t , &p[i]);
}
break;
case 'y':
case 'Y':
rotaty_mat(t , zz);
for(i = 0 ; i<8 ; ++i)
{
mat_mul(t , &p[i]);
}
break;
case 'z':
case 'Z':
rotatz_mat(t , zz);
for(i = 0 ; i<8 ; ++i)
{
mat_mul(t , &p[i]);
}
break;
}
break;
case 3 : cout<<"Enter the X-scale facto
r";
float sx , sy, sz;
cin>>sx;
cout<<"Enter the Y -scale factor
";
cin>>sy;
cout<<"Enter the Z -scale factor
";
cin>>sz;
scale_mat(t , tx , ty, tz);
for(i = 0 ; i<8 ; ++i)
{
mat_mul(t , &p[i]);
}
break;

}
break;
case 4 : cout<<"Exiting . . . ";
delay(100);
exit(0);
break;
}
}while(1);
/*
cout<<"Enter the values of the first matrix";
for(int i = 0 ; i < 3 ; ++i)
for(int j = 0 ; j < 3 ; ++j)
cin>>t[i][j];
cout<<"Enter the values of the second matrix";
for(i = 0 ; i < 3 ; ++i)
for(j = 0 ; j < 3 ; ++j)
cin>>m[i][j]; */
/*
cout<<"The multiplied matrix is :: \n";
for(i = 0 ; i<3 ; ++i)
{
for( j = 0 ; j<3 ; ++j)
{
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
*/
getch();
}

Das könnte Ihnen auch gefallen