Aim :
To write a C++ program to perform composite
2D transformations such as translation,
rotation, scaling, reflection and shearing.
Algorithm:
Step 1: Start the program.
Step 2: Input the object coordinates.
Step 3: Translation
a) Enter the translation factors tx and ty.
b) Move the original coordinate position
(x,y) to a new position
(x1,y1).ie. x=x+x1, y=y+y1.
Step 4: Rotationd) Enter the radian for
rotation angle θ.
a) Rotate a point at position (x,y) through
an angle θ about the origin x1=xcos θ -
ysinθ , y1=ycosθ + xsinθ.
Step 5: Scaling
a) Input the scaled factors sx and sy.
b) The transformed coordinates (x1,y1) ,
x1=x.sx and y1=y.sy.
Step 6: Reflection
Reflection can be performed about x axis
and y axis.
a) Reflection about x axis : The
transformed coordinates are x1=a and y1=-y.
Step 7: Reflection about y axis : The
transformed coordinates are x1=x and y1=y.
Step 8: Shearing
a) Input the shearing factors shx and shy.
b) Shearing related to x axis : Transform
coordinates x1=x+shx*y and
y1=y.
c) Shearing related to y axis : Transform
coordinates x1=x and
y1=y+shy*x.
d) Input the xref and yref values.
e) X axis shear related to the reference
line y-yref is
x1=x+shx(y-yref) and y1=y.
f) Y axis shear related to the reference
line x=xref is x1=x and
y1=y+shy(x-xref)
Step 9: Finally display the transformed
object after the successive transformations.
Step 10: Stop the Program.
PROGRAM:
#include
#include
#include
#include
#include
void main()
{
int
gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,
sx,sy,shx,shy,xref,yref;
char
d;
gd=DETECT;
initgraph(&gd,&gm,"");
cout<<"enter the no of
points";
cin>>n;
for(i=0;i
{
cout<<"enter the
coordinates"<
cin>>xa[i]>>ya[i];
}
do
{
cout<<"menu";
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit";
cin>>op;
switch(op)
{
case
1:
cout<<"enter the translation
vector";
cin>>tx>>ty;
for(i=0;i
{
xa1[i]=xa[i]+tx;
ya1[i]=ya[i]+ty;
}
cleardevice();
cout<<"before translation";
for(i=0;i
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after translation";
for(i=0;i
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case
2:
cout<<"enter the rotation
angle";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the reference
points";
cin>>xf>>yf;
for(i=0;i
{
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
cleardevice();
cout<<"before rotation";
for(i=0;i
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after rotation";
for(i=0;i
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case
3:
cout<<"enter the scaling
factor";
cin>>sx>>sy;
cout<<"enter the reference
point";
cin>>rx>>ry;
for(i=0;i
{
xa1[i]=xa[i]*sx+rx*(1-sx);
ya1[i]=ya[i]*sy+ry*(1-sy);
}
cleardevice();
cout<<"before scaling";
for(i=0;i
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after scaling";
for(i=0;i
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case
4:
cout<<"enter the shear value";
cin>>shx>>shy;
cout<<"enter the reference
point";
cin>>xref>>yref;
cout<<"enter the shear direction x
or y";
cin>>d;
if(d=='x')
{
for(i=0;i
{
xa1[i]=xa[i]+shx*(ya[i]-yref);
ya1[i]=ya[i];
}
}
cleardevice();
cout<<"before shearing";
for(i=0;i
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after shearing";
for(i=0;i
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case
5:
cout<<"before reflection";
for(i=0;i
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after reflection";
for(i=0;i
{
line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);
}
getch();
cleardevice();
break;
case
6:
exit(0);
break;
}
}while(op!=6);
}
OUTPUT :
enter the no of points: 3
enter the coordinates 1: 50 150
enter the coordinates 2: 50 50
enter the coordinates 3: 75 150
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
1
enter the translation vector: 30 40
BEFORE TRANSLATION AFTER TRANSLATION
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
2
enter the rotation angle: 60
enter the reference points: 30 40
BEFORE
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
3
Enter the scaling factor: 3 4
Enter the reference points: 30 40
BEFORE AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
4
Enter the shear value: 3 4
Enter the reference point: 20 30
Enter the shear direction x or y: X
BEFORE
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
5
BEFORE
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
6
No comments:
Post a Comment