Chapter 3
Chapter 3
Q. Digitized the line with end points (3,7) and (8,3) using DDA.
Given Starting Point :(x1, y1)= (3,7) and Ending Point: (x2,y2) = (8,3)
Now Slope m=(y2-y1)/(x2-x1)= (3-7)/(8-3) =(-4/5) = -0.8
Since |m|<1, From DDA algorithm we have
Xk+1 = Xk + 1
Yk+1 = Yk + m
Q. Consider a line from (0,0) to (5,5). Using simple DDA algorithm, rasterize this line.
Solution:
Given Starting Point :(x1, y1)= (0,0) and Ending Point: (x2,y2) = (5,5)
Now Slope m=(y2-y1)/(x2-x1)= (5-0)/(5-0) =(/55) = 1
Since |m|<=1, From DDA algorithm we have
Compiled by: Mohan Bhandari 4
Xk+1 = Xk + 1
Yk+1 = Yk + m
X Y X-Plot Y-Plot (X,Y)
0 0 0 0 (0,0)
1 1 1 1 (1,2)
2 2 2 2 (2,2)
3 3 3 3 (3,3)
4 4 4 4 (4,4)
5 5 5 5 (5,5)
Assume that (xk,yk) is pixel at kth step then next point to plot may
be either ( x k 1, y k ) or ( x k 1, y k 1)
A decision parameter pk for the kth step in the line algorithm can be obtained by substituting
y
m in above eqn and defining
x
p k x ( d 1 d 2 )
Δy
= Δx[2 ( x +1)−2 y k +2 b−1]
Δx k
=2 Δy . x k−2 Δx . y k +2 Δy+ Δx(2 b−1)
2y.x k 2x. y k c Where the constant c=2 Δy+ Δx (2 b−1 ) which is independent.
If decision parameter pk is negative i.e. d1 < d2, pixel at yk is closer to the line path than pixel at
yk+1 . In this case we plot lower pixel ( xk + 1 , yk) other wise plot upper pixel (xk+1,yk+1).
Q. If the starting point is (x0 , y0 ), then what will be the value of initial decision
parameter (P0) when |m| ≤ 1?
We have Pk = ∆x(d1 - d2)
Pk = ∆x (2m(xk +1) - 2yk + 2b -1)
Pk = ∆x (2mxk +2m + 2b - 2yk -1)
Pk = ∆x {2(mxk + b - yk )+2m -1)
The line y = mx + b must passes through the initial point (x 0, y0) and hence (mxk+b -
yk) = 0, we have
Thus, P0 = ∆x (2m -1)
P0 = ∆x (2 ∆y/∆x - 1)
P0 = 2∆y - ∆x, which is the initial decision parameter.
Q. Digitize the line with end points (20, 10) and (30, 18) using BLA.
Solution
Here, Starting point of line = (x1, y1) = (20, 10)
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,
|m| = 0.8 ≤ 1
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,
P k+1 = P k + 2∆y - 2∆x, and y k+1 = y k + 1 & x k+1 = x k +1
Else If Pk<0,
P k+1 = P k + 2∆y , and y k+1 = yk & x k+1 = xk +1
K Pk (Xk+1, Yk+1)
0 5 (21,16)
1 -5 (21,17)
2 15 (22,18)
3 5 (23,19)
4 -5 (23,20)
5 15 (24,21)
6 5 (25,22)
7 -5 (25,23)
8 15 (26,24)
9 5 (27,25)
10 -5 (27,26)
11 15 (28,27)
12 5 (29,28)
13 -5 (29,29)
14 15 (30,30)
Y
Y (-y,x) (y,x)
● ●
(-x,y) ●
(x,y)
●
(-x,y) ● ● (x,y)
● ● X
X (-x,-y) (x,-y)
● (x,-y)
●
● ●
(-x,-y)
(-y,-x) (y,-x)
8-way symmetry
4-way symmetry
(0,0)
ab )
2 2
If Pk < 0, the midpoint is inside the ellipse and the pixel on the scan line y k closer to the ellipse boundary.
Otherwise, the midpoint is outside or on the ellipse boundary, and we select the pixel on scan line y k -1.
Case: P1k < 0
x k+1 = x k +1
y k+1 = y k
P1k+1 = P1k + 2b 2(x k+1) + b 2
Case I: P1k >=0
x k+1 = x k +1
y k+1 = y k - 1
Compiled by: Mohan Bhandari 13
P k+1 = P k + 2b 2 (xk +1) + b 2 + a 2{((yk+1)2 – y k2) -(y k+1 – y k )}
= P k + 2b 2 (xk +1) + b 2 + a 2(( y k - 1)2 – y k2) -a 2( y k - 1 – y k )}
= P k +2b 2 (x k +1) + a2 [(y k 2 - 2y k +1 - y k2 )] – a 2 (y k -1- y k ) + b 2
= P k +2b 2 (x k +1) – 2a2 y k + a 2 + a 2 + b 2
= P k +2b 2 (x k +1) – 2a 2 (y k -1) + b 2
= P k +2b 2 x k+1 – 2a 2 y k+1 + b 2
Initial decision parameter (P10 ) for region-1 of ellipse
The initial decision parameter is obtained by evaluating the ellipse function at the start position (x 0,y0 )
=(0, b).
Here, the next pixel will either be (1, b) or (1, b-1) where the midpoint is (1, b- ½). Thus, the initial
decision parameter is given by:
P10 = F ellipse (1, b- ½)
= b 2 + a 2 (b- ½) 2 – a 2 b 2
= b 2 – a 2 b + a 2/4
For Region – 2: Condition (2xb2 >= 2ya2)
Assuming that the position (xk ,yk ) has been plotted, we determine next position (x k+1, yk+1 ) as either
(xk+1 , yk-1 ) or (xk, yk-1 ) by evaluating the decision
parameter P2 k as:
P2k = Fellipse (x k + ½ , y k -1)
= b2(x k + ½ )2 + a2(yk -1)2 – a2b2
At next sampling position, the decision parameter will be
P2 k+1 = F ellipse (xk+1 + ½ , y k+1 -1)
= b2(x k+1 + ½ )2 + a 2(y k+1 -1) 2 – a2b2
= {b2(xk+1 + ½ )2 + a2 (yk+1-1)2 - b (x
2
k + ½ )2 - a2(yk -1)2}
= b2(xk+1 + ½ )2 + a2 {(yk-1)-1}2 - b (x
2
k + ½ )2 - a2(yk -1)2
= b2xk+12+ b2xk+1 + b2/4 + a2(yk -1)2 - 2a2(yk -1) +a2 - b2xk2 – b2xk - b2/4 -a2(yk -1)2
= P2 k – 2a 2 (y k -1) + a 2
= P2 k – 2a 2 y k+1 + a 2
Initial decision parameter (P20) for region-2 of ellipse
When we enter region 2, the initial position (xo, y 0 ) is taken as the last position selected in region 1 and
the initial derision parameter in region 2 is given by:
P2 0 = F ellipse (x 0 +1/2, y 0 -1)
= b 2 (x0 +1/2) 2 + a 2 (y 0 -1) 2 –a 2 b 2
Q. Example: Given input ellipse parameters rx = a = 8 and r y = b = 6, we illustrate
the steps in the midpoint ellipse algorithm by determining raster positions along
the ellipse path in the first quadrant.
Solution:
Given a = 8 and b= 6
For Region 1
The initial point for the ellipse on the origin (xo, yo) = (0, b) = (0,6)
Calculation the initial decision parameter
P10 = b 2 – a 2 b + a 2/4
= 62 - 6 * (8)2+(82 / 4)
= -332
From midpoint algorithm, for Region 1 we know,
If(P1k<0) then
x k+1 = x k +1
y k+1 = y k
P1 k+1= P1k + 2b 2x k+1+ b 2
Else (i.e. P1k >=0)
x k+1 = x k +1
y k+1 = y k - 1
P1 k+1= P1 k +2b 2 x k+1 – 2a 2 y k+1 + b 2
And stop when 2xk+1b2 >= 2yk+1a2
Now successive decision parameter values and positions along the ellipse path are
calculated using midpoint method as
K P1k (Xk+1, YK+1) 2b 2(x k+1) 2a 2 y k+1
0 -332 (1, 6) 72 768
1 -224 (2, 6) 144 768
2 -44 (3, 6) 216 768
3 208 (4, 5) 288 640
4 -108 (5, 5) 360 640
5 288 (6, 4) 432 512
6 244 (7, 3) 540 384
Recursive boundary-fill algorithm not fill regions correctly if some interior pixels are already displayed in
the fill color. Encountering a pixel with the fill color can cause a recursive branch to terminate, leaving
other interior pixel unfilled. 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.