Saturday, May 10, 2008

Comuter graphics 2D transformation


#include " stdio.h "
#include " conio.h "
#include " math.h "
#include " graphics.h "
#include " stdlib.h "
#include " ctype.h "

void intgraph();
void display_cordinate(char bg_color,char line_color);
int Create_poly(int poly[][2]);
void fill_poly(int poly[][2],int points,char line_color,char fill_color);
void Identity(float Matrix[3][3]);
void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis);

int instance=0;
int current=-1;


void main()
{
int poly[10][10][2],i,points[10],Xdis[10],Ydis[10];
float Sx,Sy,Matrix[10][3][3],temp,Cos,Sin;
char ch;
intgraph();

setfillstyle(8,RED);
points[current]=Create_poly(poly[current+1]);
display_cordinate(DARKGRAY,WHITE);
fill_poly(poly[current],points[current],BLUE,YELLOW);

Identity(Matrix[current]);
getch();
Xdis[current]=0-poly[current][0][0];
Ydis[current]=0-poly[current][0][1];

Translate_to_Relative(poly[current],points[current],Xdis[current],Ydis[current]);

Menu:
restorecrtmode();
do
{
clrscr();
instance=0;
printf("\t\t0. Instance transformation\n");
printf("\t\t1. Scaling\n");
printf("\t\t2. Translation\n");
printf("\t\t3. Rotation\n");
printf("\t\t4. Reflection [ X axis ]\n");
printf("\t\t5. Reflection [ Y axis ]\n");
printf("\t\t6. Reflection [ X=Y axis ]\n");
printf("\t\t7. Draw \n");
printf("\t\t8. New input set\n");
printf("\t\t9. EXIT \n");


printf("\n\nChoice: ");
ch=getche();
}while((ch < '0')||(ch > '9'));

switch(ch)
{
case '0':
//printf("Available instances are:");
instance=1;
goto babu;
case '1':
printf("\n\tScaling Ratio: ");
printf("\n\tSx: ");
scanf("%f",&Sx);
printf("\n\tSy: ");
scanf("%f",&Sy);
for(i=0;i<3;i++)
{
Matrix[current][i][0]=Matrix[current][i][0]*Sx;
Matrix[current][i][1]=Matrix[current][i][1]*Sy;
}
goto Menu;

case '2':
printf("\n\tTranslation : ");
printf("\n\tTx: ");
scanf("%f",&Sx);
printf("\n\tTy: ");
scanf("%f",&Sy);

Matrix[current][2][0]=Matrix[current][2][0]+Sx;
Matrix[current][2][1]=Matrix[current][2][1]+Sy;

goto Menu;

case '3':
printf("\nRotation Angle [in Degree]: ");
scanf("%f",&Sx);

Sx=(Sx*3.14159265358)/180.0;
Cos=cos(Sx);
Sin=sin(Sx);
for(i=0;i<=2;i++)
{
temp=Matrix[current][i][0]*Cos-Matrix[current][i][1]*Sin;
Matrix[current][i][1]=Matrix[current][i][0]*Sin+Matrix[current][i][1]*Cos;
Matrix[current][i][0]=temp;
}
goto Menu;

case '4':
Matrix[current][1][1]=Matrix[current][1][1]*-1;
printf("\t\tCompleted");
getch();

goto Menu;

case '5':
Matrix[current][0][0]=Matrix[current][0][0]*-1;
printf("\t\tCompleted");
getch();

goto Menu;

case '6':
Matrix[current][0][1]=1;
Matrix[current][1][1]=1;
printf("\t\tCompleted");
getch();
goto Menu;

case '7':
if(!instance)
{int j =current;
for(i=0;i<=points[j];i++)
{
temp=poly[j][i][0]*Matrix[j][0][0]+poly[j][i][1]*Matrix[j][1][0]+Matrix[j][2][0];
poly[j][i][1]=poly[j][i][0]*Matrix[j][0][1]+poly[j][i][1]*Matrix[j][1][1]+Matrix[j][2][1];
poly[j][i][0]=temp;
}

}

goto babu;

case '8':
points[current]=Create_poly(poly[current+1]);
Identity(Matrix[current]);
Xdis[current]=0-poly[current][0][0];
Ydis[current]=0-poly[current][0][1];
Translate_to_Relative(poly[current],points[current],Xdis[current],Ydis[current]);
goto Menu;

case '9':
getch();
exit(0);

}
babu:
setgraphmode(2);
display_cordinate(DARKGRAY,WHITE);

if(instance)
{
for(int u=0;u<=current;u++)
{
Translate_to_Relative(poly[u],points[u],-Xdis[u],-Ydis[u]);
fill_poly(poly[u],points[u],BLUE,YELLOW);
//getch();
//printf("1");
}
instance=0;
}
else
{ Translate_to_Relative(poly[current],points[current],-Xdis[current],-Ydis[current]);
fill_poly(poly[current],points[current],BLUE,YELLOW);
}
getch();
restorecrtmode();
do
{
clrscr();
printf("\n\tDo you Want to Switch to MENU [Y|N]:- ");
ch=toupper(getche());
}while( (ch!='Y') && (ch!='N') );
if(ch=='Y')
{
//Identity(Matrix[current]);
Xdis[current]=0-poly[current][0][0];
Ydis[current]=0-poly[current][0][1];

Translate_to_Relative(poly[current],points[current],Xdis[current],Ydis[current]);
goto Menu;
}
closegraph();
}

void intgraph()
{
int g=DETECT,d;
initgraph(&g,&d,"e:\\tc\\bgi");
}

void fill_poly(int poly1[][2],int points,char line_color,char fill_color)
{
int pol[20],i;
char str[2];
int fac=5;
for(i=0;i<=points;i++)
{
pol[i*2]=320+fac*poly1[i][0];
pol[i*2+1]=240-fac*poly1[i][1];
}
pol[i*2]=320+fac*poly1[0][0];
pol[i*2+1]=240-fac*poly1[0][1];

setcolor(line_color);
if(points>2)
setfillstyle(1,fill_color);
fillpoly(points,pol);


}

void Identity(float Matrix[3][3])
{
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==j)
Matrix[i][j]=1;
else
Matrix[i][j]=0;
}
}
}
int Create_poly(int poly[][2])
{
//printf("%d %d\n\n",getmaxx(),getmaxy());//640 480
current++;
int total_point;
printf("No of vertices: ");
scanf("%d",&total_point);

for(int i=0;i < total_point;i++)
{
printf("Co-ordinate [%d]: ",i+1);
scanf("%d %d",&poly[i][0],&poly[i][1]);

}

return total_point;
}

void display_cordinate(char bg_color,char line_color)
{
int i;
setbkcolor(bg_color);
setcolor(line_color);
for(i=0;i<=640;i+=5)
{
line(i,0,i,480);
}

for(i=0;i<=480;i+=5)
{
line(0,i,640,i);
}

rectangle(0,0,639,479);

setcolor(RED);
line(0,240,640,240);
line(320,0 ,320,480);
}

void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis)
{
int i;
for(i=0;i<=points;i++)
{
poly[i][0]=poly[i][0]+Xdis;
poly[i][1]=poly[i][1]-Ydis;
}
}

PC Magazine Tips and Solutions

PC World: Latest Technology News

PCWorld.com - Most Popular Downloads of the Week