0% found this document useful (0 votes)
8 views

CG EXP-2

Uploaded by

buktarshubham
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 views

CG EXP-2

Uploaded by

buktarshubham
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
You are on page 1/ 6

Vidyavardhini’s College of Engineering & Technology

Department of Computer Science and Engineering (Data Science)

ACADEMIC YEAR: 2024-25


Course: Computer Graphics Lab
Course code: CSL303
Year/Sem: SE/III

Experiment No.: 2

Aim: Bresenham’s Line Drawing Algorithm

Name: Shubham Buktar

Roll Number: 19

Date of Performance:31/07/2024

Date of Submission: 07/08/2024

Evaluation
Marks
Performance Indicator Max. Marks
Obtained
Performance 5
Understanding 5
Journal work and timely submission. 10
Total 20

Performance Exceed Expectations Meet Expectations Below Expectations


Indicator (EE) (ME) (BE)
Performance 5 3 2
Understanding 5 3 2
Journal work and
10 8 4
timely submission.

Checked by

Name of Faculty : Ms. Kranti Gule


Signature :
Date :

CSL303-Computer Graphics Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Science and Engineering (Data Science)

Aim Write a program to implement Bresenham Line Drawing Algorithm in C.

Objective To implement Bresenham line drawing algorithm for drawing a line


segment between two points A (x1, y1) and B (x2, y2)

Theory Bresenham Algorithm


This algorithm is used for scan converting a line. It was developed by
Bresenham. It is an efficient method because it involves only integer
addition, subtractions, and multiplication operations. These operations
can be performed very rapidly so lines can be generated quickly.
In this method, next pixel selected is that one who has the least distance
from true line.

Basic Concept:
Move across the x axis in unit intervals and at each step choose
jbetween two different y coordinates.
For example, from position (2, 3) we have to choose between (3, 3) and
(3, 4). We would like the point that is closer to the original line.
So we have to take decision to choose next point. So next pixels are
selected based on the value of decision parameter p. The equations are
given in below algorithm

CSL303-Computer Graphics Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Science and Engineering (Data Science)

Algorithm
Step1: Start Algorithm
Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: Consider (x, y) as starting point and xend as maximum possible
value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend
Stop.
Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step10: Draw a point of latest (x, y) coordinates
Step11: Go to step 7
Step12: End

Program: #include<stdio.h>
#include<graphics.h>

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


int dx, dy, p, x, y;

CSL303-Computer Graphics Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Science and Engineering (Data Science)

dx = x2 - x1;
dy = y2 - y1;
x = x1;
y = y1;
p = 2 * dy - dx;

while(x < x2) {


if(p >= 0) {
putpixel(x, y, 7);
p = p + 2 * dy - 2 * dx;
y = y + 1;
} else {
putpixel(x, y, 7);
p = p + 2 * dy;
}
x = x + 1;
}
}

void main() {
int gdriver = DETECT, gmode, x1, y1, x2, y2;

// Initialize the graphics mode


initgraph(&gdriver, &gmode, "C:\\tc\\BGI");

// Get the coordinates of the first point


printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x1, &y1);

CSL303-Computer Graphics Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Science and Engineering (Data Science)

// Get the coordinates of the second point


printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x2, &y2);

// Draw the line using the drawline function


drawline(x1, y1, x2, y2);

// Wait for user input and close the graphics window


getch();
closegraph();
}

Output:

CSL303-Computer Graphics Lab


Vidyavardhini’s College of Engineering & Technology
Department of Computer Science and Engineering (Data Science)

Conclusion:
1. Why is Bresenham's algorithm preferred over the DDA algorithm?
2. What type of arithmetic is used in Bresenham's algorithm?

CSL303-Computer Graphics Lab

You might also like