3D Translation Program Using C Programming
On this page (5sections)
3D Translation Program Using C Programming
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();
}
void draw()
{
bar3d(x1, y1, x2, y2, depth, 1);
}
void trans()
{
int tx, ty, a1, a2, b1, b2, dep;
printf("\\n\\nEnter translation distances along x and y axes: ");
scanf("%d%d", &tx, &ty);
a1 = x1 + tx;
a2 = x2 + tx;
b1 = y1 + ty;
b2 = y2 + ty;
dep = (a2 - a1) / 4;
setcolor(RED);
draw();
setcolor(WHITE);
bar3d(a1, b1, a2, b2, dep, 1);
}
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
-
3D Translation is a transformation that moves an object in 3D space without altering its shape or orientation.
-
The graphics.h library provides basic graphics functions in C to create simple graphical applications.
-
The bar3d() function is used to draw a 3D rectangle.
-
Global variables are used to store coordinates and dimensions for easy access across functions.
-
The draw() function is responsible for drawing the 3D rectangle, and the trans() function handles the translation.
Related Tutorials
2D Scaling Program in C: Understanding Graphics Transformation...
In computer graphics, scaling is a fundamental transformation that alters the size of an object while preserving its shape. It is commonly used to zoom in or ou
Read tutorial2D Rotation Program Using C Programming
In computer graphics, transformations play a crucial role in manipulating objects on the screen. One such fundamental transformation is rotation, which involves
Read tutorial2D Translation Rectangle Program Using C Programming
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 pro
Read tutorial