0% found this document useful (0 votes)
22 views117 pages

Computer Garphics

Uploaded by

Arun Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views117 pages

Computer Garphics

Uploaded by

Arun Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 117

Line Drawing Algorithm

Unit 2

Asistant Professor Loknath Regmi (CG)


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.)
• 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).

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


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
• Components-
• RGB Y Scan Line
• or
(cyan ,magenta,
• 4 components
yellow & black)
Pixel
0
0
Column number X

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
DDA Algorithm
🡪 Digital Differential Analyzer
Scan-conversion line – algorithm based on calculating
either

Sample the line at unit intervals in one coordinate


Determine the corresponding integer values nearest
the line path in another co-ordinate

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
DDA

Asistant Professor Loknath Regmi (CG)


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/Mk+1 k
YK+1 = Yk + 1

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)
Asistant Professor Loknath Regmi (CG)
Q.> Digitize a Line with end point A(2,3) and B(6,8) , using DDA

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

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

Asistant Professor Loknath Regmi (CG)


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) .
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). Asistant Professor Loknath Regmi (CG)
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 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.
Asistant Professor Loknath Regmi (CG)
BLA for slope +ve and |m| ≤ 1

+1 +1

+1 +1

+1 +1

Asistant Professor Loknath Regmi (CG)


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,
d1 = y - yk = m (xk +1) + c - yk
and
d2 = (yk +1)- y = yk+1– {m (xk +1) + c }

Asistant Professor Loknath Regmi (CG)


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,
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
=2 Δy Xk+2 Δy + 2 Δx c - 2 Δx yk – Δx
Asistant Professor Loknath Regmi (CG)
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 }

= 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

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


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

Pk+1 = Pk + 2 Δy
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
Asistant Professor Loknath Regmi (CG)
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


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

P0 = 2 Δy - Δx

Asistant Professor Loknath Regmi (CG)


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)
= 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

Asistant Professor Loknath Regmi (CG)


Thus,
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)

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


BLA for slope +ve and |m| > 1
(Xk, ) (Xk, )

( ,Yk) ( ,Yk)

+1 +1

+1 +1

+1 +1

Asistant Professor Loknath Regmi (CG)


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
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
Asistant Professor Loknath Regmi (CG)
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
Since, slope of line (m) = Δy / Δx,
we have
Pk = Δy (d1 - d2)
= 2Δx (yk+1) - 2c Δx - 2Δy xk - Δy
Asistant Professor Loknath Regmi (CG)
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} -
{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)


Asistant Professor Loknath Regmi (CG)
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
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk+1
Yk+1 = Yk+1

P = P + 2 Δx -2Δy
Asistant Professor Loknath Regmi (CG)
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

P0 = 2 Δx - Δy
Asistant Professor Loknath Regmi (CG)
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,
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 Asistant Professor Loknath Regmi (CG)
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

Asistant Professor Loknath Regmi (CG)


Example-5: Digitize A(5,2) and B(7,8) using BLA.

Asistant Professor Loknath Regmi (CG)


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.
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.

Asistant Professor Loknath Regmi (CG)


Slope –Ve and |M| ≤ 1
(Xk-1,Yk+1) (Xk-1,Yk+1)
(Xk,Yk+1)

d2
d2
Y
Y

d1
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

Asistant Professor Loknath Regmi (CG)


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
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
Here slope of line (m) = - Asistant
(Δy /Professor
Δx), {for
Loknath–ve
Regmislope)
(CG)
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
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(X k+1 - Xk) + 2Δx (-Yk+1 + Yk)


Asistant Professor Loknath Regmi (CG)
Pk+1 = Pk - 2Δy(Xk+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
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk - 1
Yk+1 = Yk +1

P = P + 2 Δy -2Δx
Asistant Professor Loknath Regmi (CG)
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

P0 = 2 Δy - Δx
Asistant Professor Loknath Regmi (CG)
Slope –Ve and |M| > 1
(Xk-1,Yk+1) (Xk,Yk+1)
(Xk,Yk+1) (Xk-1,Yk+1)

d1 d2 d1
d2

(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

Asistant Professor Loknath Regmi (CG)


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,
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
Here slope of line (m) = - (Δy / Δx), {for –ve slope)
Asistant Professor Loknath Regmi (CG)
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
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 )

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


Asistant Professor Loknath Regmi (CG)
Pk+1 = Pk +2 Δy (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
Case 2
If Pk ≥ 0 (i.e. d1-d2 is Positive )
then,
Xk+1 = Xk - 1
Yk+1 = Yk +1

P = P + 2Δx - 2 Δy Asistant Professor Loknath Regmi (CG)


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

P0 = 2 Δx - Δy
Asistant Professor Loknath Regmi (CG)
Example: Digitize the given line endpoints (10, 7) and (5, 10) using Bresenham’s
line drawing algorithm.(TU 2014)
Solution:
Here, (X1,Y1)= (10,7) & Δx = |5-10| = 5
Δy = |10-7| = 3
(X2,Y2)=(5,10)
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 +1 =1
Yk+1 = Yk
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx
(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)

Asistant Professor Loknath Regmi (CG)


Example: Digitize line with endpoints (6,2) and (3, 10) using Bresenham's Line
Drawing Algorithm. ( TU 2016)
Solution:
Here, (X1,Y1)= (6,2) & Δx = |3-6| = 3
Δy = |10-2| = 8
(X2,Y2)=(3,10)
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

(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)
5 =-10+6= -4 4 9 (4,9)
6 =-4 +6 =2 3 Professor Loknath
Asistant
(3,10)
10 Regmi (CG)
Example: Digitize the given line endpoints (15, 15) and (10, 18) using
Bresenham’s line drawing algorithm.
Solution:
Here, (X1,Y1)= (15,15) & Δx = |10-15| = 5
Δy = |18-15| = 3
(X2,Y2)= (10,18)
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 +1 =1
Yk+1 = Yk
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx
(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)

Asistant Professor Loknath Regmi (CG)


Example: Digitize the given line endpoints (10, 10) and (20, 5) using
Bresenham’s line drawing algorithm.
Solution:
Here, (X1,Y1)= (20,5) & Δx = |10-20| = 10
Δy = |10-5| = 5
(X2,Y2)= (10,10)
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 +1 =0
Yk+1 = Yk
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx
(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)
8 = -10 + 2x5 = 0 11 10 (11,10)
9 = 0 + 2x5 - 2x10 =-10 Asistant10Professor Loknath
10Regmi (CG) (10,10)
Example: Digitize line with endpoints (6, 3) and (5,6) using Bresenham's Line
Drawing Algorithm.
Solution:
Here, (X1,Y1)= (6,3) & Δx = |5-6| = 1
Δy = |6-3| = 3
(X2,Y2)=(5,6)
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

(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)

Asistant Professor Loknath Regmi (CG)


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

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
Asistant Professor Loknath Regmi (CG)
Circle Algorithm
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

Asistant Professor Loknath Regmi (CG)


Clockwise direction

Mid Point Circle Algorithm


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

Pk<0

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

(Xk+1,Yk-1)

Pk< 0 Pk ≥ 0
XK+1 = Xk+1 XK+1 = Xk+1
YK+1 = Yk YK+1 = Yk-1
Asistant Professor Loknath Regmi (CG)
Mid Point Circle Algorithm

Asistant Professor Loknath Regmi (CG)


Mid Point Circle Algorithm

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
(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

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
= Pk+2(xk+1) – 2(yk +1) +1
Asistant Professor Loknath Regmi (CG)
P = P +2X – 2Y +1
Initial decision parameter
(1,r)
(0,r)
i.e.,
(x0,y0) = (0,r)
(1,r-1/2)

Now, (1,r-1)
P0 = 12+(r-1/2 )2 – r2
= 1 + r2 - r + ¼ - r 2
= 5/4 – r
P0 ≈ 1-r
P0 ≈ 1-r
Asistant Professor Loknath Regmi (CG)
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

Asistant Professor Loknath Regmi (CG)


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

7 =5 +2*7 – 2*7 +1 =6 7+1 = 8 7-1=6 (8,6)

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
Example : Digitize a circle with radius 8

Asistant Professor Loknath Regmi (CG)


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
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
Asistant Professor Loknath Regmi (CG)
k Pk Xk+1 Y+1k (Xk+1, Yk+1) (Xk+1, Yk+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)

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)

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
Anti-Clockwise direction

Mid Point Circle Algorithm


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

Pk<0

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

(Xk-1,Yk-1)

Pk< 0 Pk ≥ 0
XK+1 = Xk-1 XK+1 = Xk-1
YK+1 = Yk YK+1 = Yk-1
Asistant Professor Loknath Regmi (CG)
Here,
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
[ ‫ ؞‬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
Pk+1= Pk-2Xk+1 +1 Pk+1= Pk-2Xk+1 – 2Yk+1 +1
Asistant Professor Loknath Regmi (CG)
Initial decision parameter
(-1,r) (0,r)

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

Now, (-1,r-1)
P0 = (-1)2+(r-1/2 )2 – r2
= 1 + r2 - r + ¼ - r 2
= 5/4 – r
P0 ≈ 1-r
P0 ≈ 1-r
Asistant Professor Loknath Regmi (CG)
Ellipse

Asistant Professor Loknath Regmi (CG)


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 parts or region as shown in figure. The
region-1 just behaves as the circular property and the region-2 is slightly
straight curve.

Asistant Professor Loknath Regmi (CG)


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 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.

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


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:
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

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
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:
P10 = Fellipse (1, b-1/2) = b2 + a2 (b -1/2)2 – a2 b2
= b2 – a2 b + a 2 *1/4

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

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
Asistant Professor Loknath Regmi (CG)
Fill Area Algorithms
Unit 1

Asistant Professor Loknath Regmi (CG)


Polygon Fill Algorithm
• Different types of Polygons
• Simple Convex
• Simple Concave
• Non-simple : self-intersecting
• With holes

Convex Concave Self-intersecting

Asistant Professor Loknath Regmi (CG)


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 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.

Asistant Professor Loknath Regmi (CG)


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 easily
selected.

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


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 the


designated boundary for the area.

Asistant Professor Loknath Regmi (CG)


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.
• Areas filled by this method are called 4-connected.

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


Area Fill Algorithm

Asistant Professor Loknath Regmi (CG)


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.

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


region.

Asistant Professor Loknath Regmi (CG)


Area Fill Algorithm

Asistant Professor Loknath Regmi (CG)


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 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).

Asistant Professor Loknath Regmi (CG)


Area Fill Algorithm

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


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 have the same
color.

Asistant Professor Loknath Regmi (CG)


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 recursively,


starting from the input position.

Asistant Professor Loknath Regmi (CG)


Area Fill Algorithm

Asistant Professor Loknath Regmi (CG)


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
boundary color and for fill color.

Asistant Professor Loknath Regmi (CG)


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 branch to
terminate, leaving other interior pixels unfilled.

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


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 line.

Asistant Professor Loknath Regmi (CG)


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 from the
top of the stack and repeat the process.

Asistant Professor Loknath Regmi (CG)


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 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.

Asistant Professor Loknath Regmi (CG)


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 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.

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


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

Asistant Professor Loknath Regmi (CG)


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.

Asistant Professor Loknath Regmi (CG)


Example
• Finish up the upper scan lines.

Asistant Professor Loknath Regmi (CG)


Example
• Start the bottom scan lines.

Asistant Professor Loknath Regmi (CG)


Example
• Finish up the bottom scan lines.

Asistant Professor Loknath Regmi (CG)


Example
• Finish up the bottom scan lines.

Asistant Professor Loknath Regmi (CG)


Chapter 2
Finished
Asistant Professor Loknath Regmi (CG)

You might also like