0% found this document useful (0 votes)
8 views28 pages

C.G Practical

The document is a practical record for a Computer Graphics course by Rajkishore Behera, detailing various programming experiments related to graphics algorithms. It includes implementations of Bresenham's line drawing, mid-point circle drawing, Cohen-Sutherland line clipping, Sutherland-Hodgeman polygon clipping, and Scan line fill algorithms. Each experiment contains source code, input/output examples, and a declaration of originality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views28 pages

C.G Practical

The document is a practical record for a Computer Graphics course by Rajkishore Behera, detailing various programming experiments related to graphics algorithms. It includes implementations of Bresenham's line drawing, mid-point circle drawing, Cohen-Sutherland line clipping, Sutherland-Hodgeman polygon clipping, and Scan line fill algorithms. Each experiment contains source code, input/output examples, and a declaration of originality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical record

On
Computer Graphics

Name:- Rajkishore Behera


College Roll No: - BCA-23-032
Exam Roll No :- NAC23BCA-031
Class:- BCA 3rd yr, Semester - VI

Nimapara Autonomous College


Nimapada, Puri, 752106
DECLARATION

I do hereby declare that, the assignment submitted by me for the partial


fulfilment of 6st Semester Examinations – 2026 is an original piece of work done
by me. It has not been submitted previously to any other institution for award of
any degree or diploma.

Date: Signature
INDEX

Experiment Date of Date of Page Teacher’s


Sl No. Experiment Remark
No. Experiment Submission No.

Write a program to
01 1 1
implement Bresenham’s
line drawing algorithm.

Write a program to
implement mid-point
02 2 3
circle drawing algorithm.

Write a program to clip a


03 3 line using Cohen and 6
Sutherland line clipping
algorithm.

Write a program to clip a


04 4 9
polygon using Sutherland
Hodgeman algorithm.

Write a program to fill a


05 5 polygon using Scan line 13
fill algorithm.

Write a program to apply


06 6 various 2D 16
transformations on a 2D
object (use homogenous
coordinates).

07 7 Write a program to apply 21


various 3D
transformations on a 3D
object and then apply
parallel and perspective
projection on it.
Q.1 Write a program to implement Bresenham’s line drawing
algorithm.
Source Code :
#include <stdio.h>

#include <stdlib.h>

void bresenhamLine(int x1, int y1, int x2, int y2) {

int dx = abs(x2 - x1);

int dy = abs(y2 - y1);

int sx = (x1 < x2) ? 1 : -1;

int sy = (y1 < y2) ? 1 : -1;

int err = dx - dy;

while (1) {

printf("(%d, %d)\n", x1, y1);

if (x1 == x2 && y1 == y2)

break;

int e2 = 2 * err;

if (e2 > -dy) {

err -= dy;

x1 += sx;

if (e2 < dx) {

err += dx;

y1 += sy;

int main() {

int x1, y1, x2, y2;

1
printf("Enter starting point (x1 y1): ");

scanf("%d %d", &x1, &y1);

printf("Enter ending point (x2 y2): ");

scanf("%d %d", &x2, &y2);

printf("Points on the line:\n");

bresenhamLine(x1, y1, x2, y2);

return 0;

OUTPUT :

Enter starting point (x1 y1): 4 8

Enter ending point (x2 y2): 7 2

Points on the line:

(4, 8)

(4, 7)

(5, 6)

(5, 5)

(6, 4)

(6, 3)

(7, 2)

=== Code Execution Successful ===

2
Q.2 Write a program to implement mid-point circle drawing algorithm.

Source Code :
#include <stdio.h>

#include <stdlib.h>

#define SIZE 40

char screen[SIZE][SIZE];

void initScreen() {

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

screen[i][j] = '.';

void plot(int x, int y) {

if (x >= 0 && x < SIZE && y >= 0 && y < SIZE) {

screen[y][x] = '*';

void drawCirclePoints(int xc, int yc, int x, int y) {

plot(xc + x, yc + y);

plot(xc - x, yc + y);

plot(xc + x, yc - y);

plot(xc - x, yc - y);

plot(xc + y, yc + x);

plot(xc - y, yc + x);

plot(xc + y, yc - x);

plot(xc - y, yc - x);
3
}

void midpointCircle(int xc, int yc, int r) {

int x = 0;

int y = r;

int p = 1 - r;

while (x <= y) {

drawCirclePoints(xc, yc, x, y);

x++;

if (p < 0) {

p = p + 2 * x + 1;

} else {

y--;

p = p + 2 * (x - y) + 1;

void display() {

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

printf("%c ", screen[i][j]);

printf("\n");

int main() {

int xc, yc, r;

4
printf("Enter center (xc yc): ");

scanf("%d %d", &xc, &yc);

printf("Enter radius: ");

scanf("%d", &r);

initScreen();

midpointCircle(xc, yc, r);

display();

return 0;

OUTPUT :
Enter center (xc yc): 20 20

Enter radius: 8

5
Q.3 Write a program to clip a line using Cohen and Sutherland line
clipping algorithm.
Source Code :
#include <stdio.h>

#define INSIDE 0

#define LEFT 1

#define RIGHT 2

#define BOTTOM 4

#define TOP 8

float xmin, ymin, xmax, ymax;

int computeCode(float x, float 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;

void cohenSutherlandClip(float x1, float y1, float x2, float y2) {

int code1 = computeCode(x1, y1);

int code2 = computeCode(x2, y2);

int accept = 0;

while (1) {

if ((code1 == 0) && (code2 == 0)) {

6
accept = 1;

break;

else if (code1 & code2) {

break;

else {

float x, y;

int code_out = code1 ? code1 : code2;

if (code_out & TOP) {

x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);

y = ymax;

else if (code_out & BOTTOM) {

x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);

y = ymin;

else if (code_out & RIGHT) {

y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);

x = xmax;

else if (code_out & LEFT) {

y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);

x = xmin;

if (code_out == code1) {

x1 = x;

y1 = y;

7
code1 = computeCode(x1, y1);

} else {

x2 = x;

y2 = y;

code2 = computeCode(x2, y2);

if (accept) {

printf("Line accepted from (%.2f, %.2f) to (%.2f, %.2f)\n", x1, y1, x2, y2);

} else {

printf("Line rejected\n");

int main() {

float x1, y1, x2, y2;

printf("Enter clipping window (xmin ymin xmax ymax): ");

scanf("%f %f %f %f", &xmin, &ymin, &xmax, &ymax);

printf("Enter line endpoints (x1 y1 x2 y2): ");

scanf("%f %f %f %f", &x1, &y1, &x2, &y2);

cohenSutherlandClip(x1, y1, x2, y2);

return 0;

OUTPUT :
Enter clipping window (xmin ymin xmax ymax): 10 10 100 100

Enter line endpoints (x1 y1 x2 y2): 12 13.6 42.5 65.3

Line accepted from (12.00, 13.60) to (42.50, 65.30)

=== Code Execution Successful ===

8
Q.4 Write a program to clip a polygon using Sutherland Hodgeman algorithm.

Source Code :
#include <stdio.h>

#define MAX 100

typedef struct {

float x, y;

} Point;

Point input[MAX], output[MAX];

int input_size;

float xmin, ymin, xmax, ymax;

int inside(Point p, int edge) {

switch(edge) {

case 0: return (p.x >= xmin); // Left

case 1: return (p.x <= xmax); // Right

case 2: return (p.y >= ymin); // Bottom

case 3: return (p.y <= ymax); // Top

return 0;

Point intersect(Point p1, Point p2, int edge) {

Point p;

float m;

if (p2.x != p1.x)

m = (p2.y - p1.y) / (p2.x - p1.x);

switch(edge) {

case 0:

p.x = xmin;

p.y = p1.y + (xmin - p1.x) * m;

break;

9
case 1:

p.x = xmax;

p.y = p1.y + (xmax - p1.x) * m;

break;

case 2:

p.y = ymin;

if (p2.x != p1.x)

p.x = p1.x + (ymin - p1.y) / m;

else

p.x = p1.x;

break;

case 3:

p.y = ymax;

if (p2.x != p1.x)

p.x = p1.x + (ymax - p1.y) / m;

else

p.x = p1.x;

break;

return p;

void clip(int edge) {

Point temp[MAX];

int k = 0;

for (int i = 0; i < input_size; i++) {

Point curr = input[i];

Point prev = input[(i + input_size - 1) % input_size];

int curr_in = inside(curr, edge);

int prev_in = inside(prev, edge);

10
if (curr_in) {

if (!prev_in) {

temp[k++] = intersect(prev, curr, edge);

temp[k++] = curr;

} else if (prev_in) {

temp[k++] = intersect(prev, curr, edge);

for (int i = 0; i < k; i++)

input[i] = temp[i];

input_size = k;

int main() {

printf("Enter clipping window (xmin ymin xmax ymax): ");

scanf("%f %f %f %f", &xmin, &ymin, &xmax, &ymax);

printf("Enter number of polygon vertices: ");

scanf("%d", &input_size);

printf("Enter polygon vertices (x y):\n");

for (int i = 0; i < input_size; i++) {

scanf("%f %f", &input[i].x, &input[i].y);

for (int edge = 0; edge < 4; edge++) {

clip(edge);

if (input_size > 0) {

printf("Clipped polygon vertices:\n");

for (int i = 0; i < input_size; i++) {

printf("(%.2f, %.2f)\n", input[i].x, input[i].y);

11
}

} else {

printf("Polygon completely outside\n");

return 0;

OUTPUT :

Enter clipping window (xmin ymin xmax ymax): 20 35 75 85

Enter number of polygon vertices: 5

Enter polygon vertices (x y):

21 45

12 23

25 43

24 34

19 34

Clipped polygon vertices:

(20.00, 35.00)

(20.00, 39.50)

(21.00, 45.00)

(20.00, 42.56)

(20.00, 35.31)

(25.00, 43.00)

(24.11, 35.00)

=== Code Execution Successful ===

12
Q.5 Write a program to fill a polygon using Scan line fill algorithm.

Source Code :
#include <stdio.h>

#define MAX 20

int main() {

int x[MAX], y[MAX];

int interx[MAX];

int n;

int ymin, ymax;

int i, j, k;

printf("Enter number of vertices: ");

scanf("%d", &n);

printf("Enter coordinates (x y):\n");

for (i = 0; i < n; i++) {

scanf("%d %d", &x[i], &y[i]);

ymin = ymax = y[0];

for (i = 1; i < n; i++) {

if (y[i] < ymin) ymin = y[i];

if (y[i] > ymax) ymax = y[i];

printf("\nScan Line Fill Output (X intersections per scan line):\n\n");

for (int scan = ymin; scan <= ymax; scan++) {

k = 0;

for (i = 0; i < n; i++) {

13
int x1 = x[i], y1 = y[i];

int x2 = x[(i + 1) % n], y2 = y[(i + 1) % n];

if (y1 == y2) continue;

if (scan >= (y1 < y2 ? y1 : y2) &&

scan < (y1 > y2 ? y1 : y2)) {

interx[k++] = x1 + (scan - y1) * (x2 - x1) / (y2 - y1);

for (i = 0; i < k - 1; i++) {

for (j = 0; j < k - i - 1; j++) {

if (interx[j] > interx[j + 1]) {

int temp = interx[j];

interx[j] = interx[j + 1];

interx[j + 1] = temp;

printf("Scan line %d: ", scan);

for (i = 0; i < k; i += 2) {

if (i + 1 < k) {

printf("Fill from %d to %d ", interx[i], interx[i + 1]);

printf("\n");

14
return 0;

OUTPUT :
Enter number of vertices: 4

Enter coordinates (x y):

12 18

13 24

21 15

17 16

Scan Line Fill Output (X intersections per scan line):

Scan line 15: Fill from 21 to 21

Scan line 16: Fill from 17 to 20

Scan line 17: Fill from 15 to 19

Scan line 18: Fill from 12 to 18

Scan line 19: Fill from 12 to 17

Scan line 20: Fill from 12 to 16

Scan line 21: Fill from 12 to 15

Scan line 22: Fill from 12 to 14

Scan line 23: Fill from 12 to 13

Scan line 24:

=== Code Execution Successful ===

15
Q.6 Write a program to apply various 2D transformations on a 2D object
(use homogenous coordinates).
Source Code :
#include <stdio.h>

#include <math.h>

#define MAX 20

typedef struct {

float x, y;

} Point;

Point p[MAX];

int n;

void display(char msg[]) {

printf("\n%s\n", msg);

for (int i = 0; i < n; i++) {

printf("(%.2f, %.2f)\n", p[i].x, p[i].y);

void translate(float tx, float ty) {

for (int i = 0; i < n; i++) {

p[i].x = p[i].x + tx;

p[i].y = p[i].y + ty;

void scale(float sx, float sy) {

for (int i = 0; i < n; i++) {

p[i].x = p[i].x * sx;

p[i].y = p[i].y * sy;

16
}

void rotate(float angle) {

float rad = angle * 3.1416 / 180;

for (int i = 0; i < n; i++) {

float x = p[i].x;

float y = p[i].y;

p[i].x = x * cos(rad) - y * sin(rad);

p[i].y = x * sin(rad) + y * cos(rad);

int main() {

int choice;

float tx, ty, sx, sy, angle;

printf("Enter number of vertices: ");

scanf("%d", &n);

printf("Enter coordinates (x y):\n");

for (int i = 0; i < n; i++) {

scanf("%f %f", &p[i].x, &p[i].y);

display("Original Object:");

while (1) {

printf("\n--- 2D Transformations Menu ---\n");

printf("1. Translation\n");

printf("2. Scaling\n");

printf("3. Rotation\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter choice: ");

scanf("%d", &choice);

17
switch (choice) {

case 1:

printf("Enter tx ty: ");

scanf("%f %f", &tx, &ty);

translate(tx, ty);

display("After Translation:");

break;

case 2:

printf("Enter sx sy: ");

scanf("%f %f", &sx, &sy);

scale(sx, sy);

display("After Scaling:");

break;

case 3:

printf("Enter angle: ");

scanf("%f", &angle);

rotate(angle);

display("After Rotation:");

break;

case 4:

display("Current Object:");

break;

case 5:

return 0;

default:

printf("Invalid choice!\n");

18
OUTPUT :

Enter number of vertices: 3

Enter coordinates (x y):

13 19

24 28

18 27

Original Object:

(13.00, 19.00)

(24.00, 28.00)

(18.00, 27.00)

--- 2D Transformations Menu ---

1. Translation

2. Scaling

3. Rotation

4. Display

5. Exit

Enter choice: 1

Enter tx ty: 2 5

After Translation:

(15.00, 24.00)

(26.00, 33.00)

(20.00, 32.00)

--- 2D Transformations Menu ---

1. Translation

2. Scaling

3. Rotation

19
4. Display

5. Exit

Enter choice: 2

Enter sx sy: 4 9

After Scaling:

(60.00, 216.00)

(104.00, 297.00)

(80.00, 288.00)

--- 2D Transformations Menu ---

1. Translation

2. Scaling

3. Rotation

4. Display

5. Exit

Enter choice: 3

Enter angle: 60°

After Rotation:

(-157.06, 159.96)

(-205.21, 238.57)

(-209.42, 213.28)

--- 2D Transformations Menu ---

1. Translation

2. Scaling

3. Rotation

4. Display

5. Exit

=== Code Execution Successful ===

20
Q.7 Write a program to apply various 3D transformations on a 3D object
and then apply parallel and perspective projection on it.
Source Code :
#include <stdio.h>

#include <math.h>

#define MAX 20

typedef struct {

float x, y, z, w;

} Point;

Point p[MAX], result[MAX];

int n;

void transform(float mat[4][4]) {

for (int i = 0; i < n; i++) {

result[i].x = p[i].x * mat[0][0] + p[i].y * mat[0][1] + p[i].z * mat[0][2] + p[i].w * mat[0][3];

result[i].y = p[i].x * mat[1][0] + p[i].y * mat[1][1] + p[i].z * mat[1][2] + p[i].w * mat[1][3];

result[i].z = p[i].x * mat[2][0] + p[i].y * mat[2][1] + p[i].z * mat[2][2] + p[i].w * mat[2][3];

result[i].w = 1;

for (int i = 0; i < n; i++)

p[i] = result[i];

void display(char *msg) {

printf("\n%s\n", msg);

for (int i = 0; i < n; i++) {

printf("(%.2f, %.2f, %.2f)\n", p[i].x, p[i].y, p[i].z);

void translate(float tx, float ty, float tz) {

float mat[4][4] = {

21
{1,0,0,tx},

{0,1,0,ty},

{0,0,1,tz},

{0,0,0,1}

};

transform(mat);

void scale(float sx, float sy, float sz) {

float mat[4][4] = {

{sx,0,0,0},

{0,sy,0,0},

{0,0,sz,0},

{0,0,0,1}

};

transform(mat);

void rotateZ(float angle) {

float rad = angle * 3.1416 / 180;

float mat[4][4] = {

{cos(rad), -sin(rad), 0, 0},

{sin(rad), cos(rad), 0, 0},

{0,0,1,0},

{0,0,0,1}

};

transform(mat);

void parallelProjection() {

printf("\nParallel Projection (XY plane):\n");

for (int i = 0; i < n; i++) {

printf("(%.2f, %.2f)\n", p[i].x, p[i].y);

22
}

void perspectiveProjection(float d) {

printf("\nPerspective Projection:\n");

for (int i = 0; i < n; i++) {

float xp = p[i].x / (1 + p[i].z / d);

float yp = p[i].y / (1 + p[i].z / d);

printf("(%.2f, %.2f)\n", xp, yp);

int main() {

float tx, ty, tz, sx, sy, sz, angle, d;

printf("Enter number of points: ");

scanf("%d", &n);

printf("Enter 3D coordinates (x y z):\n");

for (int i = 0; i < n; i++) {

scanf("%f %f %f", &p[i].x, &p[i].y, &p[i].z);

p[i].w = 1;

display("Original Object:");

printf("\nEnter translation (tx ty tz): ");

scanf("%f %f %f", &tx, &ty, &tz);

translate(tx, ty, tz);

display("After Translation:");

printf("\nEnter scaling (sx sy sz): ");

23
scanf("%f %f %f", &sx, &sy, &sz);

scale(sx, sy, sz);

display("After Scaling:");

printf("\nEnter rotation angle (Z-axis): ");

scanf("%f", &angle);

rotateZ(angle);

display("After Rotation:");

parallelProjection();

printf("\nEnter distance for perspective projection: ");

scanf("%f", &d);

perspectiveProjection(d);

return 0;

OUTPUT :
Enter number of points: 3

Enter 3D coordinates (x y z):

256

361

975

Original Object:

(2.00, 5.00, 6.00)

(3.00, 6.00, 1.00)

(9.00, 7.00, 5.00)

Enter translation (tx ty tz): 1 5 8

After Translation:

(3.00, 10.00, 14.00)

24
(4.00, 11.00, 9.00)

(10.00, 12.00, 13.00)

Enter scaling (sx sy sz): 2 7 4

After Scaling:

(6.00, 70.00, 56.00)

(8.00, 77.00, 36.00)

(20.00, 84.00, 52.00)

Enter rotation angle (Z-axis): 90°

After Rotation:

(-70.00, 6.00, 56.00)

(-77.00, 8.00, 36.00)

(-84.00, 20.00, 52.00)

Parallel Projection (XY plane):

(-70.00, 6.00)

(-77.00, 8.00)

(-84.00, 20.00)

Enter distance for perspective projection:

Perspective Projection:

(-0.00, 0.00)

(-0.00, 0.00)

(-0.00, 0.00)

=== Code Execution Successful ===

25

You might also like