code for 3D Tranformation



#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int s1,s2,h,w;
class trans
{
public:
void draw();
void modified();
void translate();
void shearx();
void sheary();
void rotate();
void scale();
void assign();
int op[10],ip[10],mop[10],mip[10],i,dist;
}tr;
void trans::assign()
{
op[0]=op[6]=op[8]=s1;
op[1]=op[3]=op[9]=s2;
op[2]=op[4]=s1+w;
op[5]=op[7]=s2+h;
ip[0]=ip[6]=ip[8]=s1+w/2;
ip[1]=ip[3]=ip[9]=s2-h/2;
ip[2]=ip[4]=s1+w+w/2;
ip[5]=ip[7]=s2+h-h/2;
}
void trans::draw()
{
cleardevice();
setcolor(5);
drawpoly(5,op);
drawpoly(5,ip);
line(op[0],op[1],ip[0],ip[1]);
line(op[2],op[3],ip[2],ip[3]);
line(op[4],op[5],ip[4],ip[5]);
line(op[6],op[7],ip[6],ip[7]);
}
void trans::modified()
{
setcolor(15);
drawpoly(5,mop);
drawpoly(5,mip);
line(mop[0],mop[1],mip[0],mip[1]);
line(mop[2],mop[3],mip[2],mip[3]);
line(mop[4],mop[5],mip[4],mip[5]);
line(mop[6],mop[7],mip[6],mip[7]);
}
void trans::translate()
{
cout<<"\n Transition distance";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i++)
{
mop[i]=op[i]+dist;
mip[i]=ip[i]+dist;
}
modified();
}
void trans::shearx()
{
cout<<"\n\t shearx factor";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i=i+2)
{
mop[i]=op[i]+(dist+op[i+1]);
mop[i+1]=op[i+1];
mip[i]=ip[i]+(dist+ip[i+1]);
mip[i+1]=ip[i+1];
}
modified();
}
void trans::sheary()
{
cout<<"\n\t sheary factor";
cin>>dist;
cleardevice();
draw();
for(i=0;i<10;i=i+2)
{
mop[i]=op[i];
mop[i+1]=dist+op[i]+op[i+1];
mip[i]=ip[i];
mip[i+1]=dist+ip[i]+ip[i+1];
}
modified();
}
void trans::rotate()
{
int h,k;
float ang,rad;
cout<<"rotation angle";
cin>>ang;
rad=(3.14*ang)/180;
cout<<"\n\t rotate x by:";
cin>>h;
cout<<"\n\t rotate y by:";
cin>>k;
cleardevice();
draw();
for(int i=0;i<10;i=i+2)
{
mop[i]=h+(op[i]-h)*cos(rad)-(op[i+1]-k)*sin(rad);
mop[i+1]=k+(op[i]-h)*sin(rad)+(op[i+1]-k)*cos(rad);
mip[i]=h+(ip[i]-h)*cos(rad)-(ip[i+1]-k)*sin(rad);
mip[i+1]=k+(ip[i]-h)*sin(rad)+(ip[i+1]-k)*cos(rad);
}
modified();
}
void trans::scale()
{
int sx,sy;
cout<<"\n\t increment x by:";
cin>>sx;
cout<<"\n\t increment y by:";
cin>>sy;
cleardevice();
draw();
for(i=0;i<10;i++)
{
if(i%2==0)
{
mop[i]=op[i]*sx;
mip[i]=ip[i]*sx;
}
else
{
mop[i]=op[i]*sy;
mip[i]=ip[i]*sy;
}
}
modified();
}
void main()
{
char op,again;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cout<<"\n\t Give cuboid co ordinate:";
cout<<"\n\t enter top left:";
cin>>s1>>s2;
cout<<"Enter height and width:";
cin>>h>>w;
tr.assign();
do
{
cout<<"\n\t menu";
cout<<"\n\t.....";
cout<<"\n\t translate\n\t scale\n\t rotate";
cout<<"\n\t shearx\n\tsheary";
cout<<"\n\n\t I like to do transformation";
op=toupper(getch());
cout<getch();
cleardevice();
switch(op)
{
case 'T':
tr.translate();
break;
case 'S':
tr.scale();
break;
case 'X':
tr.shearx();
break;
case 'Y':
tr.sheary();
break;
case 'R':
tr.rotate();
break;
}
cout<<"do once again:";
again=getch();
cleardevice();
}
while(again!='n'&&again!='n');
getch();
closegraph();
}

0 comments :