2D Translation Rectangle Program Using C Programming

🎯 Introduction

In computer graphics, translating an object means moving it from one position to another within the coordinate space. In this blog post, we will explore a C programming implementation of a 2D rectangle translation program using the graphics.h library. The program allows users to draw a rectangle by specifying its starting point, height, and width. Additionally, it demonstrates how to translate the rectangle to a new position based on user-defined translation coordinates. We will provide a detailed explanation of the concepts involved, step-by-step implementation, and the key points to understand.

🎯 Concept

The concept of 2D translation involves changing the position of an object in a 2-dimensional plane without altering its size, shape, or orientation. In the rectangular context, translation shifts the rectangle to a new location specified by the user through translation coordinates.

🎯 2D Translation Rectangle Example Program

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<process.h>

#include<math.h>


void RectAngle(int x, int y, int Height, int Width);

void Translate(int x, int y, int Height, int Width);


void main()

{

    int gd = DETECT, gm;

    int x, y, Height, Width;

    

    // Initialize the graphics system

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


    printf("Enter the First point for the Rectangle:");

    scanf("%d%d", &x, &y);


    printf("Enter the Height&Width for the Rectangle:");

    scanf("%d%d", &Height, &Width);


    // Draw the original rectangle

    RectAngle(x, y, Height, Width);

    

    getch();

    cleardevice();

    

    // Translate the rectangle

    Translate(x, y, Height, Width);


    // Draw the translated rectangle

    RectAngle(x, y, Height, Width);


    getch();

}


void RectAngle(int x, int y, int Height, int Width)

{

    // Draw the four sides of the rectangle

    line(x, y, x + Width, y);              // Top side

    line(x, y, x, y + Height);             // Left side

    line(x + Width, y, x + Width, y + Height); // Right side

    line(x, y + Height, x + Width, y + Height); // Bottom side

}


void Translate(int x, int y, int Height, int Width)

{

    int Newx, Newy, a, b;


    printf("Enter the Translation coordinates:");

    scanf("%d%d", &Newx, &Newy);


    cleardevice();


    // Calculate the new position of the rectangle after translation

    a = x + Newx;

    b = y + Newy;


    // Draw the translated rectangle

    RectAngle(a, b, Height, Width);

}


🎯 2D Translation Rectangle Program Explanation

Header Files: 

The necessary header files are included in the program. The graphics.h library provides functions for drawing basic shapes and is widely used in C programming for simple graphics applications.

RectAngle Function: 

This function is responsible for drawing the rectangle on the screen. It takes four arguments - the initial position (x, y) and the height and width of the rectangle. The function uses the line() function from the graphics.h library to draw lines between the specified points, forming a rectangle.

Translate Function: 

The Translate function handles the translation of the rectangle. It takes the current position (x, y) of the rectangle, as well as the height and width, as input. The user is prompted to enter the translation coordinates (Newx and Newy). The function then clears the screen using cleardevice() and calculates the new position (a, b) of the rectangle after translation by adding the translation coordinates to the initial position. Finally, it calls the RectAngle function to draw the translated rectangle.

Main Function: 

The main function initializes the graphics system using initgraph() and prompts the user to enter the initial position (x, y) of the rectangle, as well as its height and width. It then calls the RectAngle function to draw the original rectangle. After the user presses any key, the screen is cleared, and the Translate function is called, allowing the user to enter translation coordinates. The translated rectangle is then drawn using the RectAngle function.

🎯 Summary

In this blog post, we explored a C programming implementation of a 2D rectangle translation program using the graphics.h library. The program allows users to draw a rectangle at a specified position and subsequently translate it to a new location by entering translation coordinates. Understanding 2D translation is essential in computer graphics, as it forms the basis for various transformations and animations.

Key Points