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

UNIT - 2 Line Drawing Algorithm

Uploaded by

yehid13435
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

UNIT - 2 Line Drawing Algorithm

Uploaded by

yehid13435
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 120

https://genuinenotes.

com

Line Drawing

Nipun Thapa (Computer Graphics)


Algorithm
Unit 2

1
https://genuinenotes.com

Points and Lines


• Points
• Plotted by converting co-ordinate position to appropriate
operations for the output device (e.g.: in CRT monitor, the electron
beam is turned on to illuminate the screen phosphor at the
selected location.)

Nipun Thapa (Computer Graphics)


• Line
• Plotted by calculating intermediate positions along the line
path between two specified endpoint positions.
• Screen locations are referenced with integer values, so
plotted positions may only approximate actual line
positions between two specified endpoints  “the
jaggies”. E.g.: position (10.48,20.51)  (10,21).

2
https://genuinenotes.com

Pixel Position
• Pixel position: referenced by scan line number and column
number

Nipun Thapa (Computer Graphics)


3
https://genuinenotes.com

LINE DRAWING ALGORITHM


• DDA Algorithm- (Digital Differential Analyzer)
• Bresenham’s Line Algorithm
Note:
• Pixel- picture element smallest piece of information in an
image represented using dots, squares and rectangle

Nipun Thapa (Computer Graphics)


• Components-
• RGB
• or
Y Scan Line
• 4 components
(cyan ,magenta,
yellow & black)
Pixel
0 4
0
Column number X
https://genuinenotes.com

DDA Algorithm
 Digital Differential Analyzer
Scan-conversion line – algorithm based on calculating either

Nipun Thapa (Computer Graphics)


Sample the line at unit intervals in one coordinate
Determine the corresponding integer values nearest the line
path in another co-ordinate

5
https://genuinenotes.com

CASE 1
Positive Slope
(Y always increase when X increases and
Y always Decreases when X decrease)

Nipun Thapa (Computer Graphics)


CASE 2
Negative Slope
(Y always increase when X decrease and
Y always Decreases when X increases)
6
CASE 1 https://genuinenotes.com

Positive Slope
1. Slope less than 1 ( |M| < 1 ) and
moving Left to Right
M=1

Nipun Thapa (Computer Graphics)


Xk+1 = Xk + 1
Y2
YK+1 = Yk + M
Y1

7
X1 X2
CASE 1 https://genuinenotes.com

Positive Slope
2. Slope less than 1 ( |M| < 1 ) and
moving Right to Left
M=1

Nipun Thapa (Computer Graphics)


Xk+1 = Xk - 1
Y1
YK+1 = Yk - M
Y2

8
X2 X1
CASE 1 https://genuinenotes.com

Positive Slope
3. Slope greater than 1 ( |M| > 1 ) and
moving Left to Right
M=1

Y2

Nipun Thapa (Computer Graphics)


Xk+1 = Xk + 1/M
YK+1 = Yk + 1 Y1

X1 X2 9
CASE 1 https://genuinenotes.com

Positive Slope
4. Slope Greater than 1 ( |M| > 1 ) and
moving Right to Left
M=1

Y2

Nipun Thapa (Computer Graphics)


Xk+1 = Xk – 1/M
YK+1 = Yk - 1 Y1

X1 X2 10
CASE 2
https://genuinenotes.com

Negative Slope
1. Slope less than 1 ( |M| < 1 ) and
moving Left to Right

M=-1

Nipun Thapa (Computer Graphics)


Xk+1 = Xk + 1
Y1
YK+1 = Yk + M
Y2

X2 11
X1
CASE 2
https://genuinenotes.com

Negative Slope
2. Slope less than 1 ( |M| < 1 ) and
moving Right to Left

M=-1

Nipun Thapa (Computer Graphics)


Xk+1 = Xk - 1
Y1
YK+1 = Yk - M
Y2

X2 12
X1
CASE 2
https://genuinenotes.com

Negative Slope
3. Slope greater than 1 ( |M| > 1 ) and
moving Left to Right

M=-1

Nipun Thapa (Computer Graphics)


Y1
Xk+1 = Xk – 1/M
Y2
YK+1 = Yk - 1

X1 X2 13
CASE 2
https://genuinenotes.com

Negative Slope
4. Slope Greater than 1 ( |M| > 1 ) and
moving Right to Left

M=-1

Nipun Thapa (Computer Graphics)


Y1
Xk+1 = Xk +1/M
Y2
YK+1 = Yk +1

X1 X2 14
DDA
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


15
https://genuinenotes.com
DDA Examples
Q.> Digitize a Line with end point A(2,3) and B(6,8) , using DDA.
A.> Here , Slope (M) = (8-3)/(6-2) = 1.25
here Slope is positive and greater than 1
And moving left to right and 1/m = 0.8
So , we use X = X + 1/M
k+1 k
YK+1 = Yk + 1

Nipun Thapa (Computer Graphics)


K Xk+1 = Xk + 1/M YK+1 = Yk + 1 (X,Y)

1 =2 + 0.8 = 2.8 ~ 3 4 (3,4)


2 =2.8 + 0.8= 3.6 ~ 4 5 (4,5)
3 =3.6 + 0.8 = 4.4 ~ 4 6 (4,6)
4 = 4.4 + 0.8 = 5.2 ~ 5 7 (5,7)
5 = 5.2 + 0.8 = 6 8 (6,8) 16
https://genuinenotes.com
DDA Examples
Q.> Digitize a Line with end point A(2,3) and B(6,8) , using DDA
Right to left.

Q.> Digitize a Line with end point A(3,2) and B(8,4) , using DDA

Nipun Thapa (Computer Graphics)


Q.>Digitize a Line with end point A(2,6) and B(4,2) , using DDA

17
https://genuinenotes.com
DDA Final Exercise
1. Digitize the line from A(1,1) to B(10,8) using scan conversion line
algorithm.
2. Draw a line with two end point P(5,3) and Q(1,2) using DDA
3. Digitize the line with endpoints A(1,7) and B(6,3) using digital
differential analyzer line drawing algorithm. Show all necessary
steps. (TU) .

Nipun Thapa (Computer Graphics)


4. Digitize the line with end points (1,2) and (5,6) using digital
differential analyzer method.(TU).
5. Digitize the given line endpoints (1,1) and (5, 5) using DDA.
6. Draw a line using DDA with two end points A(0,0) and B(-10,8).
7. Digitize line with start at (5,3) and end at (1,5) using DDA.
8. Digitize DDA with all necessary steps with two end points (0,0)
and (10,0). 18
https://genuinenotes.com

Bresenham’s Line Algorithm


The BLA is a more efficient method used to plot pixel position
along a straight-line path.
Advantage of BLA over DDA
• In DDA algorithm each successive point is computed in floating
point, so it requires more time and more memory space. While in

Nipun Thapa (Computer Graphics)


BLA each successive point is calculated in integer value or whole
number. So it requires less time and less memory space.
• In DDA, since the calculated point value is floating point number, it
should be rounded at the end of calculation but in BLA it does not
need to round, so there is no accumulation of rounding error.
• Due to rounding error, the line drawn by DDA algorithm is not
accurate, while in BLA line is accurate.
• DDA algorithm cannot be used in other application except line
drawing, but BLA can be implemented in other application such as
circle, ellipse and other curves. 19
https://genuinenotes.com

BLA for slope +ve and |m| ≤ 1

Nipun Thapa (Computer Graphics)


+1 +1

+1 +1

+1 +1 20
https://genuinenotes.com
Let us assume that pixel (xk, yk) is already
plotted assuming that the sampling direction is
along X-axis i.e. (xk+1, yk) or (xk+1, yk+1). Thus, the
common equation of the line is
y = m(xk+1) + c
Now,

Nipun Thapa (Computer Graphics)


d1 = y - yk = m (xk +1) + c - yk
and
d2 = (yk +1)- y = yk+1– {m (xk +1) + c }

21
https://genuinenotes.com

The difference betn these two separation is,


d1 - d2 = [m (xk +1) + c - yk ] - [yk+1– {m (xk +1) + c }]
Or,
d1- d2 = 2m (xk+1)+ 2c - 2yk -1
Since, slope of line (m) = Δy / Δx,

Nipun Thapa (Computer Graphics)


We have,
Δx (d1- d2) = 2 Δy (xk+1)+ 2 Δx C - 2 Δx Yk – Δx
Define Decision parameter at Kth step,
Pk = Δx (d1- d2)
= 2 Δy (xk+1)+ 2 Δx C - 2 Δx yk – Δx
22
=2 Δy Xk+2 Δy + 2 Δx c - 2 Δx yk – Δx
https://genuinenotes.com
Pk=2 Δy Xk+2 Δy + 2 Δx c - 2 Δx yk – Δx
Now , K+1th term
Pk+1 = 2 Δy Xk+1+2 Δy + 2 Δx c - 2 Δx yk+1 – Δx

Here,
Pk+1- Pk = 2 Δy Xk+1+2 Δy + 2 Δx c - 2 Δx yk+1 – Δx – {2 Δy Xk+2
Δy + 2 Δx c - 2 Δx yk – Δx }

Nipun Thapa (Computer Graphics)


= 2 Δy Xk+1+2 Δy + 2 Δx c - 2 Δx yk+1 – Δx – 2 Δy Xk- 2
Δy - 2 Δx c + 2 Δx yk + Δx
Pk+1 = Pk +2 Δy Xk+1 - 2 Δx yk+1– 2 Δy Xk+ 2 Δx yk

23
Pk+1 = Pk +2 Δy(Xk+1 – Xk)- 2 Δx(Yk+1– Yk)
https://genuinenotes.com
Case 1
If Pk < 0 (i.e. d1-d2 is Negative )
then,
Xk+1 = Xk+1
Yk+1 = Yk

Pk+1 = Pk + 2 Δy

Nipun Thapa (Computer Graphics)


Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk+1
Yk+1 = Yk+1

Pk+1 = Pk + 2 Δy -2Δx 24
https://genuinenotes.com
Initial Decision Parameter (P0)

y = m(xk+1) + c

Let, X0 = 0 , Y0 = 0 then C = 0

Δx (d1- d2) = 2 Δy Xk+2 Δy + 2 Δx c - 2 Δx yk – Δx

Nipun Thapa (Computer Graphics)


P0 = 0 + 2 Δy + 0 + 0 – Δx

P0 = 2 Δy - Δx

25
https://genuinenotes.com
Example-1: Digitize the line with end points (20, 10) and (30, 18)
using BLA.
Solution :
Here, Starting point of line = (x1, y1) = (20, 10) and
Ending point of line = (x2, y2) = (30, 18)
Thus, slope of line, m = Δy / Δx = y2-y1 / x2-x1
= (18-10) / (30-20)

Nipun Thapa (Computer Graphics)


= 8/10
As the given points, it is clear that the line is moving left to right
with the positive slop
|m| = 0.8 <1

26
Thus, https://genuinenotes.com
The initial decision parameter (P0) = 2Δy - Δx = 2*8 – 10 = 6
Since, for the Bresenham’s Line drawing Algorithm of slope, |m| ≤ 1, we have

If Pk < 0 (i.e. d1-d2 is Negative ) If Pk ≥ 0


then, then,
Xk+1 = Xk+1 Xk+1 = Xk+1
Yk+1 = Yk Yk+1 = Yk+1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx
(20, 10)

Nipun Thapa (Computer Graphics)


27
https://genuinenotes.com
Example-2: Digitize the line with end points (15, 5) and (30, 10)
using BLA.

Nipun Thapa (Computer Graphics)


28
https://genuinenotes.com
Example-3: Digitize the line with end points (20, 5) and (25, 10)
using BLA.

Nipun Thapa (Computer Graphics)


29
https://genuinenotes.com

BLA for slope +ve and |m| > 1


(Xk, ) (Xk, )

Nipun Thapa (Computer Graphics)


( ,Yk) ( ,Yk)

+1 +1

+1 +1
30
+1 +1
https://genuinenotes.com

Let us assume that pixel (xk, yk) is already


plotted assuming that the sampling direction is
along X-axis i.e. (xk, yk+1) or (xk+1, yk+1). Thus, the
common equation of the line is
Y=MX+C
yK+1 = mx + c

Nipun Thapa (Computer Graphics)


X= {yk+1 – c}/m
Now,

d1 = X - Xk = {yk +1 – c}/m - xk
and
d2 = (Xk +1)- X = xk+1– {yk+1 – c}/m 31
https://genuinenotes.com
So,
d1 - d2 = [{yk +1 – c}/m - xk] -[xk+1–
{{yk+1 – c}/m }]
Or,
d1 - d2 = 2/m* (yk+1) - 2c/m – 2xk -1

Nipun Thapa (Computer Graphics)


Since, slope of line (m) = Δy / Δx,
we have
Pk = Δy (d1 - d2)
= 2Δx (yk+1) - 2c Δx - 2Δy xk - Δy 32
https://genuinenotes.com
Pk = 2Δx (yk+1) - 2c Δx - 2Δy xk - Δy
Now , K+1th term
Pk+1 = 2Δx (yk+1+1) - 2c Δx - 2Δy xk+1 – Δy
Here,
Pk+1-Pk = {2Δx (yk+1+1) - 2c Δx - 2Δy xk+1 – Δy} -

Nipun Thapa (Computer Graphics)


{2Δx (yk+1) - 2c Δx - 2Δy xk - Δy}
= 2Δx (yk+1 - yk) - 2Δy (xk+1 - xk)
Or,

Pk+1 = Pk + 2Δx (yk+1 - yk) - 2Δy (xk+1 - xk) 33


https://genuinenotes.com
Pk+1 = Pk + 2Δx (yk+1 - yk) - 2Δy (xk+1 - xk)
Case 1
If Pk < 0 (i.e. d1-d2 is Negative )
then,
Xk+1 = Xk
Yk+1 = Yk+1

Pk+1 = Pk + 2 Δx

Nipun Thapa (Computer Graphics)


Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk+1
Yk+1 = Yk+1
34
Pk+1 = Pk + 2 Δx -2Δy
https://genuinenotes.com

Initial Decision Parameter (P0)


We Know,
Pk = 2Δx (yk+1) - 2c Δx - 2Δy xk - Δy
Let, X0 = 0 , Y0 = 0 then C = 0
Then,
P0 = 2Δx (y0 +1) - 2c Δx - 2Δy x0 - Δy

Nipun Thapa (Computer Graphics)


P0 = 2 Δx - Δy
35
https://genuinenotes.com
Example-4: Digitize the line with end points (1, 0) and
(3, 3) using BLA.
Solution :
Here,
Starting point of line = (x1, y1) = (1, 0) and
Ending point of line = (x2, y2) = (3, 3)
Thus,

Nipun Thapa (Computer Graphics)


slope of line, m = Δy / Δx = y2-y1 / x2-x1
= (3-0) / (3-1)
= 3/2
As the given points, it is clear that the line is moving
left to right with the positive slope,
|m| = 1.5 >1 36
https://genuinenotes.com
Thus, the initial decision parameter
Δx = x2-x1 = 3-1 = 2
(P0) = 2Δx - Δy = 2*2 – 3 = 1 Δy = y2-y1 = 3-0 = 3
we have
If Pk < 0 If Pk ≥ 0
then, then,
Xk+1 = Xk Xk+1 = Xk+1
Yk+1 = Yk+1 Yk+1 = Yk+1
Pk = Pk + 2 Δx Pk = Pk + 2 Δx -2Δy

Nipun Thapa (Computer Graphics)


37
https://genuinenotes.com
Example-5: Digitize A(5,2) and B(7,8) using BLA.

Nipun Thapa (Computer Graphics)


38
https://genuinenotes.com
Example
1. Digitize the line A(1,1) and B(5,6) using BLA.
2. Digitize the line A(7,8) and B(1,4) using BLA.
3. Digitize the line A(1,1) and B(5,6) using DDA.
4. Digitize the line A(5,2) and B(7,8) using BLA.

Nipun Thapa (Computer Graphics)


5. Digitize the line A(0,0) and B(10,10) using
BLA.
6. Digitize the line A(1,3) and B(10,10) using
BLA and DDA.
39
https://genuinenotes.com

Slope –Ve and |M| ≤ 1


(Xk-1,Yk+1) (Xk-1,Yk+1)
(Xk,Yk+1)

d2
d2
Y
Y

d1

Nipun Thapa (Computer Graphics)


d1

(Xk-1,Yk) (Xk,Yk) (Xk-1,Yk) (Xk,Yk)

d1-d2 > 0 (Positive) d1-d2 < 0 (Negative)


Xk+1 = Xk - 1 Xk+1 = Xk - 1
Yk+1 = Yk +1 Yk+1 = Yk
40
https://genuinenotes.com
Let us assume that pixel (xk, yk) is already plotted assuming
that the sampling direction is along X-axis i.e. (xk-1, yk+1)
or (xk-1, yk). Thus, the common equation of the line is
Y=Mx + C
Here,
Y= m(Xk-1)+c
Now,
d1=Y-Yk

Nipun Thapa (Computer Graphics)


And
d2= (Yk+1)-Y
Or,
d1-d2= Y-Yk – {(Yk+1)-Y}
= Y-Yk –Yk-1+Y
=2y-2yk -1
=2m(Xk-1)+ 2c - 2yk - 1 41
Here slope of line (m) = - (Δy / Δx), {for –ve slope)
https://genuinenotes.com
d1- d2 = 2m(Xk-1)+ 2c - 2yk - 1
Now , (m) = - (Δy / Δx),
d1- d2 = -2 {(Δy / Δx)} (Xk-1)+ 2c - 2yk - 1
Δx(d1-d2) = -2 Δy (Xk-1)+ 2 c Δx – 2 Δx Yk - Δx
Here Pk = Δx(d1-d2)
= -2 Δy (Xk-1)+ 2 c Δx – 2 Δx Yk - Δx
Pk = -2 Δy Xk + 2 Δy + 2 c Δx – 2 Δx Yk - Δx
Now k+1 th term

Nipun Thapa (Computer Graphics)


Pk+1 = -2 Δy Xk+1 + 2 Δy + 2 c Δx – 2 Δx Yk+1 - Δx
Or,
Pk+1 – Pk = -2 Δy Xk+1 + 2 Δy + 2 c Δx – 2 Δx Yk+1 - Δx
+2 Δy Xk - 2 Δy - 2 c Δx + 2 Δx Yk + Δx
= -2Δy(Xk+1 - Xk) +2Δx (-Yk+1 + Yk)

Pk+1 = Pk - 2Δy(Xk+1 - Xk) + 2Δx (-Yk+1 + Yk) 42


https://genuinenotes.com
Pk+1 = Pk - 2Δy(X k+1 - Xk) - 2Δx (Yk+1 – Yk)

Case 1
If Pk < 0 (i.e. d1-d2 is Negative )
then,
Xk+1 = Xk - 1
Yk+1 = Yk

Pk+1 = Pk + 2 Δy

Nipun Thapa (Computer Graphics)


Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk - 1
Yk+1 = Yk +1
43
Pk+1 = Pk + 2 Δy -2Δx
https://genuinenotes.com

Initial Decision Parameter (P0)


We Know,
Pk = -2 Δy Xk + 2 Δy + 2 c Δx – 2 Δx Yk - Δx
Let, X0 = 0 , Y0 = 0 then C = 0
Then,
P0 = -2 Δy X0 + 2 Δy + 2 c Δx – 2 Δx Y0 - Δx

Nipun Thapa (Computer Graphics)


P0 = 2 Δy - Δx
44
https://genuinenotes.com

Slope –Ve and |M| > 1


(Xk-1,Yk+1) (Xk,Yk+1)
(Xk,Yk+1) (Xk-1,Yk+1)

d1 d2 d1
d2

Nipun Thapa (Computer Graphics)


(Xk,Yk) (Xk-1,Yk)
(Xk-1,Yk) X X
(Xk,Yk)

d1-d2 > 0 (Positive) d1-d2 < 0 (Negative)


Xk+1 = Xk - 1 Xk+1 = Xk
Yk+1 = Yk +1 Yk+1 = Yk +1
45
https://genuinenotes.com
Let us assume that pixel (xk, yk) is already plotted assuming that the
sampling direction is along X-axis i.e. (xk-1, yk+1) or (xk, yk +1). Thus,
the common equation of the line is
Y=Mx + C
Here,
Yk +1 = mX+c
X= {Yk +1-C}/M
Now,

Nipun Thapa (Computer Graphics)


d1=Xk-X Slope –Ve and |M|> 1
And
d2= X-{Xk-1}
Or,
d1-d2 = Xk-X – { X-{Xk-1} }
= Xk-X – X+Xk-1
= 2Xk – 2X -1
= 2Xk -2 {Yk +1-C}/M -1 46
Here slope of line (m) = - (Δy / Δx), {for –ve slope)
https://genuinenotes.com
d1 – d2 = 2Xk -2 {Yk +1-C}/M -1
= 2Xk -2 {Yk +1-C}/{- (Δy / Δx)} -1
= 2Xk + 2 {Yk +1-C}/{ (Δy / Δx)} -1
Δy (d1 – d2 ) = 2 Δy Xk + 2 Δx {Yk +1-C} - Δy
= 2 Δy Xk + 2 Δx Yk + 2 Δx - 2 Δx C - Δy
Pk = Δy (d1 – d2 ) for decision parameter
Pk = 2 Δy Xk + 2 Δx Yk + 2 Δx - 2 Δx C - Δy

Nipun Thapa (Computer Graphics)


Now K+1 th term
Pk+1 = 2 Δy Xk+1 + 2 Δx Yk+1 + 2 Δx - 2 Δx C - Δy
Or,
Pk+1- Pk = 2 Δy Xk+1 + 2 Δx Yk+1 + 2 Δx - 2 Δx C - Δy
- 2 Δy Xk - 2 Δx Yk - 2 Δx + 2 Δx C + Δy
= 2 Δy (Xk+1 - Xk ) + 2 Δx (Yk+1 - Yk )
47
Pk+1 = Pk +2 Δy (Xk+1 - Xk ) + 2 Δx (Yk+1 - Yk )
Pk+1 = Pk +2 Δyhttps://genuinenotes.com
(Xk+1 - Xk ) + 2 Δx (Yk+1 - Yk )
Case 1
If Pk < 0 (i.e. d1-d2 is Negative )
then,
Xk+1 = Xk
Yk+1 = Yk +1

Pk = Pk + 2 Δx

Nipun Thapa (Computer Graphics)


Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk - 1
Yk+1 = Yk +1
48
Pk = Pk+ 2Δx - 2 Δy
https://genuinenotes.com

Initial Decision Parameter (P0)


We Know,
Pk = 2 Δy Xk + 2 Δx Yk + 2 Δx - 2 Δx C - Δy
Let, X0 = 0 , Y0 = 0 then C = 0
Then,
P0 = 2 Δy X0 + 2 Δx Y0 + 2 Δx - 2 Δx C - Δy

Nipun Thapa (Computer Graphics)


P0 = 2 Δx - Δy
49
Example: Digitize the given line endpoints (5, 10) and (10, 7) using Bresenham’s line
https://genuinenotes.com
drawing algorithm.(TU 2014)
Solution:
Here, (X1,Y1)= (10,7) & Δx = |5-10| = 5
(X2,Y2)=(5,10) Δy = |10-7| = 3
M= (10-7)/(5-10) = -3/5 –ve slope and |M| < 1 here,
Initial (P0) = 2 Δy – Δx
If Pk < If Pk ≥ 0
Xk+1 = Xk - 1 = 2x3-5
Xk+1 = Xk - 1
Yk+1 = Yk Yk+1 = Yk +1 =1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx

Nipun Thapa (Computer Graphics)


(10, 7)
K Pk Xk+1 Yk+1 (Xk+1, Yk+1)
0 1 9 8 (9,8)
1 = 1 + 2x3 - 2x5 =-3 8 8 (8,8)
2 = -3 + 2x3 = 3 7 9 (7,9)
3 = 3 + 2x3 – 2x5 = -1 6 9 (6,9)
4 = -1 + 2x3 = 5 5 10 (5,10) 50
Example: Digitize line with endpoints (3, 10) and (6,2) using Bresenham's Line
https://genuinenotes.com
Drawing Algorithm. ( TU 2016)
Solution:
Here, (X1,Y1)= (6,2) & Δx = |3-6| = 3
(X2,Y2)=(3,10) Δy = |10-2| = 8
M= (10-2)/(3-6) = 8/-3 –ve slope and |M| > 1 here,
If Pk < 0 If Pk ≥ 0 Initial (P0) = 2 Δx – Δy
Xk+1 = Xk Xk+1 = Xk - 1 = 2x3-8
Yk+1 = Yk +1 Yk+1 = Yk +1 = -2
Pk = Pk + 2 Δx Pk = Pk+ 2Δx - 2 Δy

Nipun Thapa (Computer Graphics)


(6, 2)
K PK Xk+1 Yk+1 (Xk+1 ,Yk+1)
0 -2 6 3 (6,3)
1 =-2+2x3 = 4 5 4 (5,4)
2 =4+6-16=-6 5 5 (5,5)
3 =-6+6=0 4 7 (4,7)
4 =0+6-16=-10 4 8 (4,8) 51
5 =-10+6= -4 4 9 (4,9)
6 =-4 +6 =2 3 10 (3,10)
Example: Digitize the given line endpoints (15, 15) and (10, 18) using Bresenham’s
https://genuinenotes.com
line drawing algorithm.
Solution:
Here, (X1,Y1)= (15,15) & Δx = |10-15| = 5
(X2,Y2)= (10,18) Δy = |18-15| = 3
M= (18-15)/(10-15) = 3/-5 –ve slope and |M| < 1 here,
Initial (P0) = 2 Δy – Δx
If Pk < If Pk ≥ 0
Xk+1 = Xk - 1 = 2x3-5
Xk+1 = Xk - 1
Yk+1 = Yk Yk+1 = Yk +1 =1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx

Nipun Thapa (Computer Graphics)


(15, 15)
K Pk Xk+1 Yk+1 (Xk+1, Yk+1)
0 1 14 16 (14,16)
1 = 1 + 2x3 - 2x5 =-3 13 16 (13,16)
2 = -3 + 2x3 = 3 12 17 (12,17)
3 = 3 + 2x3 – 2x5 = -1 11 17 (11,17)
4 = -1 + 2x3 = 5 10 18 (10,18) 52
Example: Digitize the given line https://genuinenotes.com
endpoints (10, 10) and (20, 5) using Bresenham’s
line drawing algorithm.
Solution:
Here, (X1,Y1)= (20,5) & Δx = |10-20| = 10
(X2,Y2)= (10,10) Δy = |10-5| = 5
M= (10-5)/(10-20) = 5/-10 –ve slope and |M| < 1 here,
Initial (P0) = 2 Δy – Δx
If Pk < If Pk ≥ 0
Xk+1 = Xk - 1 = 2x5-10
Xk+1 = Xk - 1
Yk+1 = Yk Yk+1 = Yk +1 =0
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx

Nipun Thapa (Computer Graphics)


(20, 5)
K Pk Xk+1 Yk+1 (Xk+1, Yk+1)
0 0 19 6 (19,6)
1 = 0 + 2x5 - 2x10 =-10 18 6 (18,6)
2 = -10 + 2x5 = 0 17 7 (17,7)
3 = 0 + 2x5 - 2x10 =-10 16 7 (16,7)
4 = -10 + 2x5 = 0 15 8 (15,8)
5 = 0 + 2x5 - 2x10 =-10 14 8 (14,8)
6 = -10 + 2x5 = 0 13 9 (13,9)
7 = 0 + 2x5 - 2x10 =-10 12 9 (12,9)
53
8 = -10 + 2x5 = 0 11 10 (11,10)
9 = 0 + 2x5 - 2x10 =-10 10 10 (10,10)
Example: Digitize line with endpoints (5, 6) and (6,3) using Bresenham's Line
https://genuinenotes.com
Drawing Algorithm.
Solution:
Here, (X1,Y1)= (6,3) & Δx = |5-6| = 1
(X2,Y2)=(5,6) Δy = |6-3| = 3
M= (6-3)/(5-6) = 3/-1 –ve slope and |M| > 1 here,
If Pk < 0 If Pk ≥ 0 Initial (P0) = 2 Δx – Δy
Xk+1 = Xk Xk+1 = Xk - 1 = 2x1-3
Yk+1 = Yk +1 Yk+1 = Yk +1 = -1
Pk = Pk + 2 Δx Pk = Pk+ 2Δx - 2 Δy

Nipun Thapa (Computer Graphics)


(6, 3)
K PK Xk+1 Yk+1 (Xk+1 ,Yk+1)
0 -1 6 4 (6,4)
1 =-1+2x1 = 1 5 5 (5,5)
2 =1+2x1-2x3=-3 5 6
(5,6)
54
https://genuinenotes.com
BLA for slope +ve
|M| ≤ 1 |M| >1

if Pk < 0 If Pk ≥ 0 If Pk < 0 If Pk ≥ 0
Xk+1 = Xk+1 Xk+1 = Xk+1 Xk+1 = Xk Xk+1 = Xk+1
Yk+1 = Yk Yk+1 = Yk+1 Yk+1 = Yk+1 Yk+1 = Yk+1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx Pk = Pk + 2 Δx Pk = Pk + 2 Δx -2Δy

P0 = 2 Δy - Δx P0 = 2 Δx - Δy

Bresenham’s Line Algorithm

Nipun Thapa (Computer Graphics)


BLA for slope -ve
|M| ≤ 1 |M| >1

If Pk < 0 If Pk ≥ 0 If Pk < 0 If Pk ≥ 0
Xk+1 = Xk - 1 Xk+1 = Xk - 1 Xk+1 = Xk Xk+1 = Xk - 1
Yk+1 = Yk Yk+1 = Yk +1 Yk+1 = Yk +1 Yk+1 = Yk +1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx Pk = Pk + 2 Δx Pk = Pk+ 2Δx - 2 Δy 55

P0 = 2 Δy - Δx P0 = 2 Δx - Δy
Circle Algorithm
https://genuinenotes.com

What is Circle ?
• Similarly to the case with lines, there is an incremental
algorithm for drawing circles – the mid-point circle algorithm
• In the mid-point circle algorithm we use eight-way symmetry
so only ever calculate the points for the top right eighth of a
circle, and then use symmetry to get the rest of the points

Nipun Thapa (Computer Graphics)


56
https://genuinenotes.com Clockwise direction

Mid Point Circle Algorithm


(Xk,Yk) (Xk+1,Yk)

Pk<0

Pk≥0 (Xk+1,Yk-1/2)

Nipun Thapa (Computer Graphics)


(Xk+1,Yk-1)

Pk< 0 Pk ≥ 0
XK+1 = Xk+1 XK+1 = Xk+1
57
YK+1 = Yk YK+1 = Yk-1
https://genuinenotes.com

Mid Point Circle Algorithm

Nipun Thapa (Computer Graphics)


58
https://genuinenotes.com

Mid Point Circle Algorithm

Nipun Thapa (Computer Graphics)


59
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


60
https://genuinenotes.com

(Xk+1,Yk)
(Xk,Yk)
Pk<0
Case 1 : Pk < 0
Xk+1 = Xk +1
Yk+1 = Yk
Pk≥0 (Xk+1,Yk-1/2)
Pk+1 = Pk + 2(Xk+1) + 1
Pk+1 = Pk + 2Xk+1 + 1

Nipun Thapa (Computer Graphics)


Case 2: Pk ≥ 0 (Xk+1,Yk-1)
Xk+1 = Xk +1
Yk+1 = Yk - 1
Pk+1 = Pk + 2(xk+1) + [(yk – 1)2 - yk2)] - (yk - 1 - yk) +1
= Pk+2(xk+1) + [(yk 2 - 2yk +1 – yk 2)] - (yk-1- yk) + 1
= Pk+2(xk+1) - 2yk +1 +1 + 1 61
= Pk+2(xk+1) – 2(yk +1) +1
Pk+1 = Pk+2Xk+1 – 2Yk+1 +1
https://genuinenotes.com
Initial decision parameter
(1,r)
(0,r)
i.e.,
(x0,y0) = (0,r)
(1,r-1/2)
Here ,

Nipun Thapa (Computer Graphics)


Fcircle (1,r-𝟏/𝟐) = P0

Now, (1,r-1)
P0 = 12+(r-1/2 )2 – r2
= 1 + r 2- r + ¼ - r 2
= 5/4 – r
P0 ≈ 1-r
62
P0 ≈ 1-r
https://genuinenotes.com
Example : Digitize a circle with radius 10 at center (0,0)
Solution
Here, the initial decision parameter (P0) =1 – r = 1-10 = - 9
Since, for the Midpoint Circle Algorithm of initial point(0, r) &
center at origin (0, 0) rotating at clockwise direction, we have
Initial point (x0,y0) = (0,10)
P0=1-r = 1-10 = -9

Nipun Thapa (Computer Graphics)


63
https://genuinenotes.com
Case 1 : Pk < 0 Case 2: Pk ≥ 0
Xk+1 = Xk +1 Xk+1 = Xk +1
Yk+1 = Yk Yk+1 = Yk - 1
Pk+1 = Pk + 2Xk+1 + 1 Pk+1 = Pk+2Xk+1 – 2Yk+1 +1
Thus, (0 , 10)
K PK X k+1 Y k+1 ( X k+1, Y k+1)
0 -9 0+1 = 1 10 (1,10)

Nipun Thapa (Computer Graphics)


1 = -9 + 2*1 +1= -6 1+2 =2 10 (2, 10)
2 =-6 + 2*2 +1= -1 2+1=3 10 (3, 10)
3 =-1 + 2*3 +1= 6 3+1 = 4 10-1=9 (4, 9)
4 =6 + 2*4 - 2*9 +1= -3 4+1 = 5 9 (5, 9)
5 =-3 + 2*5 +1= 8 5+1 = 6 9-1=8 (6, 8)
6 =8 + 2*6 - 2*8 +1= 5 6+1 = 7 8-1=7 (7, 7)
64
7 =5 +2*7 – 2*7 +1 =6 7+1 = 8 7-1=6 (8,6)
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


65
https://genuinenotes.com

Example : Digitize a circle with radius 8

Nipun Thapa (Computer Graphics)


66
https://genuinenotes.com
Example: Digitize a circle with radius 9 and
center at (6, 7).
Here, the initial decision parameter (P0)
P0 =1 – r = 1-9 = - 8
Since, for the Midpoint Circle Algorithm of starting point (0, r) &
centre at origin (0, 0) rotating at clockwise direction, we have

Nipun Thapa (Computer Graphics)


If P < 0
Plot (xk +1, yk )
Pk+1 = Pk + 2xk+1 + 1
Else (P ≥ 0)
Plot (xk +1, yk - 1)
Pk+1 = Pk+2xk+1 – 2yk+1 +1
67
https://genuinenotes.com

k Pk Xk+1 Yk+ (Xk+1, Yk+1) (Xk+1, Yk+1)


1 At (0, 0) At (6, 7)

0. -8 0+1 = 1 9 (1, 9) (1+6, 9+7) = (7, 16)


1. = -8 + 2*1 +1= -5 1+1 = 2 9 (2, 9) (2+6, 9+7) = (8, 16)
2. = -5 + 2*2 +1= 0 2+1 = 3 8 (3, 8) (3+6, 8+7) = (9, 15)

Nipun Thapa (Computer Graphics)


3. = 0 + 2*3 - 2*8 +1=-9 3+1 = 4 8 (4, 8) (4+6, 8+7) = (10,15)

4. = -9 + 2*4 +1= 0 4+1 = 5 7 (5, 7) (5+6, 8+7) = (11,15)

5. = 0 + 2*5 - 2*7 +1=-3 5+1 = 6 7 (6, 7) (6+6, 7+7) = (12,14)


6. = -3 + 2*6 +1= 10 6+1 = 7 6 (7, 6) (7+6, 6+7) = (13,13)

68
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


69
https://genuinenotes.com Anti-Clockwise direction

Mid Point Circle Algorithm


(Xk-1,Yk) (Xk,Yk)

Pk<0

(Xk-1,Yk-1/2) Pk ≥ 0

Nipun Thapa (Computer Graphics)


(Xk-1,Yk-1)

Pk< 0 Pk ≥ 0
XK+1 = Xk-1 XK+1 = Xk-1
70
YK+1 = Yk YK+1 = Yk-1
Here, https://genuinenotes.com
Decision parameter(Pk) = Fcircle (Xk -1,Yk-1/2)
= (Xk -1)2 + (Yk-1/2)2 - r2
Then, K+1th term is,
Pk+1= (Xk+1 -1)2 + (Yk+1 - 1/2)2 - r2

Now,
Pk+1 – Pk = (Xk+1 -1)2 + (Yk+1 - 1/2)2 - r2 – {(Xk -1)2 + (Yk-1/2)2 - r2}
= -2(xk-1)+(Y2k+1-Y2k) – (Yk+1 – Yk) + 1

Nipun Thapa (Computer Graphics)


[ ‫ ؞‬Xk+1 =Xk-1 in both condition ]

Pk+1= Pk-2xk+1+(Y2k+1-Y2k) – (Yk+1 – Yk) + 1

Case 1 : Pk< 0 Case 2: Pk ≥ 0


XK+1 = Xk-1 XK+1 = Xk-1
YK+1 = Yk YK+1 = Yk-1 71
Pk+1= Pk-2Xk+1 +1 Pk+1= Pk-2Xk+1 – 2Yk+1 +1
https://genuinenotes.com
Initial decision parameter
(-1,r) (0,r)

i.e.,
(x0,y0) = (0,r)
(-1,r-1/2)
Here ,

Nipun Thapa (Computer Graphics)


Fcircle (-1,r-𝟏/𝟐) = P0

Now, (-1,r-1)
P0 = (-1)2+(r-1/2 )2 – r2
= 1 + r 2- r + ¼ - r 2
= 5/4 – r
P0 ≈ 1-r
72
P0 ≈ 1-r
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


Ellipse
73
Ellipse https://genuinenotes.com

Midpoint Ellipse Algorithm


Our approach here is similar to that used in displaying a
raster circle but the ellipse has 4-way symmetry. The midpoint
ellipse method is applied throughout the first quadrant in two

Nipun Thapa (Computer Graphics)


parts or region as shown in figure. The region-1 just behaves as
the circular property and the region-2 is slightly straight curve.
The equation of ellipse, whose centre at (0, 0)
is
x2/a2 + y2/b2 = 1

74
https://genuinenotes.com
Midpoint Ellipse Algorithm (cont..)

Starting at (0, b), we take unit steps in the x direction until we


reach the boundary between region 1 and region 2. Then we
switch to unit steps in the y direction over the remainder of the

Nipun Thapa (Computer Graphics)


curve in the first quadrant. At each step, we need to test the
value of the slope of the curve. The ellipse slope is calculated by
differentiating the ellipse function as:

2xb2 + 2ya2 * dy/dx= 0 Or dy/dx = - 2xb2 / 2ya2

At the boundary between region 1 and region 2, dy/dx = - 1 and


2xb2 = 2ya2. Therefore, we move out of region 1 whenever
2xb2>=2ya2.
75
https://genuinenotes.com

For Region – 1: Condition (2xb2 >= 2ya2)

Nipun Thapa (Computer Graphics)


76
https://genuinenotes.com

Midpoint Ellipse Algorithm (cont..)

Assuming that the position (xk, yk) has been plotted, we


determine next position (xk+1, yk+1) as either (xk+1, yk) or (xk+1, yk-1)
by evaluating the decision parameter P1k
as:

Nipun Thapa (Computer Graphics)


P1k = Fellipse (xk+1, yk-1/2)
= b2 (x k+1)2 + a2 (yk-1/2 )2 –a2 b2 --- ------------- I
At next sampling position, the decision parameter will be
P1k+1 = Fellipse (xk+1+1, yk+1-1/2)
= b2 (xk+1+1)2 + a2 (yk+1-1/2)2 –a2 b2
= b2 {(xk+1) +1}2 + a2 (yk +1-1/2) 2 – a2 b2
77
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


78
https://genuinenotes.com

Initial decision parameter (P10) for region-1 of ellipse

The initial decision parameter is obtained by evaluating the


ellipse function at the start position (x0, y0) = (0, b). Here, the
next pixel will either be (1, b) or (1, b-1) where the midpoint is
(1, b -1/2). Thus, the initial decision parameter is given by:

Nipun Thapa (Computer Graphics)


P10 = Fellipse (1, b-1/2) = b2 + a2 (b -1/2)2 – a2 b2
= b2 – a2 b2 + a 2 *1/4

Thus, P10 = b2 – a2 b2 + a 2 *1/4

79
https://genuinenotes.com

For Region – 2: Condition (2xb2 < 2ya2)

Nipun Thapa (Computer Graphics)


80
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


81
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


82
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


83
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


84
https://genuinenotes.com

Nipun Thapa (Computer Graphics)


85
https://genuinenotes.com

Nipun Thapa - Area Filling


Fill Area Algorithms
Unit 1

86
https://genuinenotes.com

Polygon Fill Algorithm


• Different types of Polygons
• Simple Convex
• Simple Concave
• Non-simple : self-intersecting

Nipun Thapa - Area Filling


• With holes

Convex Concave Self-intersecting


87
https://genuinenotes.com

Area Fill Algorithm


• An alternative approach for filling an area is to
start at a point inside the area and “paint” the
interior, point by point, out to the boundary.
• This is a particularly useful technique for filling

Nipun Thapa - Area Filling


areas with irregular borders, such as a design
created with a paint program.
• The algorithm makes the following assumptions
• one interior pixel is known, and
• pixels in boundary are known.
88
https://genuinenotes.com

Area Fill Algorithm


• If the boundary of some region is specified in a single color,
we can fill the interior of this region, pixel by pixel, until the
boundary color is encountered.
• This method, called the boundary-fill algorithm, is employed
in interactive painting packages, where interior points are

Nipun Thapa - Area Filling


easily selected.

89
https://genuinenotes.com

Example
• One can sketch a figure outline, and pick an interior point.
• The figure interior is then painted in the fill color as shown in
these Figures

Nipun Thapa - Area Filling


90
https://genuinenotes.com

Area Fill Algorithm


• Basically, a boundary-fill algorithm starts from an interior
point (x, y) and sets the neighboring points to the desired
color.

• This procedure continues until all pixels are processed up to

Nipun Thapa - Area Filling


the designated boundary for the area.

91
https://genuinenotes.com

Area Fill Algorithm


• There are two methods for processing neighboring pixels
from a current point.
1. Four neighboring points.
• These are the pixel positions that are right, left, above, and
below the current pixel.

Nipun Thapa - Area Filling


• Areas filled by this method are called 4-connected.

92
https://genuinenotes.com

Area Fill Algorithm


2. Eight neighboring points.
• This method is used to fill more complex figures.
• Here the set of neighboring points to be set includes the four
diagonal pixels, in addition to the four points in the first method.
• Fill methods using this approach are called 8-connected.

Nipun Thapa - Area Filling


93
https://genuinenotes.com

Area Fill Algorithm

Nipun Thapa - Area Filling


94
https://genuinenotes.com

Area Fill Algorithm


• Consider the Figure in the next slide.
• An 8-connected boundary-fill algorithm would correctly fill the
interior of the area defined in the Figure.

Nipun Thapa - Area Filling


• But a 4-connected boundary-fill algorithm would only fill part
of that region.

95
https://genuinenotes.com

Area Fill Algorithm

Nipun Thapa - Area Filling


96
https://genuinenotes.com

Area Fill Algorithm


• The following procedure illustrates a recursive
method for painting a 4-connected area with a
solid color, specified in parameter fillColor, up to
a boundary color specified with parameter

Nipun Thapa - Area Filling


borderColor.
• We can extend this procedure to fill an 8-
connected region by including four additional
statements to test the diagonal positions (x ± 1, y
± 1).
97
https://genuinenotes.com

Area Fill Algorithm

Nipun Thapa - Area Filling


98
https://genuinenotes.com

Area Fill Algorithm


• Some times we want to fill in (or recolor) an area that is not
defined within a single color boundary.
• Consider the following Figure.

Nipun Thapa - Area Filling


99
https://genuinenotes.com

Area Fill Algorithm


• We can paint such areas by replacing a specified interior color
instead of searching for a particular boundary color.

• This fill procedure is called a flood-fill algorithm.

Nipun Thapa - Area Filling


100
https://genuinenotes.com

Area Fill Algorithm


• We start from a specified interior point (x, y) and reassign all
pixel values that are currently set to a given interior color with
the desired fill color.
• If the area we want to paint has more than one interior color,
we can first reassign pixel values so that all interior points

Nipun Thapa - Area Filling


have the same color.

101
https://genuinenotes.com

Area Fill Algorithm


• Using either a 4-connected or 8-connected approach, we then
step through pixel positions until all interior points have been
repainted.

• The following procedure flood fills a 4-connected region

Nipun Thapa - Area Filling


recursively, starting from the input position.

102
https://genuinenotes.com

Area Fill Algorithm

Nipun Thapa - Area Filling


103
https://genuinenotes.com

Problems with Fill Algorithm (1)


• Recursive boundary-fill algorithms may not fill regions
correctly if some interior pixels are already displayed in the fill
color.

• This occurs because the algorithm checks next pixels both for

Nipun Thapa - Area Filling


boundary color and for fill color.

104
https://genuinenotes.com

Problems with Fill Algorithm


• To avoid this, we can first change the color of any interior
pixels that are initially set to the fill color before applying the
boundary-fill procedure.

• Encountering a pixel with the fill color can cause a recursive

Nipun Thapa - Area Filling


branch to terminate, leaving other interior pixels unfilled.

105
https://genuinenotes.com

Problems with Fill Algorithm (2)


• This procedure requires considerable stacking of neighboring
points, more efficient methods are generally employed.
• These methods fill horizontal pixel spans across scan lines,
instead of proceeding to 4-connected or 8-connected
neighboring points.

Nipun Thapa - Area Filling


106
https://genuinenotes.com

Problems with Fill Algorithm (2)


• Then we need only stack a beginning position for each
horizontal pixel span, instead of stacking all unprocessed
neighboring positions around the current position.
• Starting from the initial interior point with this method, we
first fill in the contiguous span of pixels on this starting scan

Nipun Thapa - Area Filling


line.

107
https://genuinenotes.com

Problems with Fill Algorithm (2)


• Then we locate and stack starting positions for spans on the
adjacent scan lines, where spans are defined as the
contiguous horizontal string of positions bounded by pixels
displayed in the border color.
• At each subsequent step, we retrieve the next start position

Nipun Thapa - Area Filling


from the top of the stack and repeat the process.

108
https://genuinenotes.com

Area Fill Algorithm


The algorithm can be summarized as follows:
1. define seed point,
2. fill scan line containing seed point,
3. for scan lines above and below, define new seed

Nipun Thapa - Area Filling


points as:
i) first point inside left boundary,
ii) subsequent points within boundary whose left neighbor is
outside,
4. d) repeat algorithm with the new set of seed points.

109
https://genuinenotes.com

Example
• In this example, we first process scan lines
successively from the start line to the top
boundary.
• After all upper scan lines are processed, we fill in

Nipun Thapa - Area Filling


the pixel spans on the remaining scan lines in
order down to the bottom boundary.
• The leftmost pixel position for each horizontal
span is located and stacked, in left to right order
across successive scan lines.
110
https://genuinenotes.com

Example
• In (a) of this figure, the initial span has been filled, and starting
positions 1 and 2 for spans on the next scan lines (below and
above) are stacked.

Nipun Thapa - Area Filling


111
https://genuinenotes.com

Example
• In Fig.(b), position 2 has been unstacked and processed to
produce the filled span shown, and the starting pixel (position
3) for the single span on the next scan line has been stacked.

Nipun Thapa - Area Filling


112
https://genuinenotes.com

Example
• After position 3 is processed, the filled spans and stacked
positions are as shown in Fig. (c).

Nipun Thapa - Area Filling


113
https://genuinenotes.com

Example
• And Fig.(d) shows the filled pixels after processing all spans in
the upper right of the specified area.

Nipun Thapa - Area Filling


114
https://genuinenotes.com

Example
• Position 5 is next processed, and spans are filled in the upper
left of the region; then position 4 is picked up to continue the
processing for the lower scan lines.

Nipun Thapa - Area Filling


115
https://genuinenotes.com

Example
•Finish up the upper scan lines.

Nipun Thapa - Area Filling


116
https://genuinenotes.com

Example
•Start the bottom scan lines.

Nipun Thapa - Area Filling


117
https://genuinenotes.com

Example
•Finish up the bottom scan lines.

Nipun Thapa - Area Filling


118
https://genuinenotes.com

Example
•Finish up the bottom scan lines.

Nipun Thapa - Area Filling


119
https://genuinenotes.com

Chapter 2

Nipun Thapa (Computer Graphics)


Finished
120

You might also like