Area Fill Polygons: Scanline Algorithms
Area Fill Polygons: Scanline Algorithms
Area Fill
1. BoundaryFlood Fill
2. Scanline Polygon Fill
3. Scanline Boundary Fill
4. Pattern Fill
Polygons
Definittion: A shape formed by line
segments that are placed end to end,
creating a continuous closed path
Divided into three basic types:
1. convex
2. concave
3. complex
Convex polygons
Concave polygons
Complex polygons
Boundary/Flood Fill Algorithms
"Determine which points are inside from
pixel color information
eg. interior color. boundary color. fill color.
current pixel color
Color the ones that are inside
Current pixel
Fill color: red
Interior color: white
Boundary color: black
Current color: white
Scanline Algorithms
"Examine horizontal scanlines spanning area
"Find intersection points between current
scanline and borders
"Color pixels along the scanline between
alternate pairs of intersection points
"Especially useful for filling polygons
polygon int pt calculations are very simple
Use vertical and horizontal coherence to get new
intersection points from old rapidly
2
Connected Area Boundary
Fill Algorithm
"For arbitrary closed areas
"Input
Boundary Color BC. Fill Color FC
x.y coordinates of seed point known to be
inside
"Define a recursive BndFillx.y.BC.FC
function
- If pixel x.y not set to BC or FC. then set to FC
Call BndFill for neighboring points
Connected Area Boundary
Fill Algorithm (cont.)
"To be able to implement this. need an
inquire function
"eg. Windows GetPixelx.y
returns color of pixel at x.y
The BndFill() Function
BndFillx.y.BC.FC
{
color - GetPixelx.y
if color - BC ss color - FC
{
SetPixelx.y.FC.
BndFillx.y.BC.FC. BndFillx.y.BC.FC.
BndFillx.y.BC.FC. BndFillx.y.BC.FC.
}
}
"This would be called by code like
BndFilla.aa..s. .s are colors
Windows GDI colors are COLORREFs
RGB macro could be used
"As given. only works with 4-connected regions
"Boundary must be of a single color
Could have multiple interior colors
Flood Fill Algorithm
"A variation Boundary Fill
"Fill area identified by the interior color
instead of boundary color
"Good for single colored area with
multicolor border
Ups & Downs of Boundary /
Flood Fill
"Big Up Can be used for arbitrary areas
"BUT Deep Recursion so
Uses enormous amounts of stack space
Adjust stack size before building in
Windows
"Also very slow since
Extensive pushing/popping of stack
Pixels may be visited more than once
GetPixel s SetPixel called for each pixel
2 accesses to frame buffer for each pixel
plotted
3
Adjusting Stack Size in VC
Step 1
Step 2
Scanline Polygon Fill
Algorithm
"Look at individual scan lines
"Compute intersection points with polygon
edges
"Fill between alternate pairs of intersection
points (simply called Odd Parity)
Odd Parity Test
"Count the number of edges that the line
crosses:
If the number of polygon edges crossed is
odd. then P lies within the polygon.
if the number of edges is even, then P lies
outside of the polygon.
Scanline Polygon Fill Algorithm
Unfilled polygon
Step 0: Order all vertices
Scanline Polygon Fill Algorithm
Step 1: Create Edges Table (ET)
Scanline Polygon Fill Algorithm
Step 2: Create Global Edges Table (GET)
4
Scanline Polygon Fill Algorithm
Step 3: Initializing Parity
The initial parity is even since no edges have been
crossed yet
Step 4: Initializing the Scan-Line:
The initial scan-line is equal to the lowest y value for all
of the global edges. Since the global edge table is
sorted, the scan-line is the minimum y value of the first
entry in this table.
Scanline Polygon Fill Algorithm
Step 5: Create Active Edges Table (AET)
The Scanline Boundary Fill
Algorithm (General)
Select a Seed Point x.y
Push x.y onto Stack
While Stack is not empty
Pop Stack retrieve x.y
Fill current run y iterate on x until borders are hit
To left Push unfilled pixels before abovebelow border
pixels new abovebelow seeds
To right Push unfilled pixels after abovebelow border
pixels new abovebelow seeds
The Scanline Boundary Fill
Algorithm for Convex Polygons
Select a Seed Point x.y
Push x.y onto Stack
While Stack is not empty
Pop Stack retrieve x.y
Fill current run y iterate on x until borders are hit
Push leftmost unfilled. nonborder pixel above
-->new above seed
Push leftmost unfilled. nonborder pixel below
-->new below seed
4. Pattern Filling
"Represent fill pattern with a Pattern Matrix
"Replicate it across the area until covered
by nonoverlapping copies of the matrix
Called Tiling
Pattern Filling--Pattern Matrix
7
8
6
5
4
3
2
1
0
9 8 7 6 5 4 3 2 1 0
0 0 0
0 1 0
1 1 0
Pattern
Pattern Matrix
W = 3
H = 3
2 1 0 2 1 0 2 1 0 Y posn
8 7 6 5 4 3 2 1 0 y
2 1 0 2 1 0 2 1 0 x posn
8 7 6 5 4 3 2 1 0 x
Tiling
In general, posn in matrix:
xpos = x%W, ypos = y%H
5
Using the Pattern Matrix
" Modify fill algorithm
" As (x,y) pixel in area is examined:
if(pat_mat[x%W][y%H] == 1)
SetPixel(x,y);
Color Patterns
"Pattern Matrix contains color values
"So read color value of pixel directly from
the Pattern Matrix
SetPixelx. y. patmat,xW;,yH;
Moving the Filled Polygon
"As done above. pattern doesnt move with
polygon
"Need to anchor pattern to polygon
"Fix a polygon vertex as pattern reference
point. eg. xa.ya
If patmatrix,xxaW;,yyaH;--
SetPixelx.y
"Now pattern moves with polygon