Unit 2 - Scan-Conversion of Graphics Primitives
Unit 2 - Scan-Conversion of Graphics Primitives
4.0 INTRODUCTION
The major objective of computer graphics is to model graphic objects in a scene.
Such objects comprise entity primitives like point, line, circle, ellipse and curves.
For displaying these entities on a raster display the representative sets of pixels of
the appropriate intensity or colour must be identified. There are standard scan
conversion algorithms to calculate first the position coordinates of the representative
pixels of each entity primitive from basic input data and then store pixel-wise
intensity information into the graphics memory. This unit discusses the different
approaches and issues for implementing line, circle and ellipse in digital space.
No stairstep appearance in
horizontal (AB), vertical
(AC), and diagonal (AD)
line.
Stairstep resterization
of line AB
(a)
Figure 4.2
Self-Instructional Material
where m represents the slope of the line and c is the y intercept. This slope can be expressed Scan Conversion Algorithms
as
yend ? ystart
m?
xend ? xstart NOTES
In fact any two consecutive points (xi, yi) and (xi+1, yi+1) lying on this line segment
yi ?1 ? yi
should satisfy the equation ?m
xi ?1 ? xi
Thus a unit change in x changes y by m which is a constant for a given line. We know
that if xi+1 = xi + 1, then yi+1 = yi + m; the values of x and y (on the line) are defined in terms
of their previous values. Initializing (xi, yi) with (xstart, ystart) the line can be generated by
incrementing the previous x values by one unit and solving the corresponding y value at
each step, till xend is reached. At each step, we make incremental calculations based on the
previous step. This is what is defined as incremental algorithm and often referred to as the
Digital Differential Analyzer (DDA) algorithm.
While incrementing the values of y by constant m, in this DDA method, we have to
keep in mind that m being the slope of the line, it can be any real number negative or
positive – so that the calculated y values must be rounded off to the nearest integer for the
sake of plotting on the screen.
But an obvious doubt at this point would be why not increment y values by 1 instead of
x and accordingly calculate x values from the lines equation. The determining factor here is
absolute value of the line’s slope, i.e., | m |.
If | m | ? 1 which implies | ?y | ? | ?x | we
sample the line at unit x intervals, as done above.
y i ?1 ? y i ? 1 y i ?1 ? y i ? 1 y i ?1 ? y i ? 1
|m| ? 1 1 1 1
xi ?1 ? xi ? xi ?1 ? xi ? xi ?1 ? xi ?
m m m
The Pseudocode for rasterizing a line according to DDA logic is presented below. This
code works for any line in all the 4 quadrants.
We assume that points are to be plotted on a bilevel intensity system and a function
Setpixel () is such that Setpixel (x, y, 1), will load the intensity value 1 into the frame buffer
at a position corresponding to column x along scanline y on the screen.
We also assume a function Round () which rounds off the argument to the nearest
integer.
Input = xstart, ystart, xend, yend
if abs (xend – xstart) ? abs (yend – ystart) : check whether | slope | ? 1 or > 1 , then
span = abs (xend – xstart) and accordingly set the sampling length
else span = abs (yend – ystart)
?x = (xend – xstart)/span : set the larger of ?x and ?y as one raster unit
or unit sampling interval
?y = (yend – ystart)/span : initialize x, y pixel coordinate with xstart,
x = xstart
ystart
y = ystart
i=1
Self-Instructional Material
while (i ? span) : continue loop till xend or yend is reached. Scan Conversion Algorithms
Setpixel (Round (x), Round (y), 1)
x ? x ? ?x
y ? y ? ?y : incremental calculation to select next pixel.
NOTES
i =i+1
end while
Our choices are clearly the pixels at (xk +1, yk +1) and (xk + 1, yk), i.e., A and B
respectively in Figure 4.5. Let C be the intersection point of the line L with the gridline x =
xk + 1. In Bresenham’s formulation, the difference between the vertical distances of A and
B to C is computed, and the sign of the difference is used to select the pixel whose distance
from C is smaller as the best approximation to the line.
Let the vertical distance of pixel B from the true line path, i.e., BC be denoted as d1 and
the vertical distance of pixel A from the true line path i.e. AC be denoted as d2 at sampling
position xk + 1.
Now as C is a point on the true line path at sampling position xk + 1 hence the coordinates
(x, y) of C are given by
Self-Instructional Material
Scan Conversion Algorithms x ? xk ? 1
? ( k ? 1) ? considering the equation of the line in the standard slope intercept
form, y = mx + c
Then,
NOTES d1 ? y ? yk
? m( xk ? 1) ? c ? yk
and,
d 2 ? ( y k ? 1) ? y
? y k ? 1 ? m ( xk ? 1) ? c
Since ?x ? 0 in this case, ?x ( d1 ? d 2 ) has the same sign as (d1 – d2). Therefore
?x ( d1 ? d 2 ) which involves only integer calculations can be used as a decision parameter
to decide the correct pixel, (xk+1, yk+1) next to (xk, yk).
Pk ?1 ? Pk ? 2?y ( xk ?1 ? xk ) ? 2?x ( yk ?1 ? yk )
? Pk ?1 ? Pk ? 2?y ? 2?x( yk ?1 ? yk ) [since xk ?1 ? xk ? 1]
Self-Instructional Material
of the decision parameter calculated in the previous iteration; then it updates the decision Scan Conversion Algorithms
parameter simply by incrementing the old value by a factor depending upon the choice of
pixel.
This method of obtaining values of successive decision parameters (Pk ) using
incremental integer calculation avoids direct computation of Pk from eqn. (4) involving NOTES
floating point operations.
Since the first pixel is simply the first endpoint (x0, y0) of the line we can directly
calculate the initial value of Pk i.e. P0 for fixing (x1, y1).
As the point (x0, y0) lies on the true line path, it satisfies the line equation y = mx + c.
? c ? y0 ? mx0
? ?y ?
? y 0 ? ? ? x0
? ?x ?
? 2c?x ? 2 y0 ?x ? 2 x0 ?y
We can summarize Bresenham’s line drawing algorithm for a line with positive slope
less than 1 in the following steps.
( x0 , y0 ) ? (0, 2)
?y ? 5 ? 2 ? 3
?x ? 4 ? 0 ? 4
?5?2? 3
Slope m ? ? ? ? ?1
?4?0? 4
Now calculate the successive decision parameters Pk and corresponding pixel positions
(xk+1, yk+1) closest to the line path as follows.
Self-Instructional Material
Scan Conversion Algorithms P0 ? 2?y ? ?x ? 2 ? 0
? x1 ? 1, y1 ? y0 ? 1 ? 3
? P1 ? P0 ? 2?y ? 2?x
NOTES ? 2 ? 2(3) ? 2( 4)
?0
? x2 ? 2, y2 ? y1 ? 1 ? 4
? P2 ? P1 ? 2?y ? 2?x
? 0 ? 2(3) ? 2( 4)
? ?2 ? 0
? x3 ? 3, y3 ? y 2 ? 4
? P3 ? P2 ? 2?y
? ?2 ? 2(3)
? 4?0
? x4 ? 4, y4 ? y3 ? 1 ? 5
We stop further calculation because the end point (4, 5) is reached.
The result is tabulated as follows:
coordinate of pixel to be
k Pk plotted
xk+1 yk+1
x0 = 0 y0 =2 start pixel
0 P0 = 2 x1 = 1 y1 = 3
1 P1 = 0 x2 = 2 y2 = 4
2 P 2 = –2 x3 = 3 y3 =4
3 P3 = 4 x4 = 4 y4 = 5 end pixel
Figure 4.6: The Shaded Pixels (x0, y0), (x1, y1), (x2, y2), (x3, y3), (x4, y4) – Representing the
Digitalised Line, Between (0, 2) & (4, 5)
NOTES
Figure 4.8: They Coordinate Decreases from start point to end point.
Taking into account all the above cases, a generalized version of Bresenham’s algorithm
is given below which will work for any line. The algorithm yields the same result even if
we reverse the processing direction (start point to end point) for a given line.
k
The sign ( ) function returns –1, 0, 1 as its argument is < 0, = 0, or > 0; sign ( k ) ?
abs( k )
x ? xs
: initialise variables
y ? ys
?x ? abs ( xe ? xs )
?y ? abs ( ye ? y s )
s1 ? sign ( xe ? xs )
s2 ? sign ( ye ? y s )
if ?y ? ?x then
temp ? ?x
?x ? ?y : swap ?x and ?y if absolute slope | m | ? 1
?y ? temp
swap ? 1
: set the flag
else swap ? 0
end if
n ?1
P ? 2?y ? ?x : initial value of decision parameter ( P0 )
Setpixel (x, y, 1) : intensify the starting pixel (xs, ys)
while (n ? ?x) : till the endpoint is reached
if P ? 0 then
x = x + s1
y = y + s2 : choose the next pixel by incrementing x and y by + 1 or –1
P ? P ? 2( ?y ? ?x) : update decision parameter
else : i.e., if P < 0
if swap = 1 then : i.e., | slope | > 1
Self-Instructional Material
Scan Conversion Algorithms y = y + s2 : choose the next pixel by incrementing y by + 1 or –1 while
keeping x same
else : i.e., if | slope | < 1
x = x + s1 : choose the next pixel by incrementing x by +1 or –1 while
NOTES keeping y same
end if : y by +1 or –1 while keeping x same
P ? P ? 2 ?y : update decision parameter
end if
Setpixel (x, y, 1) : set the next pixel
n=n+1
end while : while loop end
If you are thinking how we have generalized the decision parameter’s (Pk+1) expression,
here we present the derivations for enthusiastic readers.
s1 ? sign ( xe ? xs ) ? ?x ? abs ( xe ? xs ) ?
Considering ? and ?
s2 ? sign ( ye ? y s ) ? ?y ? abs ( ye ? y s ) ?
( y e ? y s ) ( s 2 ?y )
Slope m ? ?
( xe ? xs ) ( s1 ?x)
Let ?y ? ?x, then sampling is along ?x. Considering (xk, yk) as the pixel chosen already,,
we need to choose the next pixel (x k+1 , y k+1 ) between the two probables
( xk ? 1, yk ? 1), i.e., ( xk ? s1 , yk ? s2 ) and ( xk ? 1, y k ) i.e. ( xk ? s1 , yk ). Let us also
assume that the y coordinate of the point where the true line path intersects the gridline
xk ? 1 (i.e., xk ? s1 ) is y.
Figure 4.9
Self-Instructional Material
d1 Scan Conversion Algorithms
? m ( xk ? s1 ) ? c ? yk
s2
d2
? ( yk ? s 2 ) ? m( xk ? s1 ) ? c [since yk ?1 ? y k ? s2 ]
s2
( d1 ? d 2 ) NOTES
? ? 2mxk ? 2ms1 ? 2c ? 2 yk ? s2
s2
s 2 ?y s ?y
?2 xk ? 2 2 s1 ? 2c ? 2 y k ? s2
s1?x s1 ?x
s1
? ?x( d1 ? d 2 ) ? ( 2?y xk ) s2 ? 2( ? xyk ) s1 ? ( 2?y ) s1s2 ? ( 2c?x) s1 ? ( ?x) ? s1s2
s2
s22
? Pk ? ?x( d1 ? d 2 ) ? ( 2?y xk ) ? ( 2?x yk ) s2 ? ( 2?y) s 22 ? ( 2c?x) s2 ? ( ?x) s22
s1
2 ?y x k
? ? ( 2?x y k ) s2 ? ( 2?y ) ? ( 2c?x) s2 ? ?x
s1
2 ? y x k ?1
? Pk ?1 ? ? ( 2?x y k ?1 ) s2 ? ( 2?y ) ? ( 2c?x) ? ?x [since s22 ? 1]
s1
2?y
? Pk ?1 ? Pk ? ( xk ?1 ? xk ) ? 2?x s2 ( yk ?1 ? yk )
s1
For Pk ? 0 xk ?1 ? xk ? s1 ?
?
y k ?1 ? y k ? s 2 ?
s1
? Pk ?1 ? Pk ? ( 2?y ) ? ( 2?x)( s2 ) 2 ? Pk ? 2( ?y ? ?x)
s2
for Pk ? 0 xk ?1 ? xk ? s1 ?
?
y k ?1 ? y k ?
s1
? Pk ?1 ? Pk ? ( 2?y ) ? ( 2?x) s2 (0) ? Pk ? 2?y
s1
s 2 ?y
Putting m ? , y ? y s and x ? xs in y ? mx ? c
s1 ?x
s2
we get, (2c?x) ? (2?x y s ) ? (2?y xs )
s1
Now replacing this value of (2c ?x) in the expression of Pk and putting k = 0, we get the
initial value of decision parameter.
2?y x0 s2
P0 ? ? (2?x y0 ) s2 ? 2?y ? (2?x y s ) s2 ? (2?y xs ) 2 ? ?x
s1 s1
?since, x0 ? xs ? 2 ?
? 2 ?y ? ?x ? ? and s2 ? 1?
? y0 ? y s ? ?
In a similar manner, the algorithm can be proved for ?y ? ?x case where sampling is
done along ?y and P0 ? 2?x ? ?y
Self-Instructional Material
Scan Conversion Algorithms For Pk ? 0, xk ?1 ? xk ? s1 ?
?
y k ?1 ? y k ? s2 and ?
Pk ?1 ? Pk ? 2?x ? 2?y ??
NOTES For Pk ? 0, yk ?1 ? yk ? s 2 ?
?
xk ?1 ? xk and ?
Pk ?1 ? Pk ? 2?x ??
Example 4.2
xstart = 0 ystart = 0
Xend = –4 yend = –8
To find out using generalised Bresenham’s algorithm the pixel locations approximating
a line between the given points.
?x ? abs(?4 ? 0) ? 4
?y ? abs(?8 ? 0) ? 8
s1 ? sign (?4 ? 0) ? ?1, s2 ? sign (?8 ? 0) ? ?1
?y 8
since ?y ? ?x i.e. slope ? ? ? 2 ?1
?x 4
?y and ?x are interchanged i.e. ?x ? 8 and ?y ? 4
and flag swap ? 1
n ?1
P ? 2?y ? ?x
? 2 ? 4 ? 8 ? 0 [since swap ? 1]
Setpixel (0, 0,1)
since n ? ( ?x ? 8)
Since P ? 0
x ? x ? s1 ? 0 ? ( ?1) ? ?1
y ? y ? s2 ? 0 ? ( ?1) ? ?1
P ? P ? 2( ?y ? ?x)
? 0 ? 2( 4 ? 8) ? ?8
Setpixel ( ?1, ? 1, 1)
n ? 1?1 ? 2
Since n ? 8
since ( P ? ?8) ? 0 and swap ? 1 so
y ? y ? s2
? ?1 ? ( ?1) ? ?2
P ? P ? 2 ?y
? ?8 ? 2 ? 4 ? 0
Setpixel ( ?1, ? 2, 1) ? 0 [ x remains unchanged as ? 1 in previous iteration ]
n ? 2 ?1 ? 3
Since n ? 8
Since P ? 0
x ? x ? s1 ? ?1 ? 1 ? ?2
y ? y ? s2 ? ?2 ? 1 ? ?3
P ? P ? 2( ?y ? ?x)
? 0 ? 2( 4 ? 8) ? ?8
Self-Instructional Material
Setpixel (?2, ? 3, 1) Scan Conversion Algorithms
n ? 3 ?1 ? 4
Since n ? 8
Since ( P ? ?8) ? 0 and swap ? 1
y ? y ? s2 ? ?3 ? 1 ? ?4 NOTES
P ? P ? 2 ?y
? ?8 ? 2 ? 4 ? 0
Setpixel (?2, ? 4, 1) [ x remains unchanged as ? 2 in previous iteration ]
n ? 4 ?1 ? 5
Since n ? 8
Since P ? 0
x ? x ? s1 ? ?2 ? 1 ? ?3
y ? y ? s2 ? ?4 ? 1 ? ?5
P ? P ? 2( ?y ? ?x ) ? 0 ? 2( 4 ? 8) ? ?8
Setpixel (?3, ? 5, 1)
n ? n ?1 ? 5 ? 1 ? 6
Since n ? 8
Since ( P ? ?8) ? 0 and swap ? 1
y ? y ? s 2 ? ?5 ? 1 ? ?6
P ? P ? 2?y ? ?8 ? 2 ? 4 ? 0
Setpixel (?3, ? 6, 1) [ x ? ?3 unchanged ]
n ? n ?1 ? 6 ?1 ? 7
Since n ? 8
Since P ? 0 so
x ? x ? s1 ? ?3 ? 1 ? ?4
y ? y ? s2 ? ?6 ? 1 ? ?7
P ? P ? 2( ?y ? ?x) ? 0 ? 2( 4 ? 8) ? ?8
Setpixel (?4, ? 7, 1)
n ? n ?1 ? 7 ?1 ? 8
Since n ? 8
Since ( P ? ?8) ? 0 and swap ? 1
y ? y ? s 2 ? ?7 ? 1 ? ?8
P ? P ? 2?y ? ?8 ? 2 ? 4 ? 0
Setpixel (?4, ? 8, 1) [ x ? ?4 unchanged ]
n ? n ?1 ? 8 ?1 ? 9
Since n ? 8 hence no more iteration is done.
Self-Instructional Material
Scan Conversion Algorithms
y ? y c ? r 2 ? ( x ? xc ) 2 (5)
Figure 4.11: Computation of a Circle-point in shaded octant yields symmetric points to in other
seven octants; Point is the image of w.r.t. y = x, and are images of
abd respectively w.r.t. Y axis, , , , are images of , , , respectively w.r.t. X axis
xk ? r cos ? ; yk ? r sin ?
xk ?1 ? r cos (? ? ?? ); y k ?1 ? r sin (? ? ?? )
Here ?? is angular increment. From trigonometry we get Scan Conversion Algorithms
?? ? 1 / r
x ? 0?
? : the top quadrant point of the circle
y ? r?
while (y > x) : continue the loop until the first octant ends
at y = x
Setpixel (Round (xc + x), Round (yc + y), 1)* : generate a circle point in the 1st octant
Setpixel (Round (xc – x), Round (yc + y), 1)
Setpixel (Round (xc + x), Round (yc – y), 1)
Setpixel (Round (xc – x), Round (yc – y), 1) : generate seven other circle points from
Setpixel (Round (xc + y), Round (yc + x), 1) symmetry
Setpixel (Round (xc – y), Round (yc + x), 1)
Setpixel (Round (xc + y), Round (yc – x), 1)
Setpixel (Round (xc – y), Round (yc – x), 1)
x temp = x
x = xc – ys : calculate the next point in the first octant
y = yc + xtemps
endwhile
4.4.3 Bresenham’s Method
As with Bresenham’s line generation algorithm, the sign of a decision parameter is checked
for finding the closest pixel to the circumference of a circle at each sampling step in
Bresenham’s circle rasterizing algorithm.This algorithm is better than the previous two
algorithms in (Polynomial and Parametric method) because it avoids trigonometric and
square root calculation by adopting only integer operation involving squares of the pixel
separation distances. For a given radius R and screen centre position (xc, yc), we can first
Self-Instructional Material
Scan Conversion Algorithms
position (x, y) is moved to its proper screen position by adding xc to x and yc to y, as done in
Parametric method. Keeping in mind the advantage of generating a complete circle by only
computing points on a single octant, we consider the first octant of an origin centred
NOTES circle.Notice that, if the algorithm begins at x = 0, y = R, then for clockwise generation of the
circle y is a monotonically decreasing function of x in the first quadrant. For any known point
(xk, yk) on the circle, for clockwise generation of the circle there are only three possible candidate
pixel for selection as the next pixel which best represents the circle: (1) adjacent pixel
horizontally to the right –H (xk+1, yk), (2) adjacent pixel diagonally downward to the right –D
(xk + 1, yk – 1) and (3) adjacent pixel vertically downward –V (xk, yk–1). Out of these three pixels
the algorithm chooses the one for which the distance from the true circle is minimum.
Now consider the pixel P w.r.t the circular arc shown in Figure 4.13.
2 2
Let dP denote the quantity |OP2 – OX2|. In this case d P ? | OP ? OX |
? x2 ? y2 ? R2
Similarly if we introduce the terms dH, dD and dV
corresponding to pixels H, D and V respectively then we
can write.
d H ? | OH 2 ? R 2 | ? | ( xk ? 1) 2 ? ( yk ) 2 ? R 2 |
d D ? | OD 2 ? R 2 | ? | ( xk ? 1) 2 ? ( yk ? 1) 2 ? R 2 |
dV ? | OV 2 ? R 2 | ? | ( xk ) 2 ? ( yk ? 1) 2 ? R 2 |
Figure 4.13: Bresenham’s Method
Though the (xk+1, yk+1) pixel has to be chosen among (xk+1, yk) , (xk + 1, yk–1) and (xk,
yk–1), there are five possible cases to be considered regarding position of the true circle
path in the vicinity of the point (x k , y k ) as
illustrated in Figure 4.14.
It is clear from Figure 4.14 that
For Case (1) & (2) the diagonal pixel D is
inside the circle, implying
OD2 < R2, i.e., OD2 – R2 < 0
For Case (3) & (4) the diagonal pixel D is
outside the circle, implying Figure 4.14
OD2 > R2, i.e., OD2 – R2 > 0
For Case (5) the diagonal pixel D is on the circle, implying
OD2 = R2, i.e., OD2 – R2 = 0
Let us now study the five cases separately while referring to the Figure 4.14
Self-Instructional Material
Case 1: To choose between H(xk + 1, yk) and D(xk + 1, yk–1) Scan Conversion Algorithms
Let ? HD ? d H ? d D
? ? HD ? | OH 2 ? R 2 | ? | OD 2 ? R 2 |
[since, OD 2 ? R 2 OH 2 ? R 2 as D is inside NOTES
? (OH 2 ? R 2 ) ? ( R 2 ? OD 2 )
the circle while H is outside the circle]
? OH 2 ? OD 2 ? 2 R 2
? {( xk ?1 ? 1) 2 ? ( y k ) 2 } ? {( xk ? 1) 2 ? ( yk ? 1) 2 } ? 2 R 2
? 2( xk ? 1) 2 ? 2( yk ? 1) 2 ? 2 R 2 ? 2 yk ? 1
? 2{( xk ? 1) 2 ? ( yk ? 1) 2 ? R 2 } ? 2 yk ? 1
? 2?Dk ? 2 yk ? 1
where ?Dk ? OD 2 ? R 2 ? ( xk ? 1) 2 ? ( yk ? 1) 2 ? R 2
If ? HD ? 0 then dH < dD which implies the horizontal pixel is closer to the actual circle
than the diagonal pixel. So choose H(xk + 1, yk)
? 2 R 2 ? 2( xk ? 1) 2 ? 2( yk ? 1) 2 ? 2 xk ? 1
? 2 xk ? 1 ? 2 {( xk ? 1) 2 ? ( yk ? 1) 2 ? R 2 }
? 2 xk ? 2?Dk ? 1 [since, ?Dk ? ( xk ? 1) 2 ? ( yk ? 1) 2 ? R 2 ]
Self-Instructional Material
Scan Conversion Algorithms Case 5: This is the case when the diagonal pixel D lies on the actual circle. So the
obvious choice is D(xk+1, yk–1).
Also note that for this case,
We will call ?D as the decision parameter because by checking the sign (<0, >0,
or, = 0) of ?D ? OD 2 ? R 2 at each step, we can determine the cases(s) it corresponds to and
proceed accordingly.
We can update the value of decision parameter ?D at every step, following the
incremental approach, i.e. by adding a factor (depending on the choice of pixel) to the old
value.
while (y > x) : continue till the diagonal axis in the first quadrant is
reached
Setpixel ((xc + x), (yc + y), 1) : plot the computed pixel in the 1st octant
Setpixel ((xc – x), (yc + y), 1)
Self-Instructional Material
Setpixel ((xc + x), (yc – y), 1) Scan Conversion Algorithms
Setpixel ((xc – x), (yc – y), 1)
Setpixel ((xc + y), (yc + x), 1) : plot the symmetric pixels in the other octants
Setpixel ((xc – y), (yc + x), 1)
NOTES
Setpixel ((xc + y), (yc – x), 1)
Setpixel ((xc – y), (yc – x), 1)
if ?D ? 0 then : OD2 < R2
? ? 2?D ? 2 y ? 1 : ? ? ? HD
if ? ? 0 then
x=x+1 : choose horizontal pixel and update ?D accordingly
?D ? ?D ? 2 x ? 1
else
x=x+1
y=y–1 : choose diagonal pixel and update ?D accordingly for
?D ? ?D ? 2 x ? 2 y ? 2 ? HD ? 0
endif
elseif ?D > 0 then : OD2 > R2
? ? 2 x ? 2?D ? 1 : ? ? ? VD
if ? < 0 then
y=y–1 : choose vertical pixel and update ?D accordingly
?D ? ?D ? 2 y ? 1
else
x=x+1 : choose diagonal pixel and update ?D accordingly
y=y–1 for ? VD ? 0
?D ? ?D ? 2 x ? 2 y ? 2
endif
else
x=x+1
y=y–1 : choose diagonal pixel and update ?D accordingly
?D ? ?D ? 2 x ? 2 y ? 2 for ?D = 0
endif
endwhile
Example 4.3 To find out (using Bresenham’s algorithm) the pixel location approximating
the first octant of a circle having centre at (4, 5) and radius 4.
xc ? 4, yc ? 5, R ? 4
x?0
y? R?4
?D ? 2(1 ? R ) ? 2(1 ? 4) ? ?6
Self-Instructional Material
Scan Conversion Algorithms Iteration 1 Since (y = 4) > (x = 0)
Setpixel (( xc ? x), ( yc ? y ),1) ? Setpixel ( 4, ? 9,1)
Since ( ?D ? ?6) ? 0
x2 y2
? ?1 where 2a = length of major axis
a2 b2
Self-Instructional Material
2b = length of minor axis Scan Conversion Algorithms
? b2 x2 + a2 y2 – a2 b2 = 0
From coordinate geometry, we get
NOTES
? ? 0 implies ( x, y ) inside the ellipse
2 2 2 2 2 2?
f ( x, y ) ? b x ? a y ? a b ? ? 0 implies ( x, y ) on the ellipse
?? 0 implies ( x, y ) outside the ellipse
?
Now an ellipse can be divided equally into four parts. So if one part (or quadrant) can
be generated then the other three parts can easily be replicated by mirroring the original
part (4-way symmetry).
Let us generate the 1st quadrant of the ellipse. For applying the Midpoint method the
1st quadrant is logically divided into two regions –
Region 1 – arc closer to the Y axis with absolute slope less than 1.
Region 2 – arc closer to the X axis with absolute slope greater than 1.
Figure 4.16
Provided we know coordinates (xi, yi) of the pixel that lies exactly on the ellipse on the
first quadrant we need to find out the next nearest pixel (xi+1, yi+1) using incremental integer
value of the decision parameter f(x, y). As in the case of rasterizing a line we changed the
unit sampling direction (X or Y) according to the slope here also we take the slope factor in
mind. Starting at (0, b) and moving clockwise along the ellipse-path in Region 1 we take
unit steps in X direction until we reach the boundary between Region 1 and 2. Then we
switch to unit steps in Y direction while sampling Region 2 of the curve.
The partial derivative of f(x, y) w.r.t x and that w.r.t y being fx = 2b2 x and fy = 2a2 y
respectively. The slope of the ellipse at any point (x, y) is given by
dy f b2 x
?? x ?? 2
dx fy a y
Self-Instructional Material
Scan Conversion Algorithms
NOTES
Figure 4.17
While deciding whether to plot (xi + 1, yi) or (xi +1, yi –1) as the (i +1)th pixel, the
choice is easily made by checking whether the halfway point (midpoint) between the centers
of the two candidate pixels lies inside or outside the theoretical ellipse-path.
The checking is done by evaluating f(xi +1, yi –1/2) and checking the sign.
2
? 1? ? 1?
Let, Pi ? f ? xi ? 1, yi ? ? ? b2 ( xi ? 1) 2 ? a 2 ? yi ? ? ? a 2 b2
? 2? ? 2?
2
? 1? ? 1?
Similarly, Pi ?1 ? f ? xi ?1 ? 1, yi ?1 ? ? ? b2 ( xi ?1 ? 1)2 ? a 2 ? yi ?1 ? ? ? a 2 b2
? 2 ? ? 2?
?? 2
1? ? 1? ?
2
? Pi ?1 ? Pi ? b 2 [( xi ?1 ? 1) 2 ? ( xi ? 1) 2 ] ? a 2 ?? yi ?1 ? ? ? ? yi ? ? ?
??? 2? ? 2 ? ??
?? 2
1? ? 1? ?
2
? Pi ?1 ? Pi ? b 2 [( xi ?1 ? 1) 2 ? ( xi ? 1) 2 ] ? a 2 ?? yi ?1 ? ? ? ? yi ? ? ?
??? 2? ? 2 ? ??
Self-Instructional Material
2 Scan Conversion Algorithms
Let
? 1
2
? ? 1?
?
Pj ? f ? x j ? , y j ? ? b 2 ? x j ? ? ? a 2 y j ? 1 2 ? a 2 b 2
2?
?
? ? ?
2
Similarly,
? 1
2
? ? 1?
? ?
Pj ?1 ? f ? x j ?1 ? , y j ?1 ? 1? ? b 2 ? x j ?1 ? ? ? a 2 y j ?1 ? 1 2 ? a 2 b 2
2?
? ? ? NOTES
?? ??
2 2?
?? 1? ?
2
1?
? Pj ?1 ? Pj ? b 2 ?? x j ?1 ? ? ? ? x j ? ? ? ? a 2 y j ?1 ? 1 2 ? y j ? 1 2
2 ? ??
? ?
?? ? ?
?? ?
2 2?
? P j ?1 2 ?? 1? ? 1?
? P j ? b ?? x j ?1 ? ? ? ? x j ? ?
2? ? 2?
?
? ? a 2 y j ?1 ? 1 ? ? yi ? 1?
2 2
?? ??
end if
if (P ? 0) then : for P ? 0, y = y – 1, but no change in x
2
P = P + a – fy : for P ? 0, P = P + a2 – 2a2 (y – 1)
else
P = P + a2 – fy + fx : for P < 0, P = P + a2 – 2a2 (y – 1) + 2b2 (x + 1)
end if
Setpixel ((xc + x), (yc + y), 1) : plot the 4 symmetric pixels corresponding to
Setpixel ((xc – x), (yc + y), 1) : each sampled pixel in Region 2
Setpixel ((xc + x), (yc – y), 1)
Setpixel ((xc – x), (yc – y), 1)
end while : end of generating Region 1 and three symmetric
regions
We have discussed one of the mostly used algorithm for generating ellipse. Although
there are other two basic methods for generating ellipse.
1. Algebraic method
2. Trigonometric method.
These are simple and briefly discussed below.
( x ? xc ) 2 ( y ? yc )
? ?1
a2 b2
From the above equation the solution for y for corresponding value of x can be obtained
as,
( x ? xc ) 2
y ? yc ? b 1 ?
a2
Self-Instructional Material
4.5.4 Trigonometric Method Scan Conversion Algorithms
Algorithm
1. Input xc, yc, a, b
2. Plot the first pixel at the rightmost coordinate where x = xc + a, y = yc; (? = 0)
2. Initialize step size ?? = .004 to create dense pixels
4. Calculate ? ? ? ? ??
5. Calculate x = xc + a cos?, y = yc + b sin?
6. Plot four symmetric pixels Setpixel (Round (xc + x), Round (yc + y), 1)
Setpixel (Round (xc – x), Round (yc + y), 1)
4.6 ANTIALIASING
If you try to implement the various scan conversion algorithms discussed so far in a computer
you will soon discover that sometimes same pixel is unnecessarily set to same intensity
multiple times; sometimes some objects appear dimmer or brighter though all are supposed
to have same intensity; and most of the objects generated are not smooth and appear to
have rough edges. All these are standard side effects of scan conversion: the first type is
called over striking effect, the second is unequal intensity effect while the third type which
is the most common and most pronounced, known as aliasing effect.
In fact aliasing is a typical image quality problem on all pixel devices, including a
computer screen. Aliasing is the stair-step effect or jaggies on the edges of objects displayed
on computer screens – all diagonal and curved lines are displayed as a series of little zigzag
horizontal and vertical lines and can be extremely distracting for PC users.
Notice how the letters have jagged edges If we zoom in on the letter A, it is easy
(except i, 1 and the horizontal portion of letter A which to see what is happening.
are either perfectly horizontal or vertical).
Self-Instructional Material
Scan Conversion Algorithms
NOTES
Figure 4.19: Aliasing is prominent in the entity primitives
drawn –inclined line, circle & ellipse.
Among the other effects of aliasing, jagged profiles and disintegrating textures are
very common.
Figrue 4.20: The picture on the left shows the sampling grid superimposed on the original scene.
The picture on the right is the rendered image. A jagged profile is quite evident in the rendered image.
As most of the pixels spanning the object’s boundaries end up being shaded incorrectly
as either wholly within the object or not at all within the object.
Figure 4.21: This is a checkered texture on a plane. You can see how the checkers become
disintegrated or irregularly shaped due to aliasing when their distance from the viewer increases.
These jaggies are essentially caused by the problem of trying to map a continuous
image onto a discrete grid of pixels. This continuous-to-discrete transformation (known as
scan conversion) is performed by sampling the continuous line, curve etc. at discrete points
(integer pixel positions) only followed by generating image pixels at integer locations that
only approximate the true location of the sampled points. Pixels so generated at alias locations
constitute aliases of the true objects or object edges. Therefore we can say aliasing occurs
as a result of an insufficient sampling rate and approximation error (or more specifically
quantization error).
In fact aliasing is a potential problem whenever an analog signal is point sampled to
convert it into a digital signal. It can occur in audio sampling, for example, in converting
music to digital forms to be stored on a CD-ROM or other digital devices. Whenever an
analog audio signal is not sampled at a high enough frequency*, aliasing manifests itself in
the form of spurious low frequencies.
Self-Instructional Material
Scan Conversion Algorithms
. NOTES
.
.
.
Figure 4.22
Now coming to Antialiasing, quite obviously it implies the techniques used to diminish
the jagged edges of an image so that the image appears to have smoother lines.
As aliasing problem is due to low sampling rate for low resolution, one easy solution is
to increase the resolution, causing sample points to occur more frequently. This in turn will
reduce the size of the pixels. The size of the ‘jaggy’ or stair-step error is never larger than
the size of the actual pixel. Hence, reducing the size of the pixel reduces the size of these
steps. But the cost of image production becomes higher with increased resolution as it calls
for more graphics memory. The computational overhead increases for maintaining a high
frame rate (60 fps) for bigger frame buffer. Thus, within the present limitations of hardware
technology increased screen resolution is not a feasible solution.
There are quite a few standard methods for antialiasing. What antialiasing basically
does is change the pixels around the edges to intermediate colours or grayscales. This has
the effect of making the edges look smoother although it also makes them fuzzier.
Figure 4.23(a): Compare this text with the aliased text shown earlier.
Can you see the jaggies any more? No, because this is an antialiased text.
Figure 4.23(b): If we zoom on the letters we see that grey shades are used to
reduce the contrast of the black pixels at the edges with the white background.
This is the reason why the text appears smooth though the jaggies are still there.
Self-Instructional Material
Scan Conversion Algorithms after combining (averaging) the results of the subpixels. The intensity value of a pixel is the
average of the intensity values of all the sampled subpixels within that pixel. The following
illustration describes the concept of supersampling.
NOTES
(a) The theoretical line to be plotted (b) Using Bresenham’s algo pixels are
on the actual pixel grid mapped – the line is displayed with aliases.
(c) For supersampling each square pixel is logically (d) The virtual image of the line is calculated
divided into 9 equal sized square subpixels. using same Bresenham’s algo but with a smaller
sampling interval (higher sampling frequency).
(e) The final antialiased image displayed on the actual pixel-
7 grid after averaging the subpixel intensity values of the virtual
image. Look three shades have been used to fill the pixels. As
from Fig. 3.24(d) there are 3 sampling positions within a pixel
5 6
box. In each of the boxes 1, 6 & 7 the three sampled subpixel
colour value is black, hence the average colour of these three
3 4 pixels is, (black +black +black)/3 = black. There are only
two sampled subpixel in pixel 3 & 4 that falls on the line
1 2 path; so the average colour of 3 & 4 is, (black + black +
white)/3 = blackish grey these three pixels is, Similarly the
colour of pixel 2 & 5 is (1*black + 2*white)/3 = whitish grey.
Figure 4.24
This method is also known as postfiltering because filtering is carried out after sampling.
Filtering means eliminating the high frequencies, i.e., combining the supersamples to compute
a pixel colour. This type of filtering is known as unweighted filtering because each supersample
in a pixel, irrespective of its position, has equal influence in determining the pixel’s colour. In
other words, an unweighted filter computes an unweighted average. The other type of filter is
a weighted filter. Through this filter each supersample is multiplied by its corresponding
weight and the products are summed to produce a weighted average, which is used as the
pixel colour. The weighting given to each sample should depend in some way on its distance
from the centre of the pixel. The centre sample within a pixel has maximum weight. An array
of values specifying the relative importance (weights) of subpixels can be set for different-
sized grids and is often referred to as Pixel-Weighting Masks.
The other standard method of antialiasing is Area Sampling or Prefiltering. Prefiltering
method treats a pixel as an area, and computes pixel colour based on the overlap of the
Self-Instructional Material
scene’s objects with a pixel’s area. These techniques compute the shades of grey based on Scan Conversion Algorithms
how much of a pixel’s area is covered by an object.
For example, a modification to Bresenham’s algorithm was developed by Pitteway and
Watkinson. In this algorithm, each pixel is given an intensity depending on the area of
overlap of the pixel and the line. NOTES
4.8 SUMMARY
Scan converting entity primitives are the basic building blocks for developing a CAD package
or for programmatically generating application graphics on a raster display. In this unit we
have seen how stress is on minimizing floating point operations and why only incremental
scan conversion algorithms are used. Moreover minimizing the error between the chosen
pixels on a raster and the points on the ideal primitive defined on a Cartesian plane is a key
factor in any graphics algorithm. Newer scan conversion algorithm evolves in pursuit of
ideal optimization between ‘correctness’ and ‘speed’. Only the basics have been covered
here–many elaborations and special cases can be considered for robust implementation.
The basic algorithms can be extended to handle thickness, as well as patterns for entity
primitives and also real-time antialiasing.
A(x1,y1)
8. Compare the advantages and disadvantages of the Bresenham’s line drawing algorithm
with those of the DDA algorithm.
9. Modify Bresenham’s line generation algorithm so that it will produce a dashed line:
the dash length should be independent of slope.
10. A line drawing algorithm draws a line by computing mid point pixel for a given pair of
known end point pixels. Would you recommend it for raster graphics in preference to
Bresenham’s algorithm? You may record your observations based on hardware, software
and visual considerations.
11. A line will be drawn from (x1, y1) to (x2, y2). Scan conversions are started from both (x1,
y1) to (x2, y2) and also from (x2, y2) to (x1, y1) simultaneously following Bresenham’s
algorithm.
(i) Write algorithm steps for such implementation.
(ii) What is the advantage of this technique? Why?
12. Contrast the differences in both appearance and computational costs resulting from
the use of unweighted and weighted antialiasing.
13. How can the Bresenham’s line drawing algorithm be modified so that the antialiasing
effects are produced during straight line generation.
14. It is desired that the circle with centre at the origin and radius 8 in the first quadrant is
to be drawn. Using Bresenham circle generation algorithm determine the pixels which
would approximate the desired portion of the circle.
15. When 8-way symmetry is used to obtain a full circle from pixel coordinates generated
for the 0º to 45º octant some pixels are set or plotted twice. This phenomenon is
sometimes referred to as overstrike. Identify where overstrike occurs.