/// <summary> /// Starts a flood fill at point startX, startY. /// O(n) space - n= width*height - makes a copy of the bitmap temporarily in the memory /// </summary> /// <param name="texture"></param> /// <param name="startX"></param> /// <param name="startY"></param> /// <param name="newColor"></param> public static void FloodFill(this Texture2D texture, int startX, int startY, Color newColor) { Point start = new Point(startX, TransformToLeftTop_y(startY, texture.height)); Flat2DArray copyBmp = new Flat2DArray(texture.height, texture.width, texture.GetPixels()); Color originalColor = texture.GetPixel(start.X, start.Y); int width = texture.width; int height = texture.height; if (originalColor == newColor) { return; } copyBmp[start.X, start.Y] = newColor; Queue <Point> openNodes = new Queue <Point>(); openNodes.Enqueue(start); int i = 0; // TODO: remove this // emergency switch so it doesn't hang if something goes wrong int emergency = width * height; while (openNodes.Count > 0) { i++; if (i > emergency) { return; } Point current = openNodes.Dequeue(); int x = current.X; int y = current.Y; if (x > 0) { if (copyBmp[x - 1, y] == originalColor) { copyBmp[x - 1, y] = newColor; openNodes.Enqueue(new Point(x - 1, y)); } } if (x < width - 1) { if (copyBmp[x + 1, y] == originalColor) { copyBmp[x + 1, y] = newColor; openNodes.Enqueue(new Point(x + 1, y)); } } if (y > 0) { if (copyBmp[x, y - 1] == originalColor) { copyBmp[x, y - 1] = newColor; openNodes.Enqueue(new Point(x, y - 1)); } } if (y < height - 1) { if (copyBmp[x, y + 1] == originalColor) { copyBmp[x, y + 1] = newColor; openNodes.Enqueue(new Point(x, y + 1)); } } } texture.SetPixels(copyBmp.data); }
/// <summary> /// Starts a flood fill at point startX, startY. /// O(n) space - n= width*height - makes a copy of the bitmap temporarily in the memory /// </summary> /// <param name="texture"></param> /// <param name="startX"></param> /// <param name="startY"></param> /// <param name="newColor"></param> public static void FloodFill(this Texture2D texture, int startX, int startY, Color newColor) { Point start = new Point(startX, TransformToLeftTop_y(startY, texture.height)); Flat2DArray copyBmp = new Flat2DArray(texture.height, texture.width, texture.GetPixels()); Color originalColor = texture.GetPixel(start.X, start.Y); int width = texture.width; int height = texture.height; if (originalColor == newColor) { return; } copyBmp[start.X, start.Y] = newColor; Queue<Point> openNodes = new Queue<Point>(); openNodes.Enqueue(start); int i = 0; // TODO: remove this // emergency switch so it doesn't hang if something goes wrong int emergency = width*height; while (openNodes.Count > 0) { i++; if (i > emergency) { return; } Point current = openNodes.Dequeue(); int x = current.X; int y = current.Y; if (x > 0) { if (copyBmp[x - 1, y] == originalColor) { copyBmp[x - 1, y] = newColor; openNodes.Enqueue(new Point(x - 1, y)); } } if (x < width - 1) { if (copyBmp[x + 1, y] == originalColor) { copyBmp[x + 1, y] = newColor; openNodes.Enqueue(new Point(x + 1, y)); } } if (y > 0) { if (copyBmp[x, y - 1] == originalColor) { copyBmp[x, y - 1] = newColor; openNodes.Enqueue(new Point(x, y - 1)); } } if (y < height - 1) { if (copyBmp[x, y + 1] == originalColor) { copyBmp[x, y + 1] = newColor; openNodes.Enqueue(new Point(x, y + 1)); } } } texture.SetPixels(copyBmp.data); }