Practical – 08
2D Transformations
Objective:
Write a program to demonstrate 2D transformations that is the basic transformations of translation, rotations
and scaling .
Input:
Enter the number of points i.e the number of vertices your figure contains.
Enter the coordinates of the vertices of your figure.
Right click the screen to open the menu.
Choose what type of action you want to perform from the various options in the menu
given.
Algorithm Description:
2D Transformation in computer graphics is a process of modifying and re-positioning the existing
graphics in 2 dimensions. Transformations help change the object's position, size,orientation, shape, etc.
Translation:
Translation refers to a technique in which a point is shift from one place to another, whose distance is known.
Consider a point A(x1, y1) be shifted to another point B (x2, y2). Now weonly know the shifting distance tx
along x-axis and ty along y-axis.
Now, the new coordinates (x2, y2) can be calculated as:
x2 = x1 + tx
y2 = y1 + ty
Scaling:
Scaling refers to a mathematical rule applied to change the size of the image. If the value ofscaling factors is
negative, the size is decreased and when the value is positive, the size of the image is increased. Suppose the
point with coordinates A (x1, y1) is to be scaled by a factor sx along the x-axis and sy along the y-axis.
Hence the new coordinates after scaling will be:
x2 = x1 * sx
y2 = y1 * sy
Rotation:
Rotation refers to mathematical operation in which the graphical object is rotated about anangle (θ) to the
axis. Rotation is of two types: anti-clockwise(considered positive) and clockwise rotation(considered
negative).
20
Suppose we want to rotate a point with coordinates A (x1, y1) clockwise through an angle θ about the
origin.
Then the new coordinates
A’ (x2, y2):
x2 = x1 cosθ + y1 sinθ
y2 = x1 sinθ - y1 cosθ
Code:
void drawPolygon(int points[], int n) {
for (int i = 0; i < n - 1; ++i)
line(points[i * 2], points[i * 2 + 1], points[(i + 1) * 2], points[(i + 1) * 2 + 1]);
line(points[(n - 1) * 2], points[(n - 1) * 2 + 1], points[0], points[1]);
}
void rotate(int points[], int n, float angle) {
float radian = angle * (M_PI / 180.0);
for (int i = 0; i < n * 2; i += 2) {
float x = points[i];
float y = points[i + 1];
points[i] = round(x * cos(radian) - y * sin(radian));
points[i + 1] = round(x * sin(radian) + y * cos(radian));
}
}
void scale(int points[], int n, float sx, float sy) {
for (int i = 0; i < n * 2; i += 2) {
points[i] = round(points[i] * sx);
points[i + 1] = round(points[i + 1] * sy);
}
}
void translate(int points[], int n, int tx, int ty) {
for (int i = 0; i < n * 2; i += 2) {
points[i] += tx;
points[i + 1] += ty;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
// Number of vertices of the polygon
int n = 4;
// Initial coordinates of the polygon
int points[] = {100, 100, 200, 100, 200, 200, 100, 200};
// Display original polygon
drawPolygon(points, n);
21
// User input for transformation type
cout << "Choose Transformation Type:\n";
cout << "1. Rotation\n";
cout << "2. Scaling\n";
cout << "3. Translation\n";
int choice;
cout << "Enter your choice (1/2/3): ";
cin >> choice;
switch (choice) {
case 1: {
// Rotation angle in degrees
float angle;
cout << "Enter rotation angle in degrees: ";
cin >> angle;
// Perform rotation
rotate(points, n, angle);
break;
}
case 2: {
// Scaling factors
float sx, sy;
cout << "Enter scaling factor for x-axis: ";
cin >> sx;
cout << "Enter scaling factor for y-axis: ";
cin >> sy;
// Perform scaling
scale(points, n, sx, sy);
break;
}
case 3: {
// Translation factors
int tx, ty;
cout << "Enter translation along x-axis: ";
cin >> tx;
cout << "Enter translation along y-axis: ";
cin >> ty;
// Perform translation
translate(points, n, tx, ty);
break;
}
default:
cout << "Invalid choice\n";
}
// Display transformed polygon
setcolor(WHITE);
drawPolygon(points, n);
getch();
closegraph();
return 0;}
22
ROTATION:
SCALING:
TRANSLATION
23
Naman Arora UE213060
// Function to perform 2D translation about an arbitrary point
void translateAboutPoint(int points[], int n, int pivotX, int pivotY, int tx, int ty) {
for (int i = 0; i < n * 2; i += 2) {
points[i] = points[i] - pivotX;
points[i + 1] = points[i + 1] - pivotY;
points[i] = points[i] + tx;
points[i + 1] = points[i + 1] + ty;
points[i] = points[i] + pivotX;
points[i + 1] = points[i + 1] + pivotY;}}
// Function to perform 2D scaling about an arbitrary point
void scaleAboutPoint(int points[], int n, int pivotX, int pivotY, float sx, float sy) {
for (int i = 0; i < n * 2; i += 2) {
points[i] = points[i] - pivotX;
points[i + 1] = points[i + 1] - pivotY;
points[i] = round(points[i] * sx);
points[i + 1] = round(points[i + 1] * sy);
points[i] = points[i] + pivotX;
points[i + 1] = points[i + 1] + pivotY;}}
// Function to perform 2D rotation about an arbitrary point
void rotateAboutPoint(int points[], int n, int pivotX, int pivotY, float angle) {
float radian = angle * (M_PI / 180.0);
for (int i = 0; i < n * 2; i += 2) {
points[i] = points[i] - pivotX;
points[i + 1] = points[i + 1] - pivotY;
float x = points[i];
float y = points[i + 1];
points[i] = round(x * cos(radian) - y * sin(radian));
points[i + 1] = round(x * sin(radian) + y * cos(radian));
points[i] = points[i] + pivotX;
points[i + 1] = points[i + 1] + pivotY;}}
24
Naman Arora UE213060
TRANSLATION
SCALING:
ROTATION:
25
Naman Arora UE213060
Practical – 09
Point Clipping
Objective:
The objective of this practical is to understand the concept of point clipping and
implement a simple algorithm to determine whether a point lies inside a specified
rectangular region.
Introduction:
Point clipping is a fundamental concept in computer graphics used to determine whether a
point is inside or outside a specified region. In this practical, we focus on a basic example
of rectangular region clipping.
Algorithm:
The algorithm involves checking if a given point (x, y) lies inside a rectangular region
defined by the coordinates of its left, top, right, and bottom boundaries.
bool isInsideRegion(int x, int y, int left, int top, int right, int bottom) {
return (x >= left && x <= right && y >= top && y <= bottom);
}
x, y: Coordinates of the point to be checked.
left, top: Coordinates of the top-left corner of the rectangular region.
right, bottom: Coordinates of the bottom-right corner of the rectangular region.
Application:
The algorithm is useful in various computer graphics applications, particularly in
situations where clipping is required to ensure that objects or points are confined within a
specified viewing area.
Implementation:
In the provided C++ code, the isInsideRegion function checks if a point is inside the
specified rectangular region.
The program displays a point and indicates whether it lies inside or outside the region.
Important Formulas:
The point (x, y) lies inside the rectangular region if and only if:
x >= left && x <= right && y >= top && y <= bottom
Conclusion:
This practical provides a basic understanding of point clipping and the implementation of
a simple algorithm to determine if a point is inside a rectangular region.
26
Naman Arora UE213060
Code:
27
Naman Arora UE213060
Practical – 10
Line Clipping
Practical: Cohen-Sutherland Line Clipping
Objective:
The objective of this practical is to implement the Cohen-Sutherland Line Clipping
algorithm to efficiently clip a line against a rectangular clipping window.
Introduction:
Cohen-Sutherland Line Clipping is a line-clipping algorithm used to efficiently determine
the portion of a line that lies inside a rectangular clipping window. This algorithm divides
the 2D plane into nine regions and uses binary codes to quickly identify the portions of
the line that are entirely outside the window.
Binary Codes:
The algorithm uses four binary codes (LEFT, RIGHT, BOTTOM, TOP) to represent the
position of a point relative to the clipping window. These codes are combined to form a
4-bit code for each point. The codes are as follows:
LEFT (0001): To the left of the window
RIGHT (0010): To the right of the window
BOTTOM (0100): Below the window
TOP (1000): Above the window
Code Computation:
The computeCode function calculates the binary code for a given point (x, y) based on its
position relative to the clipping window.
int computeCode(int x, int y) {
int code = INSIDE;
if (x < xMin) code |= LEFT;
else if (x > xMax) code |= RIGHT;
if (y < yMin) code |= BOTTOM;
else if (y > yMax) code |= TOP;
return code;
}
Cohen-Sutherland Algorithm:
The cohenSutherland function implements the Cohen-Sutherland Line Clipping algorithm.
It iteratively clips the line against the window until the entire line lies inside the window
or is determined to be outside.
Application:
Cohen-Sutherland Line Clipping is widely used in computer graphics for efficiently
rendering only the visible portions of lines, especially in interactive applications where
real-time performance is crucial.
28
Naman Arora UE213060
Conclusion:
This practical provides hands-on experience with the Cohen-Sutherland Line Clipping
algorithm, offering insights into how to efficiently determine the visible portions of a line
within a specified rectangular clipping window.
Code:
const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000
const int xMin = 100, yMin = 100, xMax = 400, yMax = 300;
int computeCode(int x, int y) {
int code = INSIDE;
if (x < xMin) // to the left of rectangle
code |= LEFT;//this is bitwise OR
else if (x > xMax) // to the right of rectangle
code |= RIGHT;
if (y < yMin) // below the rectangle
code |= BOTTOM;
else if (y > yMax) // above the rectangle
code |= TOP;
return code;
}
void cohenSutherland(int x1, int y1, int x2, int y2) {
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
bool accept = false;
while (true) {
if ((code1 == 0) && (code2 == 0)) {
accept = true;
break;
} else if (!(code1 | code2)) { // Corrected this line
accept = true;
break;
} else {
int codeOut;
int x, y;
if (code1 != 0)
codeOut = code1;
else
codeOut = code2;
if (codeOut & TOP) {
29
Naman Arora UE213060
x = x1 + (x2 - x1) * (yMax - y1) / (y2 - y1);
y = yMax;
} else if (codeOut & BOTTOM) {
x = x1 + (x2 - x1) * (yMin - y1) / (y2 - y1);
y = yMin;
} else if (codeOut & RIGHT) {
x = xMax; // Corrected this line
y = y1 + (y2 - y1) * (xMax - x1) / (x2 - x1);
} else if (codeOut & LEFT) {
x = xMin; // Corrected this line
y = y1 + (y2 - y1) * (xMin - x1) / (x2 - x1);
}
if (codeOut == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
setcolor(RED);
if (accept) {
line(x1, y1, x2, y2);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
// Draw the rectangular clipping window
rectangle(xMin, yMin, xMax, yMax);
// Input line coordinates
int x1 = 50, y1 = 50, x2 = 300, y2 = 200;
// Draw the input line
line(x1, y1, x2, y2);
// Clip the line using Cohen-Sutherland algorithm
cohenSutherland(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
30
Naman Arora UE213060
31
Naman Arora UE213060
Practical – 11
3D Transformations
Objective:
The objective of this practical is to understand and implement basic 3D transformations,
including translation, scaling, and rotation, on a 3D object.
Translation in 3D:
Objective:
Understand how to move a 3D object in space.
Method:
Implement the translate3D function for 3D translation.
Formulas:
The translation formula for a point (x, y, z) is:
x' = x + tx
y' = y + ty
z' = z + tz
where (tx, ty, tz) is the translation vector.
Scaling in 3D:
Objective:
Explore the process of resizing a 3D object.
Method:
Implement the scale3D function for 3D scaling.
Formulas:
The scaling formula for a point (x, y, z) is:
x' = x * scaleX
y' = y * scaleY
z' = z * scaleZ
where (scaleX, scaleY, scaleZ) are the scaling factors.
Rotation in 3D:
Objective:
Understand how to rotate a 3D object around the Z-axis.
Method:
Implement the rotate3DZ function for 3D rotation.
Formulas:
The rotation formula for a point (x, y, z) around the Z-axis is:
x' = x * cos(angle) - y * sin(angle)
y' = x * sin(angle) + y * cos(angle)
32
Naman Arora UE213060
z' = z
where angle is the rotation angle in radians.
Implementation:
Define a 3D object, e.g., a triangle, using its vertices.
Apply translation, scaling, and rotation to the object using the respective functions.
Display the original and transformed objects.
Conclusion:
This practical provides a hands-on experience with fundamental 3D transformations,
essential for manipulating and animating 3D objects in computer graphics.
TRANSLATION
// Function to perform 3D translation about an arbitrary point
void translate3DAroundPoint(int& x, int& y, int& z, int pivotX, int pivotY, int pivotZ, int
tx, int ty, int tz) {
// Translate the point to the origin
x -= pivotX;
y -= pivotY;
z -= pivotZ;
x += tx;
y += ty;
z += tz;
x += pivotX;
y += pivotY;
z += pivotZ;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
// Define the vertices of a triangle in 3D space
int vertices[][3] = {
{100, 50, 50},
{150, 150, 50},
{50, 150, 50}
};
int n = 3; // Number of vertices in the triangle
// Drawing the original triangle
setcolor(YELLOW);
drawpoly(n, (int*)vertices);
// Arbitrary point for translation (e.g., the centroid of the triangle)
33
Naman Arora UE213060
int pivotX = (vertices[0][0] + vertices[1][0] + vertices[2][0]) / 3;
int pivotY = (vertices[0][1] + vertices[1][1] + vertices[2][1]) / 3;
int pivotZ = (vertices[0][2] + vertices[1][2] + vertices[2][2]) / 3;
int tx = 30, ty = 30, tz = 30;
// Performing 3D translation about the arbitrary point on each vertex of the triangle
for (int i = 0; i < n; ++i) {
translate3DAroundPoint(vertices[i][0], vertices[i][1], vertices[i][2], pivotX,
pivotY, pivotZ, tx, ty, tz);
}
setcolor(RED);
drawpoly(n, (int*)vertices);
getch();
closegraph();
return 0;
SCALING:
// Function to perform 3D scaling about an arbitrary point
void scaleAboutPoint(int& x, int& y, int& z, int pivotX, int pivotY, int pivotZ, float
scaleX, float scaleY, float scaleZ) {
// Translate the point to the origin
x -= pivotX;
y -= pivotY;
z -= pivotZ;
// Scale the point
x *= scaleX;
y *= scaleY;
z *= scaleZ;
// Translate the point back to its original position
x += pivotX;
y += pivotY;
z += pivotZ;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
int vertices[][3] = {
{100, 50, 50},
{150, 150, 50},
{50, 150, 50}
};
int n = 3; // Number of vertices in the triangle
setcolor(YELLOW);
drawpoly(n, (int*)vertices);
34
Naman Arora UE213060
// Arbitrary point for scaling (e.g., the centroid of the triangle)
int pivotX = (vertices[0][0] + vertices[1][0] + vertices[2][0]) / 3;
int pivotY = (vertices[0][1] + vertices[1][1] + vertices[2][1]) / 3;
int pivotZ = (vertices[0][2] + vertices[1][2] + vertices[2][2]) / 3;
// Scaling factors
float scaleX = 1.5, scaleY = 1.5, scaleZ = 1.5;
// Performing 3D scaling about the arbitrary point on each vertex of the triangle
for (int i = 0; i < n; ++i) {
scaleAboutPoint(vertices[i][0], vertices[i][1], vertices[i][2], pivotX, pivotY,
pivotZ, scaleX, scaleY, scaleZ);
}
setcolor(RED);
drawpoly(n, (int*)vertices);
getch();
closegraph();
return 0;
}
ROTATION:
// Function to perform 3D rotation about the Z-axis
void rotate3DZ(int& x, int& y, int& z, float angle) {
float radian = angle * M_PI / 180.0;
int newX = x * cos(radian) - y * sin(radian);
int newY = x * sin(radian) + y * cos(radian);
x = newX;
y = newY;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
// Define the vertices of a triangle in 3D space
int vertices[][3] = {
{100, 50, 50},
{150, 150, 50},
{50, 150, 50}
};
int n = 3; // Number of vertices in the triangle
// Drawing the original triangle
setcolor(YELLOW);
drawpoly(n, (int*)vertices);
// Rotation angle in degrees
35
Naman Arora UE213060
float angle = 45.0;
// Center of rotation (e.g., centroid of the triangle)
// Performing 3D rotation about the Z-axis on each vertex of the triangle
for (int i = 0; i < n; ++i) {
rotate3DZ(vertices[i][0] , vertices[i][1] , vertices[i][2], angle);
// Drawing the rotated triangle
setcolor(RED);
drawpoly(n, (int*)vertices);
getch();
closegraph();
return 0;
}
TRANSLATION OUTPUT:
36
Naman Arora UE213060
SCALING OUTPUT:
ROTATION OUTPUT:
37