Created
January 26, 2012 19:27
-
-
Save ErisianArchitect/1684547 to your computer and use it in GitHub Desktop.
Flood Fill Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Queue<Point> _floodQueue = new Queue<Point>(); | |
public static void FloodFill(Point start, Tile tile, MapRenderer map, DrawLayer layer) | |
{ | |
if (map.ContainsTilePoint(start.X, start.Y)) | |
{ | |
Tile startTile = map.GetTile(start.X, start.Y, layer); | |
if (startTile != tile) | |
{ | |
_floodQueue.Clear(); | |
_floodQueue.Enqueue(start); | |
while (_floodQueue.Count > 0) | |
{ | |
Flood(_floodQueue.Dequeue(), startTile, map, layer, tile); | |
} | |
} | |
} | |
} | |
private static void Flood(Point start, Tile startTile, MapRenderer renderer, DrawLayer layer,Tile tile) | |
{ | |
if (renderer.ContainsTilePoint(start.X, start.Y) && renderer.GetTile(start.X, start.Y, layer) == startTile) | |
{ | |
renderer.SetTile(start.X, start.Y, layer, tile); | |
_floodQueue.Enqueue(new Point(start.X - 1, start.Y)); | |
_floodQueue.Enqueue(new Point(start.X + 1, start.Y)); | |
_floodQueue.Enqueue(new Point(start.X, start.Y + 1)); | |
_floodQueue.Enqueue(new Point(start.X, start.Y - 1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment