You are on page 1of 23

Filled Area Primitives

Unit Lecture Outline

 Introduction

 Types of Polygons

 Inside and Outside Tests

 Polygon Filling Algorithms

1
Introduction

We have seen scan conversion of lines, circles in our last


lectures. In this lecture, we are going to study different types
of polygons, their representation and filling algorithms for
them.

A polyline is a chain of connected line segments. It is specified


by giving the vertices (nodes) P0, P1, P2 ….. and so on. The first
vertex is called the initial or starting point and the last vertex
is called the final or terminal point.

2
Filled Area Primitives
Types of Polygons
The classification of polygons is based on where the line
segment joining any two points within the polygon is going to
lie. There are two types of polygons:
•Convex
•Concave
A Convex polygon is a polygon in which the line segment
joining any two points within the polygon lies completely
inside the polygon.
A Concave polygon is a polygon in which the line segment
joining any two points within the polygon may not lie
completely inside the polygon.

3
Inside and Outside Test
Once the polygon is entered in the display file, we can draw the
outline of the polygon. To show polygon as a solid object we have
to set the pixels inside the polygon as well as pixels on the
boundary of it.
Now, the question is how to determine
whether or not a point is inside of a
polygon.
One simple method is to construct a line segment between the
point in question and a point known to be outside the polygon.
Now, count how many intersections of the line segment with the
polygon boundary occur. If there are an odd number of
intersections, then the point in question is inside, otherwise it is
outside.
This method is called even-odd method of determining polygon
inside points
4
Inside and Outside Test

If the intersection point is vertex of the polygon then we have to


look at the other endpoints of the two line segments which meet
at the vertex.

If these points lie on the same side of the


constructed line, then the point in
question counts as an even number of
intersections.

If they lie on opposite sides of the constructed line, then the point
is counted as a single intersection.

5
Filling Polygon

There are two basic approaches to area filling on raster


systems.
One way to fill an area is to determine the overlap intervals
for scan lines that cross the area (scan line).
Another method for area filling is to start from a given
interior position and paint outward from this point until we
encounter the specified boundary conditions (fill method).

Scan-line: simple objects, polygons, circles, ellipses, and other


simple curves.

Fill-method: for more complex boundaries and in interactive


painting systems

6
Filling Polygon

Filling the polygon means highlighting all the pixels which lie
inside the polygon with any colour other than background
colour. Polygons are easier to fill since they have linear
boundaries.

There are two basic approaches used to fill the polygon.

One way to fill a polygon is to start from a given “seed”, point


known to be inside the polygon and highlight outward from
this point i.e., neighbouring pixels until we encounter the
boundary pixels. This approach is called seed fill because
colour flows from the seed pixel until reaching the polygon
boundary.

7
Filling Polygon

The other approach to fill the polygon is to apply the inside


test i.e., to check whether the pixel is inside the polygon or
outside the polygon and then highlight pixels which lie inside
the polygon. This approach is known as scan-line algorithm.
It avoids the need for a seed pixel but it requires some
computation.
Seed Fill Algorithm

The seed fill algorithm is further classified as flood fill


algorithm and boundary fill algorithm. Algorithms that fill
interior-defined regions are called flood-fill algorithms;
those that fill boundary-defined regions are called boundary-
fill algorithms or edge-fill algorithms.

8
Boundary Fill Algorithm

• Area filling is to start at a point inside a region and paint


the interior outward toward the boundary.
• If the boundary is specified in a single color, the fill
algorithm proceeds outward pixel by pixel until the
boundary colour specified, this method is called boundary-
fill algorithm .
• A boundary-fill procedure accepts as input the coordinates
of an interior point (x, y), a fill color, and a boundary color.
• Starting from (x, y), the procedure tests neighboring
positions to determine whether they are of the boundary
color.
• If not, they are painted with the fill color, and their
neighbors are tested

9
Boundary Fill Algorithm

• This process continues until all pixels up to the boundary


color for the area have been tested

10
Boundary Fill Algorithm

• There are two ways test the neighboring points


• 4-connect:- Four neighboring points are tested.
• These are the pixel positions that are right, left,
above, and below the current pixel.

• 8-connect:- Here the set of neighboring positions to


be tested includes the four diagonal pixels

11
Boundary Fill Algorithm
• The following procedure illustrates a recursive method for
filling a 4-connected area with an intensity specified in
parameter fill up to a boundary color specified with
parameter boundary.
• We can extend this procedure to fill an 8-connected region
by including four additional statements to test diagonal
positions, such is (x + 1, y + 1).

12
Boundary Fill Algorithm

• But a 4-connected boundary-fill algorithm would only


fill part of that region

13
Boundary Fill Algorithm

• 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.
• Encountering a pixel with the fill color can cause a
recursive branch to terminate , leaving other interior
pixels 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

14
Boundary Fill Algorithm

• 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.
• Then we locate and stack starting
• positions for spans on the adjacent scanlines, where spans
are defined as the contiguous horizontal string of positions
bounded by pixels displayed in the border color.

15
Flood Fill Algorithm

• Some times we want to fill in (or recolor) an area that is


not defined within a single color boundary.

16
Flood 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.
• 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.

17
Scan Line Algorithm

The basic scan-line algorithm is as follows:


– Find the intersections of the scan line with all edges of the
polygon
– Sort the intersections by increasing x coordinate
– Fill in all pixels between pairs of intersections that lie
interior to the polygon

18
Scan Line Algorithm

19
Scan Line Algorithm

Basic algorithm:
– Assume scan line start from the left and is outside the
polygon.
– When intersect an edge of polygon, start to color each
pixel (because now we’re inside the polygon), when
intersect another edge, stop coloring …
– Odd number of edges: inside
– Even number of edges: outside

Advantage of scan-line fill: It does fill in the same order as


rendering, and so can be pipelined.

20
End of Lecture

End of Lecture

21
Acknowledgements

I sincerely thank to all the authors/co-authors from which I have


taken their content or part of their content in preparing this
presentation.

22

You might also like