Unit 3
Unit 3
FILLING ALGORITHMS
ALGORITHMS
3.1 Introduction 59
Objectives
3.2 Filled-Area Primitives 60
3.3 Scanline Polygon Fill Algorithm 61
3.4 Boundary Fill Algorithm 69
3.5 Character Generation 75
3.6 Summary 77
3.7 Solutions/Answers 77
3.1 INTRODUCTION
In Unit 2, you learnt how some basic geometric objects such as line segments,
circles, ellipses and other curves are processed for plotting on a display unit.
In most of the graphics packages you will find that polygons are also used as
standard output primitives. Filled polygons with single solid color (or other
alternatives for filling the interior) are provided as shape primitives.
In this unit, we shall discuss what we mean by filled area primitives and list two
basic methods to fill polygonal or arbitrary shaped closed regions in Section
3.2. We consider here methods for solid fill of specified areas. One of these
methods, called the Scanline Polygon Fill Algorithm, is discussed with full C-
implementation in Section 3.3. The second type of methods are generally
called seed fill algorithms, and we shall present two such algorithms in Section
3.4 with full implementation of Boundary Fill Algorithm. While creating or
editing a document in a word processing software, you might have used
letters, numbers and other characters in various font types and styles. We
shall discuss in Section 3.3 how different fonts are generated using 2D
drawing primitives.
Block 1 Fundamentals of Computer Graphics
Objectives
After reading this unit, you should be able to
• describe, implement and use the Scanline Polygon Fill Algorithm;
• apply the Odd-Even rule and the Nonzero Winding Number rule;
• differentiate between a 4-connected region and an 8-connected region;
• implement the Boundary Fill Algorithm for any type of closed region;
• generate a character or a font from the alphabet of any language.
Two basic approaches are followed in area filling on raster systems. In the first
approach overlap intervals for scanlines that cross the area are determined
per scanline (see Fig. 1 (a)). Second approach begins with an interior point
and fills the area moving outward from this point until the boundary condition is
reached (see Fig. 1(b)). An algorithm following the first approach is classified
as scan line algorithm and that falling under second class is called a seed
fill algorithm. Simple objects such as polygons, circles etc. are efficiently
filled with scan line fill algorithms and more complex regions use the seed fill
method. The scan line algorithm is mostly used in general graphics packages.
(a) (b)
Fig. 1
Let us begin with scan line polygon fill algorithm. Notice that polygons can be
as simple as a triangle and could be as complicated as the one shown in Fig.
2 below.
60
Unit 3 Filling Algorithms
We broadly keep the polygons in one of the three categories (i) convex (ii)
concave (iii) self intersecting. Mathematically, a self intersecting polygon is
concave. You will deal with such polygons in greater details for the purpose of
area filling.
Before we go ahead with area filling algorithms, a word about pixel addressing
and object geometry. You know that line segments are discretized into finite
number of pixels and each pixel has its height and width depending upon the
aspect ratio of the display unit. Therefore, if you plot a single pixel it will
occupy some area on display unit. This contributes to the object geometry
resulting in aberrations from the actual size. For example, if you have to plot a
line segment starting from (10, 10) to (15, 10) , then length of the segment is 5
whereas, in plotting this line segment, six pixel areas will be contributing to this
segment, resulting in increased length of line segment by one unit. In this
situation one can plot the line from (10, 10) to (14, 10). For closed regions we
can plot pixels that are interior to the boundary.
Or one can also map the world coordinates (object coordinates) to the screen
coordinates between pixels.
through the vertex x′2 . Note that the edges meeting x2′ are both lying above
x′2 . In this case, we can duplicate x2′ to get the pairs ( x1′ , x2′ ) and ( x′2 , x3′ ).
The same approach can be adopted in the cases where a scanline passes
through a vertex and both the edges incident to the vertex lie below it.
Now let us look at the scanline y ′′, which passes through the polygon vertices
x1′′ and x2′′. The edges incident on x1′′ lie on the opposite side of the scanline
y ′′, and the same is the case with x2′′. In such cases, each intersection point
is counted once. This can be done easily if we slightly shorten the upper end
of the lower edge at an intersection point.
Observe that for any scanline y we need to fill the area between pairs of x -
coordinates where y intersects the polygon edges. So, we need to process
only the nonhorizontal edges. Now let us look at the detailed procedure.
Step 3: Create a sorted edge table to store the nonhorizontal edges. The
table consists of as many rows as there are scanlines. Each edge has
three members:
62
Unit 3 Filling Algorithms
(b) lower x -coordinate,
(c) inverse of the edge slope.
i) Create an active edge list (AEL) that consists of all those edges
whose lower y -coordinate is less than or equal to y. The
edges are stored in AEL in the ascending order of their lower
x -coordinate values.
preceding x j .
iii) Update AEL as follows. For each edge e in AEL if the upper y -
coordinate of e is smaller than or equal to y, remove e from
AEL, otherwise increase its lower x -coordinate value by slope
inverse.
v) Set y = y + 1.
63
Block 1 Fundamentals of Computer Graphics
continue till y = 15 . Clearly for y = 10 there are only two edges that
15
14
13 15 16 –½ 15 16 0
12
11
y = 10 15 10 1 15 20 0
Notice that the edges e3 and e5 do not appear in the edge table, being
Step 4: Active edge list and x -coordinate pairs for all scanlines
y = 10, AEL = {e0 , e4 }, x -pairs = (10, 20)
y = 11, AEL = {e0 , e4 }, x -pairs = (11, 20)
y = 12, AEL = {e0 , e4 }, x -pairs = (12, 20)
y = 13, AEL = {e0 , e1 , e2 , e4 }, x -pairs = (13, 16) (16, 20)
y = 14, AEL = {e0 , e1 , e2 , e4 }, x -pairs = (14, 15.5) (16, 20)
y = 15, AEL = «
(20, 15)
(15, 15)
Note that the polygon filling scheme will not fill the pixels on the horizontal
edge e4 joining (16,15) and ( 20,15). But the boundary of the polygon will
display the edge. Similarly the vertex (15, 15) is plotted by virtue of it being a
boundary point.
***
To implement the Scanline Polygon Fill Algorithm, we shall define two struct
variables namely point and Edge as follows:
Edge* edges[GLUT_INNIT_WINDOW_HEIGHT];
Here GLUT_INNIT_WINDOW_HEIGHT is an OpenGL constant indicating the
height of the window initialised in terms of the number of scanlines. The full C-
program that implements the Scanline Polygon Fill Algorithm is given below, in
Listing 1.
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int n, i;
Point points[50];
printf("How many polygon vertices? ");
scanf("%d", &n);
printf("Enter vertices line by line.\n");
for(i = 0; i < n; i++)
scanf("%d%d", &points[i].x, &points[i].y);
glBegin(GL_LINE_LOOP);
for(i = 0; i < n; i++)
glVertex2i(points[i].x, points[i].y);
glEnd();
glFlush();
scanline_polygon_fill(n, points);
myInit();
glutDisplayFunc(display);
glutMainLoop();
}
Listing 1: Scanline Polygon Fill Algorithm Implementation
Note that the first for loop prepares the array edges to reserve blocks of
memory in order to store the edglist for each scanline. Next, the function call
create_edgelist() actually creates an edgelist. Then the pointer active
is initialised. The next for loop computes the minimum and maximum values
of the y -coordinates of the polygon vertices. The last for loop fills the
polygon line by line starting from ymin to ymax-1. For each scanline in the
range, an active edglist is created using the function call
create_active_edglist(). If the active list is nonempty, the current
scanline is filled through the function fillscan(), the active edgelist is
updated through update_active_list() and then sorted using
sort_active_list() .
68
Unit 3 Filling Algorithms
E1) For the following polygon, prepare an array of sorted edge lists and then
make the active edge list for scanlines y = 5, 20, 30, 35 . Coordinates of
the vertices are v0 = (10, 20), v1 = (20, 0), v2 = (30, 10), v3 = (40, 0),
v4 = (40, 40), v5 = (30, 30), v6 = (20, 40), v7 = (30, 20). Also find the
pairs of x -coordinate values for y = 5.
40
30
20
10
0
0 10 20 30 40 50
E2) For the polygon shown below how many times will the vertex V1 appear
in the set of intersection points for the scan line passing through that
point? How many times will you count it when you form the pairs of
intersection points? Justify your answer.
V0 V5
V1 = V4
V2 V3
Odd-Even Rule: Consider any polygon A and a point P in the plane of the
polygon. To check whether P lies inside or outside A, we draw a line from P
outside A. Applying the Odd-Even rule to the following polygon you can easily
see that its interior region.
line OP , starting at P, and count how many edges of A cross OP from left
to right and how many from right to left. Then ω ( P) is essentially the
difference of the two numbers. That is,
70
Unit 3 Filling Algorithms
from left. Thus, ω ( P) = 2 ≠ 0. Therefore, P lies inside the polygon. Note that
both the rules do not give the same interior and exterior regions. Let us look at
one more example.
Example 2: Apply the Odd-Even Rule and the Winding Number Rule to find
the interior and exterior regions of the following polygon. Do both the rules
produce the same exterior regions?
Solution: Using the Odd-Even Rule, it is easy to observe that the shaded
region as shown below is the interior region, and the rest is the exterior.
In order the apply the Winding Number Rule, we orient the edges of the
polygon as follows.
71
Block 1 Fundamentals of Computer Graphics
You can now see that all the interior regions that were obtained by the Odd-
Even Rule are also the interior regions by the Nonzero Winding Number Rule.
Now consider a point P in the region shaded dark above, and a reference
point O . When we move from P to O, the edges v2 v3 and v4 v5 cross the line
Thus the exterior regions produced by the two rules are not the same.
***
Given a pixel position ( x, y ), the question arises: What are the neighbours of
4-connected 8-connected
If we consider only the top, down, left or right pixels as the neighbours of
( x, y ), then the region defined this way is called a 4-connected region.
Thus, in a 4-connected region the neighbouring pixels of ( x, y ) are ( x, y + 1),
72 region that is called 8-connected. Depending on the type of region, there are
Unit 3 Filling Algorithms
two versions of Boundary Fill Algorithm (4-connected and 8-connected). Here
we have implemented the 4-connected version.
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int n, i;
Point points[50];
printf("How many polygon vertices? ");
scanf("%d", &n);
printf("Enter (nonnegative) coordinates of vertices:\n");
73
Block 1 Fundamentals of Computer Graphics
for(i = 0; i < n; i++)
scanf("%d%d", &points[i].x, &points[i].y);
glBegin(GL_LINE_LOOP);
for(i = 0; i < n; i++)
glVertex2i(points[i].x, points[i].y);
glEnd();
glFlush();
glutInitWindowSize(width, height);
glutInitWindowPosition(0, 0);
glutCreateWindow("Boundary Fill Algorithm (4-connected)");
myInit();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
E3) Shade the interior of the following polygon using Odd-Even Rule and
Winding Number Rule.
E4) Devise an algorithm for determining the interior regions for any input
set of vertices using the nonzero winding number rule and dot-product
calculations to identify the direction of edge-crossings.
E5) Explain how an ellipse displayed with the midpoint method could be
properly filled with a boundary fill algorithm.
There are two main approaches followed in character or font generation (i)
Bitmap font method (ii) Outline font method. In the bitmap method, a matrix of
bits (0 or 1) is formed which approximates the shape of the font. Every entry in
the matrix corresponds to a pixel and those pixels are plotted for which the
matrix entry is 1.
In order to understand how fonts are converted to bitmap, just think of a letter
drawn on a graph paper. Measure its span in terms of number of maximum x
and y units it covers. Then map the letter grid to a frame buffer position. For
example, in the picture given below, the letter R spans in an area
corresponding to the matrix of order 14 × 10. On the right is shown the
corresponding frame buffer positions with 1 indicating the pixel to be drawn,
and 0 indicating the pixel not to be drawn.
R
(a) Letter R drawn on a graph paper (b) Corresponding frame buffer positions
In the outline method we simply identify a few shape control points and draw
the corresponding Bezier curves for different segments of the outline.
Some predefined character sets are available in the OpenGL Utility Toolkit
(GLUT). So you need not create fonts as bitmap shapes unless you want to
display a font that is not available in GLUT. For example, you may want to 75
Block 1 Fundamentals of Computer Graphics
generate a font of your own mother tongue. The GLUT library contains
routines for displaying both bitmapped and outline fonts. Bitmapped GLUT
fonts are rendered using the glutBitmapCharacter( ) function, as shown
below
glutBitmapCharacter (font, character);
glRasterPos2i(20, 20);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10,'B');
An outline character is displayed with the following function call.
glutStrokeCharacter(font, character);
You can assign parameter 'font' either the value GLUT_STROKE_ROMAN, which
displays a proportionally spaced font, or the value
GLUT_STROKE_MONO_ROMAN, which displays a font with constant spacing.
You can specify and control the size and position of these characters by using
certain geometric transformations that you will study in the next block. You
may refer to OpenGL reference manual for more options.
The following code demonstrates displaying the string 'OpenGL' using the
function glutStrokeCharacter( ).
You may now test your understanding by trying out the following exercises.
E7) Use the outline method to plot the following font boundaries (style should
remain the same). Implement your method in C language using OpenGL.
76
Unit 3 Filling Algorithms
Gt
E8) Design a bitmap for the English vowels A, E, I, O, U for two different
sizes and then implement the bitmaps to plot these vowels on the
display. Keep in mind that baseline of all the bitmaps should remain the
same when plotting, just as it is printed in English language.
We now end this unit by giving a summary of what we have covered in it.
3.6 SUMMARY
In this unit, we have covered the following points.
1. We have described the Scanline Polygon Fill Algorithm and two seed fill
algorithms for filled-area primitives.
2. Boundary fill algorithm is suitable when the boundary has single colour
while flood fill algorithm is more suitable for filling regions which are
defined with boundary having more than one color, for example, a map
of a country surrounded with other countries.
3.7 SOLUTIONS/ANSWERS
E1) First label the vertices and edges as shown below.
V6 V4
40
E5 E4
30
E6 V5
V0 E7
20
V7 E3
E0 V2
10
E1
E2
0 V1 V3
0 10 20 30 40 50
77
Block 1 Fundamentals of Computer Graphics
Let ei = vi vi +1 , for i = 0, 1, K , 6. If mi is the slope of edge ei , then we
m7 = 0.
Start with y = 0 and continue till y = 40 . For y = 0 there are four
only, and for y = 30 the edges are e5 , e4 . The sorted edge table is
created as follows:
y=30 40 30 –1 40 30 1
y=0 20 20 –½ 10 20 1 10 40 –1 40 40 0
Active edge lists (AEL) for scan lines y = 5, 20, 30, 35 are as follows.
y = 5, AEL = {e0 , e1 , e2 , e3 }
y = 20, AEL = {e6 , e3 }
y = 30, AEL = {e6 , e5 , e 4 , e3 }
y = 35, AEL = {e6 , e5 , e 4 , e3 }
We start with y = 0 and iteratively compute the x -coordinate values for
E2) Note that as we move from v0 v1 to v1v 2 the lower y -values of these
(i) x < x min (ii) x > x max (iii) y < y min (ii) y > y max
In case none of the conditions is satisfied, you are required to have a ray
starting from Q and extending to a distant point from the polygon and
then construct a vector in the direction of this ray. For simplifying the
computations, you can choose a vector u from Q in the direction (1, 0) .
the dot product of the vector Vi +1 − Vi with v . Let d i = (Vi +1 − Vi ).v (dot
product). If d i > 0 , the edge Vi Vi +1 crosses the ray from right to left.
Update the ω n = ω n + 1 , else the edge crosses the ray from left to right
79
Block 1 Fundamentals of Computer Graphics
E5) Modify the function plot_ellipse_points() given in E12 of Unit 2 as follows.
E6) A code for flood fill algorithm with 4-connected cells is given below.
E7) Here letter G is produced using linear segments for outline method. You
may also use Bezier curves of higher degree to approximate the outline
of the character. The outline will be drawn using the code given here. For
filling the interior, you need to employ one of the fill area methods.
Vertices of the line segments are given as follows.
GLint vt[][2]={{315,245}, { 315,195}, {316,190},{333,183},
{ 251,183}, {263,186}, { 270, 190},{272,195}, {272,225}, {
253, 238}, {242,236}, {235,229}, {231,219}, {227,198}, {
227,169}, { 228,152}, {233,133}, {242,124}, {248,120}, {
253,120}, {269,119}, { 273,121}, { 280,126}, {292,134}, {
299,141}, {305,149}, {312,162}, {312,111}, {307,110}, {307,
120}, {304, 130}, {301, 133}, {298, 131}, { 289, 125},
{279, 120}, {266, 113}, {251, 109}, {239, 110},{229,110},
{219,114}, {208,120}, {196,129}, {190,138}, {185,148},
{181, 158}, {180,172},{180,184}, {180,196}, {184, 203},
{189, 214}, {198, 226}, {211, 235}, {220,242}, {230,245},
{245,247}, {260, 247}, {271, 241}, {284, 236}, {293, 231},
{299, 231}, {306, 235}, {315,245}};
void lineG(void)
{glBegin(GL_LINE_LOOP);
for(int i = 0;i<62;i++) {
vt[i][1]=480-vt[i][1];
glVertex2iv(vt[i]);}
glEnd();
}
Call the function lineG in your display function. Similar is the code for
letter ' t'.
80
Unit 3 Filling Algorithms
E8) Here we discuss the bitmap for letter 'E'. You need to place the
character 'E' on a square grid of sufficiently large size. If the character's
area is overlapping more than 50% of a cell of the square grid, assign
that cell a bit 1, otherwise assign the cell bit 0. This way you will have a
rectangular grid having cells either assigned 0 or 1. Map this onto a
rectangular matrix and plot the character 'E' using the bitmap method as
shown below.
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 0 0 0 0 1
1 1 0 0 0 0 0
1 1 0 0 0 0 0
1 1 0 0 0 0 1
1 1 1 1 1 1 1
1 1 0 0 0 0 1
1 1 0 0 0 0 0
1 1 0 0 0 0 0
1 1 0 0 0 0 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
81
Block 1 Fundamentals of Computer Graphics
Notes
82