Flood fill, additionally referred to as seed fill, is a flooding algorithm that determines and alters the area connected to a given node in a multi-dimensional array with some matching attribute. It’s used in the “bucket” fill device of paint applications to fill related, similarly-coloured areas with a special shade, and in video games equivalent to Go and Minesweeper for figuring out which items are cleared. A variant known as boundary fill uses the same algorithms but is outlined as the realm related to a given node that does not have a specific attribute. Note that flood filling is just not appropriate for drawing filled polygons, as it would miss some pixels in additional acute corners. Instead, see Even-odd rule and Nonzero-rule. The normal flood-fill algorithm takes three parameters: a start node, a goal color, and a alternative coloration. The algorithm appears to be like for all nodes in the array that are linked to the start node by a path of the goal color and modifications them to the replacement shade.
For a boundary-fill, instead of the target colour, a border colour can be provided. In order to generalize the algorithm in the common means, the following descriptions will as an alternative have two routines available. One referred to as Inside which returns true for unfilled factors that, by their colour, would be contained in the crammed space, and one known as Set which fills a pixel/node. Any node that has Set referred to as on it must then no longer be Inside. Depending on whether we consider nodes touching at the corners related or not, we have now two variations: eight-means and 4-approach respectively. Though easy to know, the implementation of the algorithm used above is impractical in languages and environments where stack space is severely constrained (e.g. Microcontrollers). Moving the recursion into a knowledge structure (both a stack or a queue) prevents a stack overflow. Check and set every node’s pixel shade earlier than adding it to the stack/queue, decreasing stack/queue measurement.
Use a loop for the east/west instructions, queuing pixels above/beneath as you go (making it much like the span filling algorithms, under). Interleave two or more copies of the code with additional stacks/queues, to allow out-of-order processors extra alternative to parallelize. Use a number of threads (ideally with slightly completely different visiting orders, so they don’t stay in the same space). Quite simple algorithm – easy to make bug-free. Uses quite a lot of memory, notably when using a stack. Tests most crammed pixels a complete of 4 times. Not suitable for sample filling, because it requires pixel check outcomes to change. Access pattern shouldn’t be cache-friendly, for the queuing variant. Cannot simply optimize for multi-pixel words or bitplanes. It’s doable to optimize issues further by working primarily with spans, a row with constant y. The primary revealed full instance works on the next basic principle. 1. Starting with a seed level, fill left and proper.
Keep track of the leftmost filled level lx and rightmost filled point rx. This defines the span. 2. Scan from lx to rx above and below the seed point, searching for brand spanking new seed factors to continue with. As an optimisation, the scan algorithm doesn’t want restart from each seed point, but only those at the start of the following span. Using a stack explores spans depth first, while a queue explores spans breadth first. When a new scan could be completely within a grandparent span, it might definitely solely find crammed pixels, and so would not want queueing. Further, when a brand new scan overlaps a grandparent span, only the overhangs (U-turns and W-turns) should be scanned. 2-8x faster than the pixel-recursive algorithm. Access pattern is cache and bitplane-pleasant. Can draw a horizontal line fairly than setting individual pixels. Still visits pixels it has already filled. For the popular algorithm, 3 scans of most pixels. Not appropriate for pattern filling, because it requires pixel test results to alter.