2D Transformations > C Program
Computer Graphics
2D Transformations > C Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,x2,x3,y1,y2,y3,x11,x22,x33,y11,y22,y33,xc,yc,xmax,ymax;
void display()
{
line(0,yc,xmax,yc);
line(xc,0,xc,ymax);
line(xc+x11,yc-y11,xc+x22,yc-y22);
line(xc+x22,yc-y22,xc+x33,yc-y33);
line(xc+x33,yc-y33,xc+x11,yc-y11);
}
void translate()
{
int tx,ty;
printf("Enter translation factor: tx,ty\n");
scanf("%d%d",&tx,&ty);
x11=x1+tx;
y11=y1+ty;
x22=x2+tx;
y22=y2+ty;
x33=x3+tx;
y33=y3+ty;
display();
}
void scale()
{
float sx,sy;
printf("Enter scaling factor:\n");
scanf("%f%f",&sx,&sy);
x11=x1*sx;
x22=x2*sx;
x33=x3*sx;
y11=y1*sy;
y22=y2*sy;
y33=y3*sy;
display();
}
void shearx()
{
int shx;
printf("Enter the shear factor:\n");
scanf("%d",&shx);
x11=x1+shx*y1;
x22=x2+shx*y2;
x33=x3+shx*y3;
y11=y1;
y22=y2;
y33=y3;
display();
}
void sheary()
{
int shy;
printf("Enter the shear factor:\n");
scanf("%d",­);
x11=x1
x22=x2;
x33=x3;
y11=y1+shy*x1;
y22=y2+shy*x2;
y33=y3+shy*x3;
display();
}
void rotate()
{
double theta;
printf("Enter the angle:\n");
scanf("%f",theta);
theta=theta*(3.14/180.0);
x11=x1*cos(theta)-y1*sin(theta);
y11=x1*sin(theta)+y1*cos(theta);
x22=x2*cos(theta)-y2*sin(theta);
y22=x2*sin(theta)+y2*cos(theta);
x33=x3*cos(theta)-y3*sin(theta);
y33=x3*sin(theta)+y3*cos(theta);
Display();
}
void refx()
{
y11=-y1;
y22=-y2;
y33=-y3;
x11=x1;
x22=x2;
x33=x3;
Display();
}
void refy()
{
x11=-x1;
x22=-x2;
x33=-x3;
y11=y1;
y22=y2;
y33=y3;
Display();
}
void refo()
{
x11=-x1;
x22=-x2;
x33=-x3;
y11=-y1;
y22=-y2;
y33=-y3;
Display();
}
void main()
{
int gd=DETECT,gm,op,r;
clrscr();
printf("Enter co-ordinates for triangle:\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
xmax=getmaxx();
ymax=getmaxy();
xc=xmax/2;
yc=ymax/2;
do
{
printf("Enter option:\n");
printf("1. Translation\n 2. Scaling\n 3. Rotation\n 4.Reflection\n 5.Shear\n");
scanf("%d",&op);
line(0,yc,xmax,yc);
line(xc,0,xc,ymax);
line(xc+x1,yc-y1,xc+x2,yc-y2);
line(xc+x2,yc-y2,xc+x3,yc-y3);
line(xc+x3,yc-y3,xc+x1,yc-y1);
switch(op)
{
case 1:translate();
break;
case 2:scale();
break;
case 3:rotate();
break;
case 4:
printf("\n1.About X \n 2.About Y \n 3.About Origin\nEnter your choice:\n");
scanf("%d",&r);
switch(r)
{
case 1:
refx();
break;
case 2:
refy();
case 3:
refo();
break;
}
break;
case 5:
printf("\n1.About X\n2.About Y\nEnter your choice:\n");
scanf("%d",&r);
switch(r)
{
case 1:
shearx();
break;
case 2:
sheary();
break;
}
break;
default:printf("Invalid Option");
exit(0);
}
}
while(op<=4);
getch();
}
Comments
Post a Comment