You are on page 1of 33

Chapter 04:Filling Algorithms

Polygon
A polyline is a chain of connected line segments. When starting point and
terminal point of any polyline is same, i.e. when polyline is closed then it
is called polygon.

Polygon Classifications
interior angle: is an angle inside the polygon boundary
that is formed by two adjacent edges.
convex polygon: interior angles of a polygon are less
than or equal to 180
Or Convex : is a polygon in which the line segment joining
any two points within the polygon lies completely inside
the polygon

Polygon Classifications

Concave polygon: the polygon that is not convex.


or Concave: is a polygon in which the line segment
joining any two points within the polygon may not lie
completely inside the polygon.

Polygons

convex

concave

all interior angles are at least one angle is


<1800
>1800
5

Identifying Concave Polygons


v5

E4

v4
E3

E5

v3
E2

v1

E1

v2

Method
if some vertices are on one
side and some on the
other side of an
extension line, then
concave

Polygon Filling:
Inside-Outside Test
Odd-Even Test
Draw a line from any point P
to a point outside the closed
polyline
If the number of line-segment
crossings is:
odd => P is an interior point
even => P is an exterior point
7

Polygon Tables
v1

E6
E1

E2

E3
S1

v5

S2
v3

E5
E4

v2
v4

Vertex
Table

Edge Table

v1:
v2:
v3:
v4:
v5:

E1:
E2:
E3:
E4:
E5:
E6:

x1, y1, z1
x2, y2, z2
x3, y3, z3
x4, y4, z4
x5, y5, z5

v1, v2
v2, v3
x3, v1
v3, v4
v4, v5
v5, v1

Surface Table
S1: E1, E2, E3
S2: E3, E4, E5, E6
8

Inside-Outside Tests
o Odd-even rule (or odd parity rule)
o Conceptually drawing a line from any position P to
a distant point outside the object.
o Counting the number of edge crossings along the
line.
P is interior: odd
P is exterior: even

Inside-Outside Tests
P1 is an interior point.
P2 is an exterior point.

p2

p1

10

Fill Area Algorithms

11

Area-Filling Algorithms:
Introduction

Assign or fill color to the created


polygon (inside region).
Basic idea:
Assign red color

Filling Mode

12

Fill Algorithms

Given the edges defining a polygon,


and a color for the polygon, we need
to fill all the pixels inside the
polygon.
Three different algorithms:
1. Scan-line fill
2. Boundary fill
3. Flood fill

13

Region Filling
Seed Fill Approaches
2 algorithms: Boundary Fill and Flood Fill
works at the pixel level
suitable for interactive painting apllications
Scanline Fill Approaches
works at the polygon level
better performance

14

Area-Filling Algorithms

Scan-Line
Algorithm

To fill simple convex and


concave polygons.

Seed Fill Algorithm

To fill arbitrary complex,


irregular boundaries.

15

Seed Fill Algorithms:


Connectedness
4-connected region: From a given
that you can get to by a series of 4
E and W)
8-connected region: From a given
that you can get to by a series of 8
E, W, NE, NW, SE, and SW)

4-connected

pixel, the region


way moves (N, S,
pixel, the region
way moves (N, S,

8-connected
16

Fill Area Algorithms

Fill-Area algorithms are used to


fill the interior of a polygonal
shape.

Many algorithms perform fill


operations by first identifying
the interior points, given the
polygon boundary.

17

Basic Filling Algorithm


The basic filling algorithm is commonly used
in interactive graphics packages, where the
user specifies an interior point of the region to
be filled.

4-connected pixels

18

Basic Filling Algorithm


[1] Set the user specified point.
[2] Store the four neighboring pixels in
a stack.
[3] Remove a pixel from the stack.
[4] If the pixel is not set,
Set the pixel
Push its four neighboring pixels
into the stack
[5] Go to step 3
[6] Repeat till the stack is empty.
19

Basic Filling Algorithm

Requires an interior point.

Involves considerable amount of


stack operations.

The boundary has to be closed.

Not suitable for self-intersecting


polygons

20

Types of Basic Filling Algorithms

Boundary Fill Algorithm


For filling a region with a single
boundary color.
Condition for setting pixels:

Color is not the same as border color


Color is not the same as fill color

Flood Fill Algorithm


For filling a region with multiple
boundary colors.
Condition for setting pixels:

Color is same as the old interior color


21

Filling Irregular Boundaries


boundary fill: expand and fill region until you reach
boundary color
Boundary

Fill

flood fill: expand and fill region while you find interior color
Interior

Fill

Boundary-Fill Algorithm

In many graphics
packages the user can
fill a region (defined by a
boundary).
In the figure, the
boundary is red and the
filling color is blue
The user needs to click
inside the region (seed)
23

Boundary Fill
Basic concept:
To start from a given interior position and paint outward
from this point until we encounter the specified boundary
conditions.

24

Boundary-Fill Algorithm

The fill method


can be applied
to a 4-connected
area or to an 8connected area

25

Boundary-Fill Algorithm

Start at point inside a region and paint the interior outward


toward the boundary.
Particularly useful in interactive painting program
Input
interior point (x, y)
color to be filled
boundary color
Starting from (x, y) the procedure tests neighboring
pixels to determine whether they are of the boundary
color.
If pixels = boundary color : stop.
boundary color : paint with filled color and
repeat procedure
26

Boundary Fill

Suppose that the edges of the polygon has already been


colored.
Suppose that the interior of the polygon is to be colored a
different color from the edge.
Suppose we start with a pixel inside the polygon, then we
color that pixel and all surrounding pixels until we meet a pixel
that is already colored.
void boundaryFill(int x, int y, int fillColor, int borderColor)
{
int interiorColor;
getPixel(x,y,interiorColor);
if ((interiorColor!=borderColor)&&(interiorColor!=fillColor))
{
setPixel(x,y,fillColor);
boundaryFill(x+1,y,fillColor,borderColor);
boundaryFill(x-1,y,fillColor,borderColor);
boundaryFill(x,y+1,fillColor,borderColor);
boundaryFill(x,y-1,fillColor,borderColor);
}
}

27

Boundary-Fill Algorithm
void boundaryFill4 (int x, int y, int fill, int
boundary)
{
int current;
current = getPixel (x, y);
if ((current != boundary) && (current != fill))
{
setColor (fill);
setPixel (x, y);
boundaryFill4 (x+1, y, fill, boundary);
boundaryFill4 (x-1, y, fill, boundary);
boundaryFill4 (x, y+1, fill, boundary);
boundaryFill4 (x, y-1, fill, boundary);
}

28

Flood Fill

For an area not defined within a single

color boundary.
Paint such areas by replacing a
specified interior color instead of
searching for a boundary color.
Start from a specified interior point (x, y)
Reassign all pixel values that are currently
set to a given interior color(s).

29

Flood Fill Algorithm

Used when an area defined with


multiple color boundaries
Start at a point inside a region
Replace a specified interior color (old
color) with fill color
Fill the 4-connected or 8-connected
region until all interior points being
replaced
30

Flood Fill

Suppose we want to color the entire area whose


original color is interiorColor, and replace it with
fillColor.
Then, we start with a point in this area, and then
color all surrounding points until we see a pixel that
is not interiorColor.

void floodFill(int x, int y, int fillColor, int interiorColor) {


int color;
getPixel(x,y,color)
if (color==interiorColor) {
setPixel(x,y,fillColor);
floodFill(x+1,y,fillColor,interiorColor);
floodFill(x-1,y,fillColor,interiorColor);
floodFill(x,y+1,fillColor,interiorColor);
floodFill(x,y-1,fillColor,interiorColor);
31
}
}

Flood-Fill Algorithm
void floodFill4 (int x, int y, int fillColor, int oldColor)
{
if (getPixel (x, y) == oldColor)
{
setColor (fillColor);

setPixel (x, y);


floodFill4 (x+1, y, fillColor, oldColor);
floodFill4 (x-1, y, fillColor, oldColor);
floodFill4 (x, y+1, fillColor, oldColor);

floodFill4 (x, y-1, fillColor, oldColor);


}
}

32

Fill Methods
BOUNDARY
We need to
specify:

Interior point
Fill color
Border color

Condition:

FLOOD
We need to
specify:

check current
point color
Keep filling
while not
border color
and not fill color

Interior point
Fill color
Interior color

Condition:

check current point


color
Keep filling while
interior color
(and not fill color)

33

You might also like