Name
Name
Subject: -
Roll No.: -
Submitted to: -
Pag
S.
Practical Title e
No.
No.
Write a program to implement 0
1
Bresenham’s line drawing algorithm. 1
Write a program to implement a 0
2
midpoint circle drawing algorithm. 5
Write a program to clip a line using
1
3 Cohen and Sutherland line clipping
0
algorithm.
Write a program to clip a polygon using 1
4
Sutherland Hodgemann algorithm. 5
Write a program to fill a polygon using 2
5
the Scan line fill algorithm. 0
Write a program to apply various 2D
2
6 transformations on a 2D object (use
5
homogeneous Coordinates).
Write a program to apply various 3D
transformations on a 3D object and 3
7
then apply parallel and perspective 0
projection on it.
Write a program to draw Hermite 3
8
/Bezier curve. 5
Acknowledgment
I would like to express my sincere
gratitude to my instructor, DR. Aakash,
for providing me with the opportunity to
work on this series of practicals. Their
invaluable guidance and support have
been instrumental in understanding the
core concepts of computer graphics.
I would also like to thank Ramanujan
College, University of Delhi, for offering
such a rich curriculum that fosters
technical learning and hands-on
experience. The practical applications of
various algorithms and transformation
techniques have deepened my
knowledge in the field.
Furthermore, I would like to thank my
peers for their encouragement and
collaboration during the course of this
project. Their inputs have enhanced my
learning experience and contributed to a
stimulating environment for problem-
solving.
Q1. Write a program to implement
Bresenham’s line drawing
algorithm.
Code:
#include <iostream>
#include <graphics.h>
int dx = x2 - x1;
int dy = y2 - y1;
int p = 2 * dy - dx;
putpixel(x, y, WHITE);
if (p >= 0) {
y++;
p += 2 * dy - 2 * dx;
} else {
p += 2 * dy;
x++;
int main() {
int gd = DETECT, gm;
getch();
closegraph();
return 0;
Output:
#include <graphics.h>
using namespace std;
int x = 0, y = r;
int p = 1 - r;
while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (p < 0) {
p += 2 * x + 3;
} else {
p += 2 * (x - y) + 5;
y--;
x++;
int main() {
closegraph();
return 0;
Output:
#include <graphics.h>
return code;
while (true) {
accept = true;
break;
break;
} else {
int x, y;
y = ymax;
x = xmax;
} else {
x = xmin;
if (outcode == code1) {
x1 = x;
y1 = y;
} else {
x2 = x;
y2 = y;
if (accept) {
setcolor(WHITE);
setcolor(GREEN);
int main() {
getch();
closegraph();
return 0;
Output:
#include <iostream>
#include <vector>
struct Point {
int x, y;
};
switch (edge) {
return false;
// Function to find the intersection point between polygon edge and window boundary
Point i;
switch (edge) {
break;
i.x = x_max;
break;
i.y = y_min;
break;
i.y = y_max;
break;
return i;
vector<Point> clippedPolygon;
int n = polygon.size();
if (inside(p, edge)) {
if (inside(s, edge)) {
clippedPolygon.push_back(p);
} else {
// Previous point is outside, current point is inside, add intersection and current
point
clippedPolygon.push_back(intersection(s, p, edge));
clippedPolygon.push_back(p);
} else {
if (inside(s, edge)) {
clippedPolygon.push_back(intersection(s, p, edge));
return clippedPolygon;
int n = polygon.size();
setcolor(color);
int main() {
drawPolygon(polygon, RED);
getch();
cleardevice();
drawPolygon(polygon, GREEN);
getch();
closegraph();
return 0;
Output:
5. Write a program to fill a polygon
using the Scan line fill algorithm.
Code:
#include <iostream>
#include <graphics.h>
struct Point {
int x, y;
};
int xIntersections[20], k = 0;
if (y1 != y2) {
xIntersections[k++] = x;
int main() {
Point polygon[] = {{200, 100}, {300, 200}, {250, 300}, {150, 300}, {100, 200}};
scanLine(polygon, n);
getch();
closegraph();
return 0;
Output:
Q6. Write a program to apply various 2D
transformations on a 2D object (use
homogeneous Coordinates).
Code:
#include <iostream>
#include <graphics.h>
#include <cmath>
x = xNew;
y = yNew;
float translationMatrix[3][3] = {
{1, 0, static_cast<float>(tx)},
{0, 1, static_cast<float>(ty)},
{0, 0, 1}
};
drawTransformedObject(translationMatrix);
float scaleMatrix[3][3] = {
{sx, 0, 0},
{0, 0, 1}
};
drawTransformedObject(scaleMatrix);
float rotationMatrix[3][3] = {
{0, 0, 1}
};
drawTransformedObject(rotationMatrix);
int main() {
// Original object
setcolor(WHITE);
// Apply transformations
setcolor(YELLOW);
translate(50, 50);
setcolor(RED);
scale(0.5, 0.5);
setcolor(GREEN);
rotate(45);
getch();
closegraph();
return 0;
Output:
Q7.Write a program to apply
various 3D transformations on a 3D
object and then apply parallel and
perspective projection on it.
Code: parallel projection
#include <iostream>
#include <graphics.h>
#include <cmath>
x = xNew;
y = yNew;
z = zNew;
x2D = x;
y2D = y;
// Cube vertices
// Apply transformation
setcolor(WHITE);
// 3D Translation
float translationMatrix[4][4] = {
{1, 0, 0, tx},
{0, 1, 0, ty},
{0, 0, 1, tz},
{0, 0, 0, 1}
};
drawTransformed3DObject(translationMatrix);
// 3D Scaling
float scaleMatrix[4][4] = {
{sx, 0, 0, 0},
{0, 0, 0, 1}
};
drawTransformed3DObject(scaleMatrix);
float rotationMatrix[4][4] = {
{0, 0, 1, 0},
{0, 0, 0, 1}
};
drawTransformed3DObject(rotationMatrix);
int main() {
setcolor(YELLOW);
setcolor(RED);
setcolor(GREEN);
rotate3D(45);
getch();
closegraph();
return 0;
}Output:
Code: perspective projection
#include <iostream>
#include <graphics.h>
#include <cmath>
using namespace std;
x = xNew;
y = yNew;
z = zNew;
// Cube vertices
// Apply transformation
setcolor(WHITE);
// 3D Translation
float translationMatrix[4][4] = {
{1, 0, 0, static_cast<float>(tx)},
{0, 1, 0, static_cast<float>(ty)},
{0, 0, 1, static_cast<float>(tz)},
{0, 0, 0, 1}
};
drawTransformed3DObject(translationMatrix, d);
// 3D Scaling
float scaleMatrix[4][4] = {
{sx, 0, 0, 0},
{0, 0, 0, 1}
};
drawTransformed3DObject(scaleMatrix, d);
float rotationMatrix[4][4] = {
{0, 0, 0, 1}
};
drawTransformed3DObject(rotationMatrix, d);
int main() {
int d = 500;
setcolor(YELLOW);
setcolor(RED);
setcolor(GREEN);
rotate3D(45, d);
getch();
closegraph();
return 0;
}
Output:
Q8. 8. Write a program to draw
Hermite /Bezier curve.
Code:
#include <graphics.h>
#include <iostream>
void drawHermiteCurve(int x0, int y0, int x1, int y1, int rx0, int ry0, int rx1, int ry1) {
int x, y;
putpixel(x, y, WHITE);
int main() {
getch();
closegraph();
return 0;
}
Output: