Experiment No 6
Experiment No 6
:6
Name of the Experiment: Implementation of 3D geometric transformations: Translation,
Scaling and rotation.
Aim: The aim of this experiment is to explore and demonstrate the implementation of key 3D
geometric transformations—translation, scaling, and rotation—focusing on how these
operations affect the positioning, size, and orientation of 3D objects in a digital space.
/*
3D Translation
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,10,1);
printf("Enter translation factor");
scanf("%d%d",&x,&y);
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,3);
getch();
// closegraph();
}
Output
Algorithm for 3D Rotation
Initialization:
Detect and initialize the graphics mode using initgraph().
Get the maximum x (maxx) and y (maxy) screen coordinates.
Compute the center of the screen (midx, midy), which will be used as the origin for
rotation.
Drawing the Axes:
Call the axis() function to draw the X and Y axes on the screen:
o A vertical line at the center (midx) representing the Y-axis.
o A horizontal line at the center (midy) representing the X-axis.
Draw 3D Object (Bar):
A 3D bar is drawn using the bar3d() function at specific coordinates relative to the
center.
Get Rotation Angle:
Ask the user for the rotation angle (in degrees) around the Z-axis.
Rotation about the Z-axis:
Apply the 2D rotation matrix for the Z-axis to rotate the bar around the Z-axis.
o For a point (x, y), the new coordinates after rotation are:
x' = x * cos(θ) - y * sin(θ)
y' = x * sin(θ) + y * cos(θ)
o Here, the angle θ is converted to radians by multiplying the degree value by π /
180.
Display Results for Z-axis Rotation:
Call axis() to redraw the axes.
Draw the rotated bar using bar3d() at the new coordinates calculated in step 5.
Rotation about the X-axis:
The program rotates the 3D object around the X-axis, but the corresponding
transformations for 3D rotations are implied by changing the vertical displacement of
the object (Y-coordinates).
Display Results for X-axis Rotation:
Call axis() to redraw the axes.
Draw the rotated bar using bar3d() at the new coordinates after rotating about the X-
axis.
Rotation about the Y-axis:
Similar to the X-axis rotation, the program rotates the object about the Y-axis by
modifying the horizontal displacement (X-coordinates).
Display Results for Y-axis Rotation:
Call axis() to redraw the axes.
Draw the rotated bar using bar3d() at the new coordinates after rotating about the Y-
axis.
End:
Wait for the user input (getch()) before closing the program.
.