Showing posts with label two dimensional transformation using c. Show all posts
Showing posts with label two dimensional transformation using c. Show all posts

Saturday, January 16, 2010

two dimensional transformation





Download this file : 2d.c




program:
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
int gd=0,gm,theta,xf,yf,x1,y1,x2,y2,xa1,xa2,ya1,ya2,a;
int midx,midy,tx,ty,choice,sx,sy,sh,xa[10],ya[10],first;
int i,n=5;
int xf,yf;
int xb[10],yb[10];
void main()
{
do
{
first=0;
org();
getch();
printf("\n1.translation\n2.rotation\n3.scaling\n4.reflection\n5.shear\n6.exit\nenter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the translation factor (x,y) :");
scanf("%d%d",&tx,&ty);
org();
rectangle(midx+tx,midy-100+ty,midx+100+tx,midy+ty);
getch();
closegraph();
break;
case 2:
printf("Enter the angle :");
scanf("%d",&a);
org();
rotation();
break;
case 3:
printf("Enter the scaling factor (x,y):");
scanf("%d%d",&sx,&sy);
org();
x1=0;
y1=-100;
x2=100;
y2=0;
xa1=x1*sx+midx;
ya1=y1*sy+midy;
xa2=x2*sx+midx;
ya2=y2*sy+midy;
rectangle(xa1,ya1,xa2,ya2);
getch();
closegraph();
break;
case 4:
org();
rectangle(midx,midy,midx+100,midy+100);
getch();
closegraph();
break;
case 5:
printf("enter the shear value");
scanf("%d",&sh);
cleardevice();
org();
for(i=0;i < n;i++)
{
xb[i]=xa[i]+sh*(ya[i]-yf);
yb[i]=ya[i];
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
}
}while(choice<6);
getch();
}
org()
{
initgraph(&gd,&gm,"");
midx=getmaxx()/2;
midy=getmaxy()/2;
xf=midx;
yf=midy;
xa[0]=midx;
ya[0]=midy-100;
xa[1]=midx+100;
ya[1]=midy-100;
xa[2]=midx+100;
ya[2]=midy;
xa[3]=midx;
ya[3]=midy;
xa[4]=midx;
ya[4]=midy-100;
cleardevice();
setcolor(WHITE);
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
setcolor(4);
if(first==0)
{
setlinestyle(SOLID_LINE,1,1);
first=1;
}
else
{
setlinestyle(DASHED_LINE,1,1);
}
rectangle(midx,midy-100,midx+100,midy);
setlinestyle(SOLID_LINE,1,1);
return;
}
rotation()
{
theta=(a*2*3.14)/180;
for(i=0;i < n;i++)
{
xb[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
yb[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
cleardevice();
return;
}


Download this file : 2d.c

Output: