Last CG
Last CG
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:
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
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";
}
getch();
closegraph();
return 0;}
22
ROTATION:
SCALING:
TRANSLATION
23
Naman Arora UE213060
float x = points[i];
float y = points[i + 1];
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);
}
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:
Code Computation:
The computeCode function calculates the binary code for a given point (x, y) based on its
position relative to the clipping window.
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 xMin = 100, yMin = 100, xMax = 400, yMax = 300;
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;
29
Naman Arora UE213060
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);
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:
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
x += tx;
y += ty;
z += tz;
x += pivotX;
y += pivotY;
z += pivotZ;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
33
Naman Arora UE213060
// 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:
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
// 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:
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
35
Naman Arora UE213060
getch();
closegraph();
return 0;
}
TRANSLATION OUTPUT:
36
Naman Arora UE213060
SCALING OUTPUT:
ROTATION OUTPUT:
37