Boundary fill

From Wikipedia, the free encyclopedia

In computer graphics, Boundary fill, or Boundary-fill is an algorithm used to fill an area with a specified color. The algorithm works by filling an area with a specified color until the specified boundary color is encoutered. It is similar to the Flood fill algorithm.

[edit] The algorithm

The algorithm works by starting in a specified point (x,y), filling that point with the specified fill color if it is not a boundary and recursively continuing with the 4 or eight closest neighbours. The example below uses the 4 closest neighbours.

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

[edit] Other Uses

The Boundary fill algorithm can also be used in games like SameGame, in which same color adjacent blocks must be destroyed.

[edit] External links