Sometimes we want to fill in (or recolor) an area that is not defined within a single
color boundary. Figure 3-46 shows an area bordered by several different color
regions. We can paint such areas by replacing a specified interior color instead of
searching for a boundary color value. This approach is called a flood-fill algorithm.
We start from a specified interior point (x, y) and reassign all pixel values
that are currently set to a given interior color with the desired fill color. 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. Using either a Cconnected or
8-connected approach, we then step through pixel positions until all interior
points have been repainted. The following procedure flood fills a konnected region
recursively, starting from the input position.
voiQfloodFill4 ( i n t x, i n t y, i n t f i l l c o l o r , i n t oldcolor)
f
i f (getpixel (x. y) == oldcolor) (
setcolor ( f i l l c o l o r ) ;
setpixel (x, y ) :
floodFill4 ( x + l , y, fillColor, oldColor):
floodfill4 (x-1, y, fillcolor, oldcolor);
floodPill4 (x, y + l , fillcolor, oldcolor);
floodFill4 ( x , y-1, fillColor, oldcolor);
No comments:
Post a Comment