3D Rotation Program Using C Programming
🎯 Introduction
In computer graphics, 3D transformations play a crucial role in manipulating objects in a three-dimensional space. One of the fundamental transformations is rotation, which enables us to rotate an object around a specific point. This blog post provides a C programming implementation of a 3D rotation program using graphics.h library. We'll walk through the concept of 3D rotation, explain the code in detail, and present a more modernized and explanatory version of the program.
🎯 Concept of 3D Rotation
3D rotation is a geometric transformation that rotates an object in three-dimensional space around a specified axis or point. The rotation angle determines the amount and direction of rotation. To perform a 3D rotation, we utilize trigonometric functions to calculate the new coordinates of the object's vertices after the rotation.
🎯 3D Rotation Example Program
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
int x1, x2, y1, y2, mx, my, depth;
void draw();
void rotate();
void main()
{
int gd = DETECT, gm, c;
initgraph(&gd, &gm, "d:\\tc\\bgi");
printf("\n3D Transformation Rotating\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();
rotate();
getch();
}
void draw()
{
bar3d(x1, y1, x2, y2, depth, 1);
}
void rotate()
{
float t;
int a1, b1, a2, b2, dep;
printf("Enter the angle to rotate=");
scanf("%f", &t);
t = t * (3.14 / 180);
a1 = mx + (x1 - mx) * cos(t) - (y1 - my) * sin(t);
a2 = mx + (x2 - mx) * cos(t) - (y2 - my) * sin(t);
b1 = my + (x1 - mx) * sin(t) + (y1 - my) * cos(t);
b2 = my + (x2 - mx) * sin(t) + (y2 - my) * cos(t);
if (a2 > a1)
dep = (a2 - a1) / 4;
else
dep = (a1 - a2) / 4;
bar3d(a1, b1, a2, b2, dep, 1);
setcolor(5);
}
🎯 3D Rotation Example Program Explanation
We begin by including necessary C libraries for input/output, graphics, and mathematical functions.
The draw() function is responsible for drawing a 3D bar (rectangle) using the bar3d() function from the graphics.h library.
In the rotate() function, the user is prompted to enter the angle of rotation in degrees. We convert this angle to radians since trigonometric functions in C use radians.
The coordinates (mx, my) represent the midpoint of the rectangle, which acts as the center of rotation.
The coordinates (a1, b1) and (a2, b2) represent the new coordinates of the top-left and bottom-right corners of the rotated rectangle, respectively. We calculate these new coordinates using the 2D rotation transformation formulas.
The depth (dep) of the rotated rectangle is calculated based on the difference between the new x-coordinates (a1 and a2).
Finally, the rotated rectangle is drawn on the screen using the updated coordinates.
🎯 Summary
In this blog post, we've explored a C programming implementation of a 3D rotation program using graphics.h library. We discussed the concept of 3D rotation and how to calculate the new coordinates after rotation. The program allows users to input the initial position of the 3D bar and the rotation angle. It then rotates and displays the transformed 3D bar on the screen. By understanding the concepts and code explained above, you can further explore and implement other 3D transformations and graphics applications using C programming.
🎯 Key Points
3D rotation is a geometric transformation that rotates an object in three-dimensional space around a specified axis or point.
Trigonometric functions (e.g., cos() and sin()) are used to calculate the new coordinates after rotation.
The midpoint of the object serves as the center of rotation.
The program uses the graphics.h library for graphical representation.
Users can input the initial position and the angle of rotation for the 3D bar.
Computer Graphics Programs
2D Computer Graphics Programs
3D Computer Graphics Programs