2D Rotation Program Using C Programming

🎯 Introduction

In computer graphics, transformations play a crucial role in manipulating objects on the screen. One such fundamental transformation is rotation, which involves rotating an object around a specific point or axis by a given angle. In this blog post, we will delve into a C program that demonstrates how to rotate a 2D triangle using rotation transformation techniques. We will explore the concept of rotation, discuss the details of the program, and provide a step-by-step explanation of the code. By the end of this tutorial, you will have a clear understanding of how to rotate 2D objects using C programming.

🎯 Concept

Rotation in 2D space can be achieved using rotation formulas based on trigonometry. For a 2D point (x, y) and a rotation angle θ (in radians), the rotated coordinates (x', y') can be computed as follows:

x' = x * cos(θ) - y * sin(θ)

y' = x * sin(θ) + y * cos(θ)

In our program, we take three points as input to form a triangle, and then we allow the user to specify an angle for rotation. The program then performs the rotation and displays the rotated triangle on the screen.

🎯 2D triangle using rotation transformation Program

C program for rotating a 2D triangle using rotation transformation techniques:


#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <math.h>


void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3);

void RotateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float angle);


int main()

{

    int gd = DETECT, gm;

    int x1, y1, x2, y2, x3, y3;

    float angle;


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


    printf("Enter the 1st point for the triangle (x1 y1): ");

    scanf("%d%d", &x1, &y1);


    printf("Enter the 2nd point for the triangle (x2 y2): ");

    scanf("%d%d", &x2, &y2);


    printf("Enter the 3rd point for the triangle (x3 y3): ");

    scanf("%d%d", &x3, &y3);


    DrawTriangle(x1, y1, x2, y2, x3, y3);


    printf("Enter the angle for rotation (in degrees): ");

    scanf("%f", &angle);


    RotateTriangle(x1, y1, x2, y2, x3, y3, angle);


    getch();

    closegraph();

    return 0;

}


void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3)

{

    line(x1, y1, x2, y2);

    line(x2, y2, x3, y3);

    line(x3, y3, x1, y1);

}


void RotateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float angle)

{

    int p = x2, q = y2;

    float radianAngle = (angle * 3.14) / 180.0;


    int a1 = p + (x1 - p) * cos(radianAngle) - (y1 - q) * sin(radianAngle);

    int b1 = q + (x1 - p) * sin(radianAngle) + (y1 - q) * cos(radianAngle);


    int a2 = p + (x2 - p) * cos(radianAngle) - (y2 - q) * sin(radianAngle);

    int b2 = q + (x2 - p) * sin(radianAngle) + (y2 - q) * cos(radianAngle);


    int a3 = p + (x3 - p) * cos(radianAngle) - (y3 - q) * sin(radianAngle);

    int b3 = q + (x3 - p) * sin(radianAngle) + (y3 - q) * cos(radianAngle);


    setcolor(1);

    DrawTriangle(a1, b1, a2, b2, a3, b3);

}

🎯 Detailed Explanation

Let's dive into the details of the code:

Header Files and Main Function

The code starts by including the necessary header files: <stdio.h>, <conio.h>, <graphics.h>, <process.h>, and <math.h>. These headers provide functions for input/output, graphics handling, and math calculations. The main() function initializes the graphics mode and sets up the screen for drawing.

Triangle Function

The DrawTriangle () function takes six integer arguments representing the coordinates of the three points of the triangle. It uses the line() function from the graphics.h library to draw the triangle on the screen.

Rotation Function

The RotateTriangle() function performs the rotation of the triangle. It takes six integer arguments representing the coordinates of the three points of the original triangle. The user is prompted to enter an angle for rotation. The function then calculates the new coordinates (a1, b1), (a2, b2), and (a3, b3) for the rotated triangle using the rotation formulas mentioned earlier. The line() function is used again to draw the rotated triangle on the screen.

Program Execution

In the main() function, the user is prompted to input the coordinates of the triangle's three points. The TriAngle() function is called to draw the initial triangle on the screen. The user is then prompted to enter the rotation angle. After the Rotate() function is called, the rotated triangle is drawn on the screen.

🎯 Summary

In this blog post, we have explored a C program that demonstrates how to rotate a 2D triangle using rotation transformation techniques. The program utilizes trigonometric formulas for rotation and the graphics.h library for graphical output. The user can input the coordinates of the triangle's three points and specify the angle of rotation. The program then displays the original and rotated triangles on the screen.

🎯 Key Points