2D TRANSFORMATION

AIM:

To perform the 2D transformation such as translation, rotation, scaling, shearing,
Reflection

FUNCTIONS USED:

Line()
The function line() is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

ALGORITHM:

Step1: Declare the variables xa,ya,xa1,ya1 of array type.
Step2:Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step3: Initialise the graphics function.
Step4: Input the number of points.
Step5: Input the value of co-ordinate according to number of points.
Step6. Using switch statement selects the option to perform translation, rotation, scaling,    reflection and shearing.
Step7: Translation:
a).input the translation vector
b).add the translation vectors with the coordinates
xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,
c).using the function line,display the object before and after translation.
Step8: Rotation
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the value of reference point
d). calculate new coordinate point using formula
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),
e). using the function line,display the object before and after rotation.
Step9: Scaling:
a).input the scaling factor and reference point
b).calculate new coordinate point using formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the object before and after scaling.
Step10: Shearing:
a).input the shearing value and reference point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)
iii). using the function line, display the object before and after shearing.
Step11: Reflection:
a).display the object before  reflection using the function line
b). display the object after reflection using the function line
Step12: Stop.

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<<"\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;
}
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);
}
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);
}
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];
}
}
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);
}

INPUT & 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

1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit1
enter the translation vector:30 40

Before translation                                After Translation

1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit 2

enter the rotation angle:40

enter the reference points:100 100

before rotation            after  rotation

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 scaling                                       after scaling

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 shearing                          After shearing

1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit  5

Before reflection after reflection

1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit  6

RESULT:
Thus the program is executed and verified.

Composite 2D Transformations Computer graphics Lab Reviewed by Rejin Paul on 2:16 AM Rating: 5

1. where is the header files and for loops

2. Find the Full manual link in the website dude

3. www.labmanual.blogspot.com

4. will u upload all the computer graphics and open source lab programs..really im a most visited person...so pls help me

5. Anonymous5:24 AM

could u upload the composite 2d transformation program using fixed point and pivot point concept