Monday, September 23, 2013

Tagged Under:

TWO DIMENSIONAL 2-D ROTATION C PROGRAM

Share


TWO DIMENSIONAL 2-D  ROTATION

A two-dimensional rotation is applied to an object by repositioning it along a circular path in the x-y plane. When we generate a rotation we get a rotation angle (θ) and the position about which the object is rotated (xr , yr) this is known as rotation point or pivot point. The transformation can also be described as a rotation about rotation axis that is perpendicular to x-y plane and passes through the pivot point. Positive values for the rotation angle define counter-clockwise rotations about the pivot point and the negative values rotate objects in the clockwise direction

TWO DIMENSIONAL 2-D  ROTATION C PROGRAM

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
 
 
void rotate( int image[], int edge, double angle, int xc, int yc )
{
 double x, y;
 angle = -1 * (angle*3.14/180);
 double cos_a = cos(angle);
 double sin_a = sin(angle);
 
 for(int i=0; i < edge; i++)
 {
  x = image[2*i] - xc;
  y = image[2*i+1] - yc;
  image[2*i] = ceil( (x * cos_a) - (y * sin_a) + xc );
  image[2*i+1] = ceil( (x * sin_a)+(y * cos_a) + yc );
 }
}
 
void main()
{
 int image[20], edge;  // A image with Max 10 edge.
 double angle;
 int xc=0, yc=0;
 int gd = DETECT, gm;
 initgraph( &gd, &gm, "" );
 int ymax = getmaxy();
 clrscr();
 cleardevice();
 
 printf( "Number of edge: " );
 scanf( "%d", &edge );
 
 for(int i=0; i < edge; i++)
 {
  printf( "Enter edge (x%d,y%d) : ", i , i );
  scanf( "%d %d", &image[2*i], &image[2*i+1] );
 }
 image[2*i] = image[0];
 image[2*i+1] = image[1];
 edge += 1;
 
 printf( "Enter angle of rotation in degrees: ");
 scanf( "%lf", &angle);
 
 printf( "Enter the center of rotation: \n");
 printf( "xc: ");
 scanf( "%d", &xc);
 printf( "yc: ");
 scanf( "%d", &yc);
 yc = ymax - yc;
 
 cleardevice();
 setbkcolor(WHITE);
 
 setcolor(GREEN);
 setlinestyle(SOLID_LINE, 0, 3);
 drawpoly( edge, image );
 getch();
 
 for(int i=0; i < edge; i++)
  image[2*i+1] = ymax - image[2*i+1];
 rotate(image,edge,angle,xc,yc);
 for(int i=0; i < edge; i++)
  image[2*i+1] = ymax - image[2*i+1];
 
 setcolor(RED);
 drawpoly( edge, image );
 getch();
}
}