Wednesday, November 17, 2010

Program to show rotation,reflection, and shearing in 2D





//Program to show rotation,reflection, and shearing in 2D

#include
#include
#include
#include
void rotate();
void shear();
void reflect();
void main()
{
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"e:\\tc\\bgi");

setcolor(WHITE);
line(100,100,200,100);
line(200,100,200,300);
line(200,300,100,100);
printf("---MENU---");
printf("\n 1)Rotate\n 2)Reflect\n 3)Shear");
printf("\nEnter your choice: ");
scanf("%d",&ch);
cleardevice();

switch(ch)
{
case 1: rotate();
break;
case 2: reflect();
break;
case 3: shear();
break;
default: printf("you have enterd wrong choice");
break;
}
getch();
closegraph();
}

void rotate()
{
float theta;
int x1,x2,x3;
int y1,y2,y3;
int ax1,ax2,ax3,ay1,ay2,ay3;
int refx,refy;
printf("\nEnter the angle for rotation: ");
scanf("%f",&theta);
cleardevice();
setcolor(WHITE);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
printf("Theta=%d",(int)theta);
theta=theta*(3.14/180);
x1=100;
y1=100;
x2=200;
y2=100;
x3=200;
y3=300;

ax1=refx+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);

ax2=refx+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);

ax3=refx+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);

setcolor(BROWN);
line(100,100,200,100);
line(200,100,200,300);
line(200,300,100,100);

setcolor(WHITE);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax1,ay1);
}

void shear()
{
float shx;
int x1,x2,x3;
int y1,y2,y3;
int ax1,ax2,ax3,ay1,ay2,ay3;
int refy;
printf("\nEnter the shear parameter : ");
scanf("%f",&shx);
cleardevice();
setcolor(WHITE);
outtextxy(240,10,"SHEAR");
outtextxy(238,20,"-------");
refy=100;
printf("shx=%f\nyref=%d",shx,refy);

x1=100;
y1=100;
x2=200;
y2=100;
x3=200;
y3=300;

ax1=x1+shx*(y1-refy);
ay1=y1;

ax2=x2+shx*(y2-refy);
ay2=y2;

ax3=x3+shx*(y3-refy);
ay3=y3;

setcolor(BROWN);
line(100,100,200,100);
line(200,100,200,300);
line(200,300,100,100);
setcolor(WHITE);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax1,ay1);
}

void reflect()
{
int x1,x2,x3;
int y1,y2,y3;
int ax1,ax2,ax3,ay1,ay2,ay3;
int refx;
cleardevice();
setcolor(WHITE);
outtextxy(240,10,"REFLECT(about x=320)");
outtextxy(238,20,"---------------------");
refx=320;
x1=100;
y1=100;
x2=200;
y2=100;
x3=200;
y3=300;
ax1=640-x1;
ay1=y1;
ax2=640-x2;
ay2=y2;
ax3=640-x3;
ay3=y3;
setcolor(BROWN);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
setcolor(WHITE);
line(320,50,320,400);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax1,ay1);
}

No comments:

Post a Comment