Search This Blog

C Program for Rotation of Polygon - 2D Transformation in Computer Graphics

This is a c program for rotation transformation in computer graphics. Rotation is one of the important 2d transformations in computer graphics. The program will tell you how to rotate points or polygon around a point (the pivot point). This CG lab program in c language using the graphics library reads the number of sides of polygon, co-ordinates of its vertices, the pivot point for rotation, and angle of rotation. It displays the original polygon and rotated polygon in different colors in same screen. More Computer graphics lab problems are available under related posts heading.



#include<stdio.h>
#include<graphics.h>
#include<math.h>

int graDriver=DETECT,graMode;
int n,xs[100],ys[100],i,xp,yp,degree;
float radian;

void rotation();
void DrawFn();

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

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("\nenter pivot point co-ordinate");
scanf("%d%d",&xp,&yp);
printf("\nenter rotation angle");
scanf("%d",&degree);
degToRad();
initgraph(&graDriver,&graMode,"C:\\TURBOC3\\BGI\\");
cleardevice();
//Drawing original in RED color
setcolor(RED);
DrawFn();
//Doing rotation
rotation();
//Drawing rotated polygon in BLUE color
setcolor(BLUE);
DrawFn();
getch();
}

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

void rotation()
{
float t,v;
for(i=0;i<n;i++)
 {
 t=xs[i]-xp;
 v=ys[i]-yp;
 xs[i]=xp+floor(t*cos(radian)-v*sin(radian));
 ys[i]=yp+floor(v*cos(radian)+t*sin(radian));
 }
}
The above program first reads the polygon. Then original polygon is displayed in red color. Then the rotated one is displayed in blue color.

Related Posts

C Program to Scale Polygons - Scaling Transformation in 2D Computer Graphics
C Program for Translation 2D Transformation in Computer Graphics 2D Transformations C Program - Computer Graphics Lab 2D Transformation
Two Stroke Engine Piston Movement Animation C Program - CG Lab Program DDA Line Drawing Program in C : Computer Graphics Lab Program
Computer Graphics Program in C to Simulate Motion of Simple Pendulum
Blooming Flower and Bee Animation C Program - Computer Graphics Lab Experiment
Caterpillar Eating Leaf - Computer Graphics Lab Animation in C

No comments: