You are on page 1of 28

CD 326: Computer Graphics

Lecture – Bresenham’s Line Drawing Algorithm

SALIM DIWANI
Bresenham Algorithm
❖The Bresenham algorithm is another incremental
scan conversion algorithm.

❖The big advantage of this algorithm is that, it uses


only integer calculations.

❖Moving across the x axis in unit intervals and at


each step choose between two different y
coordinates.

2
Bresenham Algorithm
Advantages of Bresenham Line Drawing Algorithm
❖It is easy to implement.
❖It is fast and incremental.
❖It executes fast but less faster than DDA
Algorithm.
❖The points generated by this algorithm are more
accurate than DDA Algorithm.
❖It uses fixed points only.

3
Bresenham Algorithm
Disadvantages of Bresenham Line Drawing
Algorithm
❖Though it improves the accuracy of generated
points but still the resulted line is not smooth.
❖This algorithm is for the basic line drawing.
❖It can not handle diminishing jaggies.

4
Bresenham Algorithm
For example, as shown in the following
illustration, from position (2, 3) you need to
choose between (3, 3) and (3, 4). You would like
the point that is closer to the original line.

5
Bresenham Algorithm
At sample position xk+1, the vertical separations
from the mathematical line are labelled as
dupper and dlower

6
Bresenham Algorithm

7
Bresenham Algorithm
From the above illustration, the y coordinate on
the mathematical line at xk+1 is:
𝑌 = 𝑚(𝑋𝑘 + 1) + 𝑏
So, dupper and dlower are given as follows:
𝑑𝑙𝑜𝑤𝑒𝑟 = 𝑦 − 𝑦𝑘
= 𝑚(𝑋𝑘 + 1) + 𝑏 − 𝑌𝑘
and
𝑑𝑢𝑝𝑝𝑒𝑟 = (𝑦𝑘 + 1) − 𝑦
= 𝑌𝑘 + 1 − 𝑚(𝑋𝑘 + 1) − b 8
Bresenham Algorithm
You can use these to make a simple decision about
which pixel is closer to the mathematical line. This
simple decision is based on the difference between
the two pixel positions.
𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 = 2𝑚(𝑥𝑘 + 1) − 2𝑦𝑘 + 2𝑏 − 1

9
Bresenham Algorithm
Let us substitute m with dy/dx where dx and dy are
the differences between the end points.
𝑑𝑥(𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟) = 𝑑𝑥(2 𝑑𝑦 /𝑑𝑥 (𝑥𝑘 + 1) − 2𝑦𝑘 + 2𝑏 − 1)
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 2𝑑𝑦 + 𝑑𝑥(2𝑏 − 1)
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + C
The sign of the decision parameter pk is the same as that of
dlower – dupper

10
Bresenham Algorithm
If pk is negative, then choose the lower pixel,
otherwise choose the upper pixel.
Remember, the coordinate changes occur along the
x axis in unit steps, so you can do everything with
integer calculations. At step k+1, the decision
parameter is given as:
𝑝𝑘+1 = 2𝑑𝑦 ∙ 𝑥𝑘+1 − 2𝑑𝑥 ∙ 𝑦𝑘+1 + 𝐶
Subtracting pk from this we get:
𝑝𝑘+1 − 𝑝𝑘 = 2𝑑𝑦(𝑥𝑘+1 − 𝑥𝑘) − 2𝑑𝑥(𝑦𝑘+1 − 𝑦𝑘)
11
Bresenham Algorithm
But, xk+1 is the same as xk+1. So:
𝑝𝑘+1 = 𝑝𝑘 + 2𝑑𝑦 − 2𝑑𝑥(𝑦𝑘+1 − 𝑦𝑘)
Where, yk+1 – yk is either 0 or 1 depending on the sign of pk.
The first decision parameter p0 is evaluated at (x0, y0) is
given as:
𝑝0 = 2𝑑𝑦 − 𝑑x

If PK > 0 Pk+1 = Pk + 2dy- 2dx

If PK < 0 Pk+1 = Pk + 2dy


12
Bresenham Algorithm

13
Bresenham Algorithm
Procedure
Given
Starting coordinates = (X0, Y0)
Ending coordinates = (Xn, Yn)
The points generation using Bresenham Line Drawing
Algorithm involves the following steps

14
Bresenham Algorithm
Step-01:
Calculate ΔX and ΔY from the given input.
These parameters are calculated as
ΔX = Xn – X0
ΔY =Yn – Y0

15
Bresenham Algorithm
Step-02:
Calculate the decision parameter Pk.
It is calculated as-

16
Bresenham Algorithm
Step-03:
Suppose the current point is (Xk, Yk) and the next point is
(Xk+1, Yk+1).

Find the next point depending on the value of decision


parameter Pk.

Follow the below two cases-

17
Bresenham Algorithm

18
Practice
Step-04:
Keep repeating Step-03 until the end point is
reached or number of iterations equals to (ΔX-1)
times.

19
Practice
Calculate the points between the starting
coordinates (9, 18) and ending coordinates (14,
22).
Solution-
Given-
Starting coordinates = (X0, Y0) = (9, 18)
Ending coordinates = (Xn, Yn) = (14, 22)

20
Practice
Step-01:
Calculate ΔX and ΔY from the given input.
ΔX = Xn – X0 = 14 – 9 = 5
ΔY =Yn – Y0 = 22 – 18 = 4

21
Practice
Step-02:
Calculate the decision parameter.
Pk
= 2ΔY – ΔX
=2x4–5
=3
So, decision parameter Pk = 3

22
Practice
Step-03:
As Pk >= 0, so case-02 is satisfied.
Thus,
Pk+1 = Pk + 2ΔY – 2ΔX = 3 + (2 x 4) – (2 x 5) = 1
Xk+1 = Xk + 1 = 9 + 1 = 10
Yk+1 = Yk + 1 = 18 + 1 = 19

23
Practice
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)

24
Practice

25
Practice

26
Quiz
Problem-01:
Calculate the points between the
starting coordinates (20, 10) and
ending coordinates (30, 18).

27
Lab Assignment
Draw a line by using Bresenham
algorithms. Enter the xa & ya value:
200 200 and xb & yb value: 350 45

28

You might also like