CG EXP-2
CG EXP-2
Experiment No.: 2
Roll Number: 19
Date of Performance:31/07/2024
Evaluation
Marks
Performance Indicator Max. Marks
Obtained
Performance 5
Understanding 5
Journal work and timely submission. 10
Total 20
Checked by
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
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>
dx = x2 - x1;
dy = y2 - y1;
x = x1;
y = y1;
p = 2 * dy - dx;
void main() {
int gdriver = DETECT, gmode, x1, y1, x2, y2;
Output:
Conclusion:
1. Why is Bresenham's algorithm preferred over the DDA algorithm?
2. What type of arithmetic is used in Bresenham's algorithm?