public void FloodFill(int x, int y) { _ranges = new FloodFillRangeQueue((_fillLayer.TilesWide + _fillLayer.TilesHigh) / 2 * 5); _matchStack = _fillLayer[x, y]; LinearFill(ref x, ref y); while (_ranges.Count > 0) { FloodFillRange range = _ranges.Dequeue(); int upY = range.Y - 1; int downY = range.Y + 1; TileCoord tid; for (int i = range.StartX; i <= range.EndX; i++) { tid = new TileCoord(i, upY); if (range.Y > 0 && /*!_sourceStack.Equals(_fillLayer[tid]) &&*/ (_matchStack == null ? _matchStack == _fillLayer[tid] : _matchStack.Equals(_fillLayer[tid]))) LinearFill(ref i, ref upY); tid = new TileCoord(i, downY); if (range.Y < (_fillLayer.TilesHigh - 1) && /*!_sourceStack.Equals(_fillLayer[tid]) &&*/ (_matchStack == null ? _matchStack == _fillLayer[tid] : _matchStack.Equals(_fillLayer[tid]))) LinearFill(ref i, ref downY); } } }
public void FloodFill(int x, int y) { _ranges = new FloodFillRangeQueue((_fillLayer.TilesWide + _fillLayer.TilesHigh) / 2 * 5); _matchStack = _fillLayer[x, y]; LinearFill(ref x, ref y); while (_ranges.Count > 0) { FloodFillRange range = _ranges.Dequeue(); int upY = range.Y - 1; int downY = range.Y + 1; TileCoord tid; for (int i = range.StartX; i <= range.EndX; i++) { tid = new TileCoord(i, upY); if (range.Y > 0 && /*!_sourceStack.Equals(_fillLayer[tid]) &&*/ (_matchStack == null ? _matchStack == _fillLayer[tid] : _matchStack.Equals(_fillLayer[tid]))) { LinearFill(ref i, ref upY); } tid = new TileCoord(i, downY); if (range.Y < (_fillLayer.TilesHigh - 1) && /*!_sourceStack.Equals(_fillLayer[tid]) &&*/ (_matchStack == null ? _matchStack == _fillLayer[tid] : _matchStack.Equals(_fillLayer[tid]))) { LinearFill(ref i, ref downY); } } } }
public static void Flood(Vector2Int32 start, Vector2Int32 minBound, Vector2Int32 maxBound, Func <Vector2Int32, bool> validation) { var ranges = new FloodFillRangeQueue(); var points = new HashSet <Vector2Int32>(); LinearFloodFill(ref start, ref minBound, ref maxBound, validation, ref ranges, ref points); while (ranges.Count > 0) { //**Get Next Range Off the Queue FloodFillRange range = ranges.Dequeue(); //**Check Above and Below Each Pixel in the Floodfill Range int upY = range.Y - 1;//so we can pass the y coord by ref int downY = range.Y + 1; var curPoint = new Vector2Int32(); for (int i = range.StartX; i <= range.EndX; i++) { //*Start Fill Upwards //if we're not above the top of the bitmap and the pixel above this one is within the color tolerance curPoint = new Vector2Int32(i, upY); if (range.Y > 0 && (!points.Contains(curPoint) && validation(curPoint))) { LinearFloodFill(ref curPoint, ref minBound, ref maxBound, validation, ref ranges, ref points); } //*Start Fill Downwards //if we're not below the bottom of the bitmap and the pixel below this one is within the color tolerance curPoint = new Vector2Int32(i, downY); if (range.Y < (maxBound.Y - 1) && (!points.Contains(curPoint) && validation(curPoint))) { LinearFloodFill(ref curPoint, ref minBound, ref maxBound, validation, ref ranges, ref points); } } } }
private static void LinearFloodFill(ref Vector2Int32 start, ref Vector2Int32 minBound, ref Vector2Int32 maxBound, Func <Vector2Int32, bool> validation, ref FloodFillRangeQueue ranges, ref HashSet <Vector2Int32> points) { //FIND LEFT EDGE OF COLOR AREA int lFillLoc = start.X; //the location to check/fill on the left int x = start.X; int y = start.Y; points.Add(start); while (true) { points.Add(new Vector2Int32(lFillLoc, y)); // Preform validation for next point lFillLoc--; var curPoint = new Vector2Int32(lFillLoc, y); if (lFillLoc <= minBound.X || !validation(curPoint) || points.Contains(curPoint)) { break; //exit loop if we're at edge of bitmap or match area } } lFillLoc++; //FIND RIGHT EDGE OF COLOR AREA int rFillLoc = x; //the location to check/fill on the left while (true) { points.Add(new Vector2Int32(rFillLoc, y)); rFillLoc++; var curPoint = new Vector2Int32(rFillLoc, y); if (rFillLoc >= maxBound.X || !validation(curPoint) || points.Contains(curPoint)) { break; //exit loop if we're at edge of bitmap or color area } } rFillLoc--; var r = new FloodFillRange(lFillLoc, rFillLoc, y); ranges.Enqueue(ref r); }
private static void LinearFloodFill(ref Vector2Int32 start, ref Vector2Int32 minBound, ref Vector2Int32 maxBound, Func<Vector2Int32, bool> validation, ref FloodFillRangeQueue ranges, ref HashSet<Vector2Int32> points) { //FIND LEFT EDGE OF COLOR AREA int lFillLoc = start.X; //the location to check/fill on the left int x = start.X; int y = start.Y; points.Add(start); while (true) { points.Add(new Vector2Int32(lFillLoc, y)); // Preform validation for next point lFillLoc--; var curPoint = new Vector2Int32(lFillLoc, y); if (lFillLoc <= minBound.X || !validation(curPoint) || points.Contains(curPoint)) break; //exit loop if we're at edge of bitmap or match area } lFillLoc++; //FIND RIGHT EDGE OF COLOR AREA int rFillLoc = x; //the location to check/fill on the left while (true) { points.Add(new Vector2Int32(rFillLoc, y)); rFillLoc++; var curPoint = new Vector2Int32(rFillLoc, y); if (rFillLoc >= maxBound.X || !validation(curPoint) || points.Contains(curPoint)) break; //exit loop if we're at edge of bitmap or color area } rFillLoc--; var r = new FloodFillRange(lFillLoc, rFillLoc, y); ranges.Enqueue(ref r); }
public static void Flood(Vector2Int32 start, Vector2Int32 minBound, Vector2Int32 maxBound, Func<Vector2Int32, bool> validation) { var ranges = new FloodFillRangeQueue(); var points = new HashSet<Vector2Int32>(); LinearFloodFill(ref start, ref minBound, ref maxBound, validation, ref ranges, ref points); while (ranges.Count > 0) { //**Get Next Range Off the Queue FloodFillRange range = ranges.Dequeue(); //**Check Above and Below Each Pixel in the Floodfill Range int upY = range.Y - 1;//so we can pass the y coord by ref int downY = range.Y + 1; var curPoint = new Vector2Int32(); for (int i = range.StartX; i <= range.EndX; i++) { //*Start Fill Upwards //if we're not above the top of the bitmap and the pixel above this one is within the color tolerance curPoint = new Vector2Int32(i, upY); if (range.Y > 0 && (!points.Contains(curPoint) && validation(curPoint))) LinearFloodFill(ref curPoint, ref minBound, ref maxBound, validation, ref ranges, ref points); //*Start Fill Downwards //if we're not below the bottom of the bitmap and the pixel below this one is within the color tolerance curPoint = new Vector2Int32(i, downY); if (range.Y < (maxBound.Y - 1) && (!points.Contains(curPoint) && validation(curPoint))) LinearFloodFill(ref curPoint, ref minBound, ref maxBound, validation, ref ranges, ref points); } } }