0% found this document useful (0 votes)
18 views9 pages

Experiment No 6

The document outlines an experiment focused on implementing 3D geometric transformations, specifically translation, scaling, and rotation, using programming algorithms and code examples. It details the steps for each transformation, including initialization, user input, and graphical output, along with the corresponding C code for each transformation type. Additionally, it includes viva questions related to transformation concepts and their applications in fields like game development and computer graphics.

Uploaded by

totalclips780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Experiment No 6

The document outlines an experiment focused on implementing 3D geometric transformations, specifically translation, scaling, and rotation, using programming algorithms and code examples. It details the steps for each transformation, including initialization, user input, and graphical output, along with the corresponding C code for each transformation type. Additionally, it includes viva questions related to transformation concepts and their applications in fields like game development and computer graphics.

Uploaded by

totalclips780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

: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.

Objective: Implementation of 3D geometric transformations: Translation, Scaling and


rotation.

Algorithm for 3D Translation


Key operations of the program:
1. Initialization of Graphics: Sets up a graphical mode to draw objects on a screen.
2. Axis Drawing: The function axis() draws the X and Y axes centered at the middle of
the screen.
3. 3D Object Drawing: The bar3d() function is used to draw a 3D rectangular object at
a specific position.
4. Translation Input: The program asks the user for a translation factor for both the X
and Y axes.
5. Object Translation: The 3D object is moved (translated) based on the user's input.
6. Final Output: The program displays the translated object and waits for user input to
close.
Algorithm:
1. Initialize Graphics:
o Detect and initialize the graphics mode.
o Set the screen's maximum width (maxx) and height (maxy).
o Set the screen center as the origin point (midx and midy).
2. Draw Axes:
o Draw the X-axis and Y-axis crossing at the center of the screen (midx, midy).
o Wait for user input before clearing the screen to reset the graphics.
3. Draw Initial 3D Object:
o Draw a 3D bar (rectangular prism) at a fixed position relative to the center
using bar3d().
4. Prompt for Translation Values:
o Ask the user for translation factors along the X and Y axes (i.e., how much to
move the object).
o Read the user's input for translation values.
5. Translate Object:
o Apply the translation values to the coordinates of the 3D object.
o Redraw the translated object at the new position based on the translation
values.
6. Wait for User Input:
o Wait for a key press before exiting.
7. Exit:
o Optionally close the graphics window after the translation is displayed.
Algorithm Steps:
1. Start
2. Initialize graphics mode.
3. Set maxx = getmaxx(), maxy = getmaxy(), midx = maxx/2, midy = maxy/2.
4. Call axis() to draw axes.
5. Draw 3D object at initial position (midx+50, midy-100) with bar3d().
6. Print "Enter translation factor" and read user input for x and y translation factors.
7. Display the translated 3D object using the updated position: (midx + x + 50, midy - (y
+ 100)) with bar3d().
8. Wait for user input.
9. Close graphics mode and exit.

Program Code: 3D Translation

/*
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.
.

Program Code: 3D Rotation


/*
3D Rotation
*/
#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,5,1);
printf("Enter rotating angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotation about z axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
printf("After rotation about x axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("After rotation about yaxis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
//closegraph();
}
Output:

Algorithm for 3D Scaling


1. Initialize Graphics Mode
 Set up the graphics mode and get the maximum screen coordinates.
 Define the origin (midx, midy) as the center of the screen.
2. Draw the Axes
 Clear the screen.
 Draw two lines representing the X and Y axes. The center (midx, midy) acts as the
origin.
3. Draw Initial Object (Before Scaling)
 Draw a 3D bar using the bar3d function. This represents the object you will be
scaling.
4. Input Scaling Factors
 Prompt the user to enter three scaling factors for the X, Y, and Z dimensions (let's call
them sx, sy, and sz).
5. Apply 3D Scaling
 Apply the scaling transformations to the coordinates of the 3D object.
o The new X coordinate will be: new_x = sx * original_x
o The new Y coordinate will be: new_y = sy * original_y
o The new Z coordinate will be: new_z = sz * original_z
 Draw the scaled object using these new coordinates.
6. Display and Wait for User Input
 Display the scaled object.
 Wait for the user to press a key before ending the program.
7. Close the Program
 Optionally close the graphics window (commented out in the code).

Program Code: 3D Scaling


/*
3D Scaling
*/
#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,5,1);
printf("Enter scaling factors");
scanf("%d%d%d", &x,&y,&z);
printf("After scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
getch();
//closegraph();
}
Output:
Viva Questions:
 Transformation Basics:
What is a transformation in 3D graphics?
Why do we use transformations in 3D graphics?
Answer: A transformation in 3D graphics is the process of changing the position,
orientation, and size of an object in 3D space. We use transformations to manipulate
objects, such as rotating, scaling, translating, or changing their perspective in a scene.
 Types of 3D Transformations:
What are the different types of transformations used in 3D graphics?
Answer: The primary 3D transformations are:
Translation: Moving an object from one position to another along the X, Y, and Z axes.
Scaling: Changing the size of an object along one or more axes.
Rotation: Rotating an object around one of the three axes (X, Y, or Z).
 Mathematics of Transformation:
How are transformations represented mathematically in 3D graphics?
What is the transformation matrix, and how does it work?
Answer: In 3D graphics, transformations are represented using matrices. A 3D point can
be represented as a column vector, and transformation operations (translation, scaling,
rotation) are performed using matrix multiplication.
 Homogeneous Coordinates:
 Why do we use homogeneous coordinates in 3D transformations?
Answer: Homogeneous coordinates are used because they allow us to represent 3D
transformations (such as translation, scaling, and rotation) in a unified way using
matrix multiplication. By using a 4x4 matrix and adding a fourth coordinate (w), we
can represent all transformations, including translation, that would otherwise
require different types of operations in a non-homogeneous system.
 Composite Transformations:
 What is a composite transformation?
 How do we combine multiple transformations in 3D graphics?
Answer: A composite transformation is the combination of two or more
transformations applied sequentially to an object. To perform a composite
transformation, we multiply the corresponding transformation matrices together (in
the order of application) and apply the resulting matrix to the object. For example,
applying scaling followed by translation can be represented as:
M=T×S
where T is the translation matrix and S is the scaling matrix.
 Rotation Order:
 Does the order of rotations matter? Why or why not?
Answer: Yes, the order of rotations in 3D space matters. This is because rotations in
3D are not commutative, meaning that rotating an object around the X-axis and
then the Y-axis gives a different result than rotating it around the Y-axis and then
the X-axis.

Use Cases of 2D Transformations


3D transformations play a crucial role in various fields, especially in computer graphics,
game development, simulations, virtual reality, and engineering. They are used to manipulate
and position objects in a 3D space, and each type of transformation—translation, rotation,
scaling, and shearing—has distinct use cases. Below are some common use cases of 3D
transformations:
1. Game Development:
 Character Movement:
o In games, characters (or game objects) need to move in a 3D world.
Translation transformations are used to move characters or objects across the
game world (along the X, Y, or Z axis).
 Camera Movement:
o The viewpoint or camera in a game needs to be moved, rotated, or zoomed
in/out. Camera transformations (view and projection) allow developers to
simulate the movement or rotation of the camera to change the perspective of
the scene.
 Object Rotation:
o Characters, vehicles, or objects need to be rotated to face a certain direction,
especially when turning, rotating weapons, or animating actions.
 Collision Detection:
o By applying transformations, we can detect and calculate object interactions
(e.g., when two objects collide or overlap in 3D space).
 Animations:
o Movements such as rotating, translating, and scaling objects are often required
in 3D animations (e.g., rotating a model or making an object move along a
path).
o

You might also like