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

Computer Graphics Chapter 4

Uploaded by

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

Computer Graphics Chapter 4

Uploaded by

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

CHAPTER 4

Scan Conversion of Solids

In the earlier chapter, we have described the process of scan converting line segments
or standard geometric curves. However, these curves can constitute only the outline
or boundary of an object. Generally, each object in a scene have a certain colour or
intensity of its surfaces. So, to produce visual realism, we must fill up any closed area
in a scene with appropriate colour or intensity. The process of scan converting closed
object surfaces is called solid area scan conversion or simply flling.

4.1 Solid Areas or Polygons


In computer graphics, 'solid area' means closed object surfaces. Since any such closed
area can be approximately represented by a plane polygon, the term 'solid area' is
referred to as plane polygons also.
Apolygon may be represented as a number of line segments connected end-to-end
to form a closed shape. Alternatively, it may be represented as the set of points where
the sides of a polygon are joined. Therefore, mathematically, a polygon is a either a
set of vertices or a set of edges.
Creating a solid area or filled area is always advantageous in a raster system,
because it can easily store and display the filled area with colour or shades. Fill
patterns or colours or intensities are stored in the frame buffer. In a vector system
it is considerably difficult, as area filling requires drawing a suficient number of line
segments within the area boundary during each refresh cycle.
Polygons may be convex or concave. f any line joining two interior points of
a polygon lies completely inside the polygon, the polygon is conver, otherwise it is
concave (Figure 4.1). By the scan conversion process, each pixel inside the polygon
must be painted by a colour or intensity. Hence, we need an inside-outside test method.

4.2 Inside-Outside Test


To scan convert a polygon, i.e. to paint a closed area with a certain intensity value, we
need to differentiate between the interior and the exterior ofa polygon. There are two

63
(64
Scan Conversion of Solide

(a) (b)

Figure 4.1: Polygons: (a) convex and (b) concave.


different approaches to determine whether a point is inside or outside a given polygon
These methods are called odd even method and winding number method, which are
described below.

4.2.1 Odd-Even Method


This is a very simple method and can be applied to both convex and
concave polygons.
The steps of the method are as follows:
Step 1. Select an outside point on the horizontal line
containing the point in question.
Step 2. Draw a horizontal straight line from the outside point to the point in
question.
Step 3. Count the number of intersections of the straight line with the
aries.
polygon bound
Step 4. If the count is odd, the point in question is an inside point, otherwise it is
an outside point.
The above nethod is illustrated in Figure 4.2. Here, white circles
the points to be tested, and the black circles (dots) represent
(dots) represent
intersections with polygon
boundaries.

Figure 4.2: Odd-even method: counting intersections.


42 lnside Outside Test 65

Although the algorithm, as it is presented here, looks very simple, it may produce
incorreet results in certain situations. Let us consider a situation when the straight
line passes through a polygon vertex. Two different situations may arise:
1. The polygon boundaries that meet at the vertex Come from the same side of the
straigit line. In this case, the vertex may be considered to be a local minima or
maxima point.
2. The polygon boundaries that meet at the vertex come from two different sides
of the straight line.
In the former situation, we need to consider the occurrence of two intersections at
the vertex (even-count). In the latter, we have to consider the presence of a single
intersection at the vertex (odd-count). To determine whether a vertex is a local minima
or maxima, we can examine the other end points of the two boundary lines meeting
at the vertex. If the y-values of these two end points are both greater than that of the
vertex, it is considered a local minima. On the other hand, if the y-values of these two
end points are both lesser than that of the vertex, it is considered a local marima. In
Figure 4.3, vertices VË and V2, for example, are odd-count and even-count vertices
respectively.
Even count

V.

V.

Odd count

Figure 4.3: Odd-even method: counting intersections at vertices.

4.2.2 Winding Number Method


Conceptually one can stretch a piece of elastic between the point in question and
another point on the polygon boundary. The end attached to the polygon boundary
is slid along the boundary until it has made one complete circuit. Now, we examine
66
Scan
Conversion of Solide
the point in queestion to see how many times the elastic has Wound it If this
is at least one, the point is an inside point.
nunber
The above method involves physical movement of a piece of elastic, suro 3,
the point in question. This may be implemented logically if we
consider
direction, where each of the polygon boundaries is directed to forn either polygon
an39
a
clockwise or a clockwise circuit. Let us now mark a boundary with +1 if the li:
directed from top to bottom, and with a -1 if the line is directed from bottom to
Horizontal lines are marked with 0. These labels are called winding number of.
concerned boundary line. Then we draw a horizontal line trom an external point
the point in question and compute the sum of the winding numbers of the interseet
boundary lines. If the sum is 0., the point is an outside point, else it is an inside point
The process is illustrated in Figure 4.4.

+1

V v
+1

V,
Figure 4.4: Counting winding number.

4.3 Solid Area Filling Algorithms


There exist a number of area illing algorithms. These include the simple boundary il!
algorithm, scan line algorithms, edge list algorithms and many others. In this section,
we shall discuss some of them which are very commonly used.
4.3.1 Boundary Fill Algorithm
Once an inside pixel (often called a seed pizel ) is determined, the region bounded
by the polygon can be painted with a colour other than the boundary colour, by
approaching towards the boundaries, along all possible directions from the sed pixel.
67
4.3 Solid Area Filling Algorithms

The process of setting pixel colour along a particular direction stops as soon as a
boundary pxel in that direction is encountered.
The boundary ill algorithm accepts, as inputs, the seed pixel coordinate (z, y), the
boundary colour and the fll colour. Starting at (z, v), the procedure tests neighbour
positions to determine whether they are parts of the boundary or not. If not, they
are painted with the fill colour or pattern and their neighbours are tested, in turd,
in a similar manner. There are two different approaches to determine the neighbour
pixels: () 4+neighbour and (iü) &-neighbour approach as shown in Figure 4.5. The
pseudo-code for the boundary fill algorithm, in case of a 4-neighbour situation is given
below.

Boundary fill algorithm


Procedure boundaryfill (, y, boundary.colour)

begin
current-pizel = (, y)
current-pizel.colour = colour of current-pizel
if (current pizel colour boundary.colour) then
begin
set-pixel (,y)
boundaryfill(z + 1, y, boundary.colour)
boundaryfillz - 1,y, boundary.colour')
boundaryfill(,y + 1, boundary.colour)
boundaryfill(z,y - 1, boundary_colour)
end
end

implementation of the
The above is a recursive algorithm. However, an iterative
pixels information are
sarne is possible using an explicit stack where the neighbour
procedure can be used for the
pushed in and popped out thereof later. A similar
in
&-neighbour situation. Seed pixels with 4-neighbour and &-neighbour are shown the
Figure 4.5. While using &-neighbour seed pixels, we may face a problemn of flling is
outside region also. This situation is shown in Figure 4.6. Here, the seed pixel of
located at a point nearest to the oblique boundary. One of the eight neighbours
the concerned seed clearly lies outside the boundary and will be responsible for filling
the external region also. A similar problem will arise in the case of 4-neighbour pixels.
The situation is described in Figure 4.7. Here, the area to be filled has two parts, and
the link between the two regions is via a diagonal pixel. If we use 4-neighbour seed
pixels, it is seen that the one of the regions becomes unreachable and cannot be filled.
Besides these special cases, both the algorithms suffer from unknown stack size and
redundant checking of already flled neighbour pixels.
Scan Conversion of Solid

Seed pixel

(b)
Neighbour pixel
(a)

Figure 4.5: (a) 4-neighbour and (b) 8-neighbour seed pixels.

Boundary

Fill colour

Seed pixel

Figure 4.6: Problem with 8-neighbour seed pixels.

Boundary
Fill colour

Seed pixel

Figure 4.7: Problen with 4-neighbour seed pixels.

4.3.2 Flood Fill Algorithn


In some cases, the region bounded by the polygon is to be filled in by a different colour
at different times. In such a situation a variation of the boundary fll algorithn is used
where a fill colour is included as an input parameter. The algorithm is given as follows:

Flood fAll algorithm

Procedure Hoodfill (r, y, boundary.colour, fillLcolour)


3 Solid Area Filling Algorithms 69

begin
current-pirel = (, y)
Purrent pirel.colour= colour of current-pirel
if (eurrent pirel.colour boundary.colour) and (current-pizel.colour fill.colour)
then
begin
set-pixel (z,y)
floodfll(z + 1,y, boundary.colour, fill.colour)
floodfllz -1,y, boundary.colour, fill.colour)
floodfill(z, y+ 1, boundary.colour, fill.colour)
floodfill(z,y - 1, boundary.colour, fill.colour)
end
end

In the case of any of the above seed fll algorithms, if instead of a colour or grey
shade, we fillthe interior of a polygon with acertain predefined pattern, the algorithm
is termed a pattern fill algorithm.

4.3.3 Scan Line Fill Algorithm


In the case of seed fill algorithms, an interior pixel is selected as a seed, and the
polygon is scan converted using a recursive boundary fill or a flood fill procedure.
A more efficient technique can be developed to eliminate the need for a seed pixel
by using the fact that except at boundaries, adjacent pixels are likely to have the
same colour. This property is called spatial coherence. For a raster system, adjacent
pixels on a scan line are likely to have the same characteristics, this is called scan line
coherence.
Each scan line within the scope of the polygon intersects some of the polygon
boundaries. And the scan line is divided into several regions. These regions are
alternatively inside or outside the polygon. So, we are to determine the intersections
of the scan lines with polygon boundaries and then the pixels which lie on the part of
the scan line that are inside the polygon are set with the desired colour or pattern.
Once we have determined the intersection of a polygon boundary with a scan line,
the intersection with the next scan line can be determined by using the following
relations.
Let the polygon be bounded by the rectangle (Tmin Ymin) and (max,Vmax). Then
total number of scan lines to be processed is ymax -ysin Now let us consider two
successive scan lines at y = y and yi+. Let the intersection of these two scan lines
with a particular polygon boundary have X coordinates of r = z; and zi+1, and the
boundary line has a slope of m. The following relations can be established from the
above informations:
70 Scan
Conversion of
Solids
+1-, =1
i+1
m=

1
IË+| = I;+
m

Thus, the r-values at the intersection with the next scan ine can be
deternine3
from the previous one.
Scan line algorithm
begin
for each scan line y do
begin
determine intersections with the polygon boundaries
store r-intercept values in a list for scan line y

end
Sort the T-intercept values for the y-list in ascending order
for each y-list do
begin
for all pairs (I1,I2) of T-intercepts in the list do
begin
extract the pair (I1,T2) of z-intercepts
fill the pixels on scan line y in the range (T1,T2
end
end
end

4.3.4 Scan Line Seed Fill Algorithm


The scan line seed fill algorithn is an area
filling
conventional seed fill algorithms and the scan line fillalgorithm
that uses features of
is initially deternined on a scan line. Filling is algorithms. Here, a seed pixel
basis. When the current scan line is filled up. the
done on a scan line after scan liue
into consideration. The algorithn is preseuted below. neighbouring scan lines are taken

Scan line seed fill algorithm

begin
select a seed pixel (Is y,) and push it onto a stack
43 Solid
Aroa Filling Algorithms 71

while the stack is not empty do


begin
pop a seed (T,. y.) from the stack
fll the span containing the seed pixel to the right
and left along the scan line until a boundary is found
Tit=X coordinate of the extreme left pixel in the span
Iright= X coordinate of the extreme right pixel in the span

check locations from (Tieft. y) to (Trightsy)


on the scan line y for any span of non-boundary pixels
if any span of pixels is found non-filled, push the
extreme right pixel within the range of
(Tleft, y)- (Trights )]l on to the stack
y=ys -1
check locations from (Ilefts y) to (Irightsy)
on the scan line y for any span of non-boundary pixels
if any span of pixels is found non-flled, push the
extreme right pixel within the range of
((ziefts y) - (Zright, V) on to the stack
end
end

The above algorithm has some advantages over the other scan line algorithms.
Some of these are as follows:

1. Avery small stack size, compared the huge one needed in case of the scan line
algorithm, is required here.
2. This algorithm can effectively skip any holes present within the polygon to be
filled.

EXAMPLE 4.1: Aring-shaped polygonal area is shown in Figure 4.8(a). Trace the
scan conversion process to ill the polygonal area using the scan line seed fill algorithm.
Solution. The seed pixel is chosen at the location narked with S. A tracing of scan
line seed ill procedure is depicted in Figure 4.8. The numbers 1, 2, 3, ., ete. indicates
successive seed locations pushed on the stack in that order.

4.3.5 Ordered Edge List Algorithm


In the case of the ordered edge list algorithm, we maintain a list of a-intercepts for
cach half interval scan line at y+ . (Half-interval scan lines are only conceptual. In
à rectangular grid of pixels, the pixel positions may be assuned to be the junctions of
horizontal and vertical lines representing rows and colunns. These pixels have finite
Sie and in our conventional coordinate system, we mark a location at the lower left
Scan
Conversion of Solids

(a (b (c)

d f)

g
i

(1

Figure 4.8: (Contd.)


. Sobd Area Filling Algorithms

2 2

(m) n (o)

(q (r
(p)

FILLED
SUCCESSFULLY

Boundary

1Fill colour
S hitial seed

(t)
(Example 4.!)
Figure 4.8: Steps of scan line seedfill algorithm
y-bucket corresponding to a certaiu
Corner of a rectangular cell.) Each list is called a
arravs or linked lists. For arravs.
y-value. Each list or bucket may be uaintained as
linkel lists, the processing
the maximum size requirennent is iot k1uown initially. For
time requirements are higher.
active edge list may help. Bv
For real-time scan conversion, the concepts of an
scan line intersections can
this, the amount of data storage is further reduced and the
be calculated incrementally.

Ordered edge list algorithm


begin
for each polygon edge do
begin
74
Scan
determine the highest scan lines intersected by the edge
place the rintercept value with the highest scan line in the
Conversion of Solids
corresponding y-buckets along with the incremnental valee
Ar and Ay
end
for each scan line do
begin
examine the corresponding y-bucket for any new edges
If any new edge is found, add it to the active edge list
sort the r-intercepts from the active edge list into increasing order:
i.e., z1 precedes r2, if z1 S T
extract pairs of intersections from the sorted z-list. Activate pixels on the
scan line y for integral values of zsuch that r S <
for each edge on the active edge list
decrement Ay by 1
if Ay < 0 drop the edge from the list
calculate the new T-intercept rneu = Told + Ar
end
end

EXAMPLE 4.2: Consider a polygon with vertices at A(4,5), B(5, 3), Cís 5
DI8. 1), E(1.1), and F(1, 2). Trace the scan conversion
process to fill this polyeon
using the ordered edge list algorithm.
Solution. Here, the highest half-interval scan lines corresponds to the y-values of
y= 4+ and y =1++. Hence, we shall start with a 4-bucket and a 1-bucket as
shown in the Figure 4.9. The 4-bucket contains information regarding
AF, AB. BC and CD, while the 4-bucket contains information regardingpolygon
edges
polygon edge
FE only. Initially the lists corresponding to the 4-bucket and 1-bucket were empty.
Now, since the highest scan line intersects polygon edges AF, AB, BC and CD, we
insert entries corresponding to these edges. The polygon edge FE passes through the
highest half-interval scan line at y = 1+5. Hence, we insert the corresponding entries
to this bucket.
Since the highest half-interval scan line is at y=4+, we start with the
4-bucket.
This is the initial active edge list. Information contents of the initial active edge iS?
are as follows:

LAB = 4.25 ArAB = 0.5 AyAB =2


TAF = 3.5
Ar F =-1.0 AyAr =3
TCB = 7.5
ArcB = -1.0 ych = 2
TCD = 8,0 Arcp = 0.0
43 Sold Area Filling Algorithms
75
buckets

Ay Ptr Ay Ptr

A
Ay Ptr Ay Ptr

Ay Ptr

Figure 4.9: y-buckets for ordered edge list ill algorithm (Example 4.2).
Non. r-intercepts of the active edges are sorted. Hence. we get a sorted list of
bves 3.5.4.25,7.5 and 8.0. These c-intercept values are picked up pairwise as
(z).72) from the left and for all intermediate z srt} 2% pixels on the
half-interval scan line at y = 4+; are filled.
Now, r-intercepts are incremented by Ar and Ay is decremented by one. Thus.
we get anew active edge list for scan line at y= 3+4. This list is described as follows:
TAB = 4.75 ArAB = 0.5 AyaB =1
TAF = 2.5 ArAF = -1.0 AyJAF =2
LCB = 6.5 ArCB =-1.0 AyCB =1
LCD = 8.0 Arcp = 0.0 Aycp =3
Now, the c-intercepts are sorted again, paired and scan converted as before. Fol
lowing an increment-decrement step for Ar and Ay respectively (as before), we get
an edge list for scan line at y = 2+5. Note that the bucket entries corresponding to
edges AB and CB no longer exist, because their Ay values reach 0.
IAF = 1.5 AcAr =-1.0 AyAF = 1
zCD = 8.0 Arcp = 0.0 Aycp = 2
Following a similar approach, we get the bucket corresponding to scan line at
y=1+}.
TCD = 8.0 Arcp = 0.0 AyCD = 1
Now, there is already an entry for the y-bucket at y = 1+}. Therefore, the
complete bucket information is as follows:
TFE = 1.0 AtAF =0.0 AyAF = 1
TCD = 8.0 Arcp = 0.0 Aycp =1
Sean
Here, we fill all the pixels having r-coordinates
between 1
Conversion of Solid
8
And the process is complete by now. The initial
y-buckets for this problernfor y - l+}
tracing of the concerned are shoW
in Figure 4.9 and Figure 4.10 shows a pictorial
problem

M (b)
B

A C

B
B

(d
(c)

(e) (f

Figure 4.10: Steps of ordered edge list fill algorithm (Example 4.2).
44 Questions 77

4.4 QUESTIONS
for doing
.What is solid aren scan conversion? Name some conmon algorithms
this.
8-connected pixels
,o What are advantages and disadvantages of 4-connected and
in the case of the floodfill algorithm?
. ls the floodfill algorithm suitable for large polygons? If not, then can you suggest
aremedy?
the odd
AWrite a procedure to determine aseed pixel for flling a polygon using
even method.
winding
As Write a procedure to determine a seed pixel for filling a polygon using the
number method.
2 x 2.
A6 Write an algorithm tor pattern illing where patterns are of size
8-connected area.
A7 Write a floodfill algorithmn to fill an
5), D(8, 1),
A8 A polygon ABCDEF has vertices at A(4, 5), B(5, 3), C(8,pixel, trace the
E(1, 1) and F(1, 2). Considering a suitable wall inside seed
floodfill algorithm and show the first ive snapshots of the stack used.
C(8, 5), D(8, 1),
4.9 A polygon ABCDEF has vertices at A(4, 5), B(5, 3),
E(1, 1) and F(1, 2). Apply the ordered edge list algorithm using active edge
line at
list to show the changes in the y-bucket values for each half-interval scan
fractional
yt. Assume intersections of scan lines and polygon edges at suitable
or integral values for r and y.
4), C(9, 7),
4.10 The coordinates of a polygon ABCDEFGH are A(2, 4), B(9,edges that will
D(8, 7), E(8, 9), F(4, 9), G(4, 7) and H(2, 7). Find out the
for1m active list on scan lines y = 6, 7, 8, 9 and 10.

You might also like