Write a C Program to perform 2D Transformations in Rotation

By | 02.12.2016

Perform 2D Transformations in Rotation


Write a C Program to perform 2D Transformations in Rotation. Here’s simple Program to perform 2D Transformations in Rotation in C Programming Language.


Below is the source code for C Program to perform 2D Transformations in Rotation which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to perform 2D Transformations in Rotation  */

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.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();
}

Above is the source code for C Program to perform 2D Transformations in Rotation which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

2 1 vote
Article Rating
Category: C Programming Graphic Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments