Your Ad Here

Friday, October 10, 2008

Translation,Scaling,Rotation & Shreaing of a polygon

Translation,Scaling,Rotation & Shreaing of a polygon with moving effect...

#include stdio.h
#include conio.h
#include graphics.h
#include math.h
#include dos.h
void draw();
void graph();
int n,w;
int i,m=1;
double an,an1;
double x[10],y[10],a[10],b[10],tempx=0,tempy=0;
void main()
{
int ch,n1;
double tx,ty,x1,y1,sx,sy,sx1,sy1,sx2,sy2,tx1,ty1;
// int gd=DETECT,gm;
//initgraph(&gd,&gm,"C:\\tc\\bgi");
while(m==1)
{
printf("Enter the no. of cordinates(Not more then 10):-");
scanf("%d",&n);
if (n<=10)
{
m=0;
}
}
for(i=0;i {
printf("Enter the x cordinate of point %d :-",i+1);
scanf("%lf",&x[i]);
printf("Enter the y cordinate of point %d :-",i+1);
scanf("%lf",&y[i]);
}
//cleardevice();
graph();
draw(x,y);
getch();
closegraph();
while(1)
{
//getch();
//cleardevice();
//closegraph();
printf("\n1>For Translation of figure.");
printf("\n2>For Rotation of figure.");
printf("\n3>For Scaling of figure.");
printf("\n4>Exit.");
printf("\nEnter the choice:- ");
scanf("%d",&ch);
//cleardevice();
//clrscr();
switch(ch)
{
case 1:
printf("\nEnter tx for x-axis translation:-");
scanf("%lf",&tx);
printf("\nEnter ty for y-axis translation:-");
scanf("%lf",&ty);
tx1=tx/10;
ty1=ty/10;
graph();
//delay(500);
for(w=1;w<=10;w++)
{
cleardevice();
for(i=0;i {
x[i]=x[i]+tx1;
y[i]=y[i]+ty1;
}
draw();
delay(1000);
//cleardevice();
}
break;
case 2:
printf("Enter the value of Angle for rotation:-");
scanf("%lf",&an);
an=((an*3.14)/180);
printf("\n1>For simple Rotation.");
printf("\n2>for fixed point Rotation.");
printf("\nEnter your choice:-");
scanf("%d",&n1);
if (n1==1)
{
x1=0;
y1=0;
}
else
{
printf("Enter x value for the point of rotation:-");
scanf("%lf",&x1);
printf("Enter y value for the point of rotation:-");
scanf("%lf",&y1);
}
an1=an/10;
graph();
for(w=0;w<=10;w++)
{
cleardevice();
for(i=0;i {
tempx=x[i];
tempy=y[i];
x[i]=x1+((tempx-x1)*cos(an1))-((tempy-y1)*sin(an1));
y[i]=y1+((tempx-x1)*sin(an1))+((tempy-y1)*cos(an1));
}
draw();
delay(1000);
}
for(i=0;i {
// tempx=x[i];
// tempy=y[i];
x[i]=x1+((a[i]-x1)*cos(an))-((b[i]-y1)*sin(an));
y[i]=y1+((a[i]-x1)*sin(an))+((b[i]-y1)*cos(an));
}
break;
case 3:
printf("\nEnter the scaling factor for x direction:-");
scanf("%lf",&sx);
printf("\nEnter the scaling factor for y direction:-");
scanf("%lf",&sy);
printf("\n1>For simple scaling.");
printf("\n2>for fixed point scaling.");
printf("\nEnter your choice:-");
scanf("%d",&n1);
if (n1==1)
{
x1=0;
y1=0;
}
else
{
printf("\nEnter the value of x for fixed point:-");
scanf("%lf",&x1);
printf("\nEnter the value of y for fixed point:-");
scanf("%lf",&y1);
}
sx=sx-1;
sy=sy-1;
sx1=sx/10;
sy1=sy/10;
sx2=sx1;
sy2=sy1;
for(i=0;i {
a[i]=x[i];
b[i]=y[i];
// printf("%lf",a[i]);
//getch();
}
graph();
for(w=1;w<=10;w++)
{
cleardevice();
for(i=0;i {
//tempx=x[i];
//tempy=y[i];
x[i]=(a[i]*(1+sx2))+(x1*(1-(1+sx2)));
y[i]=(b[i]*(1+sy2))+(y1*(1-(1+sy2)));
}
draw();
delay(400);
sx2=sx2+sx1;
sy2=sy2+sy1;
}
break;
case 4:
exit();
break;
}
draw();
getch();
closegraph();
}
}

/*
{
for(i=1;i {
tempx=x[i];
tempy=y[i];
//printf("%d %d \n",tempx,tempy);
printf("\nx%lf %lf %lf",x[i],y[i],cos(an));
x[i]=(tempx*cos(an))-(tempy*sin(an))+(x[0]*(1-cos(an)))+(y[0]*sin(an));
x[i]=(tempx*sin(an))+(tempy*cos(an))+(y[0]*(1-cos(an)))-(x[0]*sin(an));
printf("\n%lf %lf\n ",x[i],y[i]);
}
}
//cleardevice();
*/


void draw()
{
//cleardevice();
for(i=0;i {
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[i],y[i],x[0],y[0]);
delay(100);
}
void graph()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
}

No comments: