Note 2
Note 2
(Line-Drawing
Algorithms)
Sourav Pramanik
Assistant Professor, Dept. of Computer Science, New Alipore College
Line-Drawing Algorithm
y
• Line drawing is accomplished by calculating 5
intermediate positions along the line path ??
between two specified endpoint positions.
An output device is then directed to fill in
2
these positions between the endpoints.
DDA (Digital Differential Analyzer)
Algorithm 2 3 4 5 6 7
x
Bresenham’s Line Algorithm
Line: (2, 2) to (7, 7)
Which intermediate
pixels to turn on?
DDA Algorithm
• The digital differential analyser (DDA) is a scan-
conversion line algorithm based on using x or y.
0 1 2 3 4 5 6 7 8 9 10 x
𝒚 = 𝒎𝒙 + 𝒄
Bresenham’s Line Algorithm
𝒚𝒌 + 𝟏
𝒅𝟐
𝒚
𝒅𝟏 y 𝒙𝒌 + 𝟏, 𝒚
𝒚𝒌
𝑦 = 𝑚𝑥 + 𝑐
⇒ y = m 𝑥𝑘 + 1 + c
Decision Parameters
𝑑1 = 𝑦 − 𝑦𝑘 =m 𝑥𝑘 + 1 + c − 𝑦𝑘 −−−− −(1)
𝑖 𝑖𝑓 𝑑1 − 𝑑2 < 0, select pixel
𝑥𝑘 + 1, 𝑦𝑘
𝑑2 = (𝑦𝑘 +1) − 𝑦 = 𝑦𝑘 + 1 − m 𝑥𝑘 + 1 + c
=𝑦𝑘 + 1 − 𝑚 𝑥𝑘 + 1 − 𝑐 −− −(2) 𝑖𝑖 𝑖𝑓 𝑑1 − 𝑑2 > 0, select
pixel 𝑥𝑘 + 1, 𝑦𝑘 + 1
Bresenham’s Line Algorithm
𝑑1 − 𝑑2 = 𝑚 𝑥𝑘 + 1 + 𝑐 − 𝑦𝑘 − 𝑦𝑘 + 1 − 𝑚 𝑥𝑘 + 1 − 𝑐
= 𝑚 𝑥𝑘 + 1 + 𝑐 − 𝑦𝑘 − 𝑦𝑘 − 1 + 𝑚 𝑥𝑘 + 1 + 𝑐
∆𝑦
In (3), there is a problem, 𝑚 is there, 𝑚 = ∆𝑥
, it may gives float results.
Bresenham’s Line Algorithm
As discussed earlier, for any given/calculated previous pixel P(Xp, Yp), there are two
candidates for the next pixel closest to the line, E(Xp+1, Yp) and NE(Xp+1, Yp+1)
(E stands for East and NE stands for North-East).
Mid-Point Line Generation Algorithm
-> For all points (x,y) above the line, F(x, y) result in a
negative number.
-> And for all points (x,y) below the line, F(x, y) result in
a positive number.
Mid-Point Line Generation Algorithm
This relationship is used to determine the relative
position of M
M = (Xp+1, Yp+1/2)
From (2), we have
∴𝑑=𝐹 𝑀
So our decision parameter d is, = 𝐹 𝑥𝑝 + 1, 𝑦𝑝 + 1ൗ2
d = F(M) = F(Xp+1, Yp+1/2) ---------(2) = 𝑎 𝑥𝑝 + 1 + 𝑏 𝑦𝑝 + 1ൗ2
+ 𝑐; 𝑎𝑡 𝑀
How to efficiently find new value of d from its old value? If 𝑑 > 0
For simplicity, let as write F(x, y) as ax + by + c. choose NE
Else
Where a = dy
choose E
b = -dx Set d_old=d
c = B*dx
We got these values from above equation (1)
Mid-Point Line Generation Algorithm
Case 1: If E is chosen then for next point :
d_new = F(Xp+2, Yp +1/2)
= a(Xp+2) + b(Yp +1/2) + c
dy = Y2- Y1; dx = X2 - X1
// initial value of
// decision parameter d
d = 2dy - dx; x = X1 , y = Y1
// 'E' is chosen
if (d <= 0)
d = d + 2dy
// 'NE' is chosen
else
d = d + 2(dy – dx)
y = y+1
Plot(x,y)