Bresenhams Line Drawing Algorithm
Bresenhams Line Drawing Algorithm
Algorithm
Step 1: Enter the 2 end points for a line and store the left end point in (X0,Y0).
Step 2: Plot the first point be loading (X0,Y0) in the frame buffer.
Setp 3: determine the initial value of the decision parameter by calculating the
constants
dx, dy, 2dy and 2dy-2dx as
dx=x1-x0, dy=y1-y0, P0 = 2dy –dx
Step 4: for each Xk, conduct the following test, starting from k= 0
If Pk <0, then the next point to be plotted is at (Xk+1, Yk) and, Pk+1 = Pk + 2dy
Else, the next point is (Xk+1, Yk+1) and
Pk+1 = Pk + 2dy –2dx (step 3)
Step 5: iterate through step (4) dx times.
Examples
Problem-01:
Calculate the points between the starting coordinates (9, 18) and ending coordinates
(14, 22).
Solution-
Given-
ΔX = Xn – X0 = 14 – 9 = 5
ΔY =Yn – Y0 = 22 – 18 = 4
Step-02:
Pk
= 2ΔY – ΔX
=2x4–5
=3
Step-03:
Thus,
Similarly, Step-03 is executed until the end point is reached or number of iterations
equals to 4 times.
(Number of iterations = ΔX – 1 = 5 – 1 = 4)
Problem-02:
Calculate the points between the starting coordinates (20, 10) and ending
coordinates (30, 18).
Solution-
Given-
Starting coordinates = (X0, Y0) = (20, 10)
Ending coordinates = (Xn, Yn) = (30, 18)
Step-01:
Calculate ΔX and ΔY from the given input.
ΔX = Xn – X0 = 30 – 20 = 10
ΔY =Yn – Y0 = 18 – 10 = 8
Step-02:
Step-03:
Thus,
Pk+1 = Pk + 2ΔY – 2ΔX = 6 + (2 x 8) – (2 x 10) = 2
Xk+1 = Xk + 1 = 20 + 1 = 21
Yk+1 = Yk + 1 = 10 + 1 = 11
20 10
6 2 21 11
2 -2 22 12
-2 14 23 12
14 10 24 13
10 6 25 14
6 2 26 15
2 -2 27 16
-2 14 28 16
14 10 29 17
10 6 30 18