Search This Blog

2D Transformations C Program - Computer Graphics Lab 2D Transformation

The main two dimensional transformation operations in computer graphics are: translation or shifting, scaling, rotation, reflection (or flipping) and shearing. Here in this c program, we can do all these transformations on any polygon. Rotation can be done about any pivot point. Reflection includes reflection about horizontal axis (flip horizontal) and reflection about vertical axis (flip vertical). Similarly, shear transformation is also done about horizontal axis (x-axis) and vertical axis (y-axis). The input to the program are number of sides of polygon and its vertices.



#include<stdio.h>
#include<graphics.h>
#include<math.h>
int graDriver=DETECT,graMode;
int n,xs[100],ys[100],i,xinc,yinc,xp,yp,degree;
float sfx,sfy,radian,shearXfactor,shearYfactor;
int tempYaxis,tempXaxis;

float degToRad()
{
radian=(float)degree*3.14f/180;
}

void DrawFn()
{
for(i=0;i<n;i++)
 {
 line(xs[i],ys[i],xs[(i+1)%n],ys[(i+1)%n]);
 }
}

void translate()
{
for(i=0;i<n;i++)
 {
 xs[i]+=xinc;
 ys[i]+=yinc;
 }
setcolor(BLUE);
}

void rotation()
{
float sinrad,cosrad,t,u;
for(i=0;i<n;i++)
 {
 sinrad=sin(radian);
 cosrad=cos(radian);
 t=(ys[i]-yp);
 u=(xs[i]-xp);
 xs[i]=xp+(int)floor(u*cosrad-t*sinrad);
 ys[i]=yp+(int)floor(u*sinrad+t*cosrad);
 }
setcolor(RED);

}
void scaleFn()
{
for(i=0;i<n;i++)
 {
 xs[i]=xs[0]+(int)((float)(xs[i]-xs[0])*sfx);
 ys[i]=ys[0]+(int)((float)(ys[i]-ys[0])*sfy);
 }
setcolor(GREEN);
}

void shearAlongX()
{
for(i=0;i<n;i++)
 {
 xs[i]=xs[i]+shearXfactor*ys[i];
 }
setcolor(BROWN);
}

void shearAlongY()
{
for(i=0;i<n;i++)
 {
 ys[i]=ys[i]+shearYfactor*xs[i];
 }
setcolor(YELLOW);
}

void FlipV()
{
tempXaxis=getmaxy()/2;
for(i=0;i<n;i++)
 {
 ys[i]=tempXaxis+(tempXaxis-ys[i]);
 }
for(i=0;i<getmaxx();i++)
 {
 putpixel(i,tempXaxis,12);
 }
setcolor(WHITE);
}

void FlipH()
{
tempYaxis=getmaxx()/2;
for(i=0;i<n;i++)
 {
 xs[i]=tempYaxis+(tempYaxis-xs[i]);
 }
for(i=0;i<getmaxy();i++)
 {
 putpixel(tempYaxis,i,14);
 }
setcolor(BLUE);
}

void main()
{
printf("Enter number of sides: ");
scanf("%d",&n);
printf("Enter co-rdinates: x,y for each point ");
for(i=0;i<n;i++)
 scanf("%d%d",&xs[i],&ys[i]);
printf("Enter x,y increment for translation: ");
scanf("%d%d",&xinc,&yinc);
printf("Enter scale factors: (xs,ys) ");
scanf("%f%f",&sfx,&sfy);
printf("Enter rotation angle");
scanf("%d",&degree);
printf("degree: %d",degree);
degToRad();
printf("radian: %f",radian);
printf("Enter pivot co-co-ordinates:");
scanf("%d%d",&xp,&yp);
xp+=xinc;
yp+=yinc;
printf("Enter x shear factor:");
scanf("%f",&shearXfactor);
printf("Enter y shear factor:");
scanf("%f",&shearYfactor);


//flipping horizontally
initgraph(&graDriver,&graMode,"");
cleardevice();
setcolor(4);
DrawFn();//original
translate();
DrawFn();//shifti
scaleFn();
DrawFn();//scaling
rotation();
DrawFn();//rotate
shearAlongX();
DrawFn();//Xshear
shearAlongY();
DrawFn();//Yshear
FlipV();
DrawFn();//vertical flip
FlipH();
DrawFn();//Horizontal flip

while(1)
{
delay(500);
}
getch();
getch();
getch();

}

The above program first reads the polygon. Then performs 2d transformation operations one by one. Each transformation is applied on the result of the previous transformation the function DrawFn() draws the polygon on screen after each 2d transformation operation to show its result.

Related Posts:

No comments: