C Program For 2D Transformation of Rotation

0
Program Name: 2 D Rotation

Program Language:c

Program Requirements:C++ Or C Program Compilers

Program Description:

A two-dimensional rotation is applied to an object by re-positioning 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.To know more on what rotation is and its matrix representation see 2 D Transformations Rotation.

Source Code Name: 2d.c


Code:
#include<stdio.h>

#include<graphics.h>
#include<conio.h>

#include<process.h>

void rotate( int figure[], int edges, double angle, int cx, int cy )

{

        double x, y;

        angle = -1 * (angle*3.14/180);

        double cos_a = cos(angle);

        double sin_a = sin(angle);

        for(int i=0; i < edges; i++)

        {

               x = figure[2*i] - cx;

               y = figure[2*i+1] - cy;

               figure[2*i] = ceil( (x * cos_a) - (y * sin_a) + cx );

               figure[2*i+1] = ceil( (x * sin_a)+(y * cos_a) + cy );

        }

}

void main()

{

        int figure[20], edges;  // A Figure with Max 10 edges.

        double angle;

        int cx=0, cy=0;

        int gd = DETECT, gm;

        initgraph( &gd, &gm, "" );

        int max_y = getmaxy();

        clrscr();

        cleardevice();

        printf( "Number of
edges: " );

        scanf( "%d", &edges );

        for(int i=0; i < edges; i++)

        {

               printf( "Enter edge
(x%d,y%d) : ", i , i );

               scanf( "%d %d", &figure[2*i], &figure[2*i+1] );

        }

        figure[2*i] = figure[0];

        figure[2*i+1] = figure[1];

        edges += 1;

        printf( "Enter angle
of rotation in degrees: ");

        scanf( "%lf", &angle);

        printf( "Enter the
center of rotation: \n");

        printf( "cx: ");

        scanf( "%d", &cx);

        printf( "cy: ");

        scanf( "%d", &cy);

        cy = max_y - cy;

        cleardevice();

        setbkcolor(WHITE);

        setcolor(GREEN);

        setlinestyle(SOLID_LINE, 0, 3);

        drawpoly( edges, figure );

        getch();

        for(int i=0; i < edges; i++)

               figure[2*i+1] = max_y - figure[2*i+1];

        rotate(figure,edges,angle,cx,cy);

        for(int i=0; i < edges; i++)

               figure[2*i+1] = max_y - figure[2*i+1];

        setcolor(RED);

        drawpoly( edges, figure );

        getch();

}
Heart
Make a Great History

    C Program For 2D Transformation of Rotation