Saturday 17 September 2011

2D TRANSFORMATION



2-D TRANSFORMATION

CODING:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
class transform
{
private:
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
float sx,sy;
public:
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
};
void transform::menu()
{
cout<<"MENU";
cout<<"1.TRANSLATION\n";
cout<<"2.ROTATION\n";
cout<<"3.SCALING\n";
cout<<"4.SHEARING\n";
cout<<"5.REFLECTION\n";
cout<<"6.EXIT\n";
cout<<"Enter the choice:";
cin>>option;
switch(option)
{
case 1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4:
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
}
}
void transform::input()
{
cout<<"Enter the number of vertices:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the coordinates:";
cin>>a[i][0]>>a[i][1]>>a[i+1][0]>>a[i+1][1];
}
}
void transform::output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void transform::translation()
{
output();
cout<<"Enter the translation vertex tx,ty: \n";
cin>>tx>>ty;
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
output();
delay(10);
menu();
}
void transform::rotation()
{
output();
cout<<"Enter the rotating angle:";
cin>>y;
cout<<"Enter the pivot point:";
cin>>fx>>fy;
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}
void transform::scaling()
{
output();
cout<<"Enter the scaling factor:\n";
cin>>sx>>sy;
cout<<"Enter the fixed point:";
cin>>fx>>fy;
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fx*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
delay(10);
menu();
}
void transform::shearing()
{
output();
cout<<"Enter the shear value:";
cin>>sh;
cout<<"Enter the fixed point:";
cin>>fx>>fy;
cout<<"Enter the axis for shearing if x-axis then '1' if y-axis then '0':";
cin>>axis;
for(i=0;i<n;i++)
{
if(axis==1)
{
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
}
else
{
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
}
}
output();
delay(10);
menu();
}
void transform::reflection()
{
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
output();
delay(10);
menu();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"E:/tc/bgi");
transform t;
t.menu();
getch();
}


0 comments:

Post a Comment