3D Translation Program Using C Programming

🎯 Introduction

This blog post introduces a simple 3D Translation program using C programming language. The program allows users to create and translate a 3D rectangle in a graphical window. The primary purpose of this program is to demonstrate the concepts of 3D graphics and basic translation transformations. Before we dive into the detailed explanation of the code, let's first understand the concept of 3D translation and the necessary background.

🎯 Concept

In computer graphics, 3D translation is a transformation that moves an object along the x, y, and z-axis in a 3-dimensional space. Translation is a fundamental geometric transformation used to change the position of an object without altering its shape or orientation. In this C program, we use the graphics.h library, which provides basic graphics functions to create simple graphical applications.

🎯 3D Translation Example Program


#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <process.h>

#include <graphics.h>


int x1, x2, y1, y2, mx, my, depth;


void draw();

void trans();


void main()

{

    int gd = DETECT, gm, c;

    initgraph(&gd, &gm, "d:\\tc\\bgi");


    printf("\n\t\t3D Translation\n\n");

    printf("Enter 1st top value (x1, y1): ");

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


    printf("Enter right bottom value (x2, y2): ");

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


    depth = (x2 - x1) / 4;

    mx = (x1 + x2) / 2;

    my = (y1 + y2) / 2;


    draw();

    getch();

    cleardevice();

    trans();

    getch();

}

🎯 3D Translation Example Program Detailed Explanation

Header Files: 

The program includes necessary header files, such as <stdio.h>, <conio.h>, <math.h>, <process.h>, and <graphics.h>. These files provide input/output, mathematical, and graphical functionalities.

Global Variables: 

The program declares global variables x1, x2, y1, y2, mx, my, and depth. These variables store the coordinates and dimensions of the 3D rectangle and the midpoint and depth required for the transformation.

draw() Function: 

The draw() function is responsible for drawing the original 3D rectangle. It uses the bar3d() function from the graphics.h library, taking x1, y1, x2, y2, and depth as arguments to draw the rectangle.

trans() Function: 

The trans() function handles the translation of the 3D rectangle. It takes user input for the translation distances along the x and y-axis. The new coordinates a1, a2, b1, and b2 are calculated by adding the user-provided values to the initial coordinates. The depth is recalculated based on the new width (a2 - a1) and is used to draw the translated rectangle.

main() Function: 

The main() function is the entry point of the program. It initializes the graphics mode using initgraph() from the graphics.h library. The user is prompted to enter the coordinates of the initial rectangle, and the midpoint and depth are calculated accordingly. The draw() function is then called to display the original 3D rectangle. After that, the screen is cleared, and the trans() function is called to display the translated 3D rectangle.

🎯 Summary

This C program demonstrates a basic 3D Translation using the Turbo C graphics library. It allows users to draw and translate a 3D rectangle in a graphical window. The draw() function is responsible for drawing the initial rectangle, while the trans() function handles the translation. The program illustrates the fundamental concepts of 3D graphics and geometric transformations, providing a stepping stone for more advanced graphics programming.

🎯 Key Points