Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void FloodFill(Point node, Color targetColor, Color replaceColor)
- {
- //perform bounds checking X
- if ((node.X >= CANVAS_SIZE) || (node.X < 0))
- return; //outside of bounds
- //perform bounds checking Y
- if ((node.Y >= CANVAS_SIZE) || (node.Y < 0))
- return; //ouside of bounds
- //check to see if the node is the target color
- if (pixels[node.X, node.Y].CellColor != targetColor)
- return; //return and do nothing
- else
- {
- pixels[node.X, node.Y].CellColor = replaceColor;
- //recurse
- //try to fill one step to the right
- FloodFill(new Point(node.X + 1, node.Y), targetColor, replaceColor);
- //try to fill one step to the left
- FloodFill(new Point(node.X - 1, node.Y), targetColor, replaceColor);
- //try to fill one step to the north
- FloodFill(new Point(node.X, node.Y - 1), targetColor, replaceColor);
- //try to fill one step to the south
- FloodFill(new Point(node.X, node.Y + 1), targetColor, replaceColor);
- //exit method
- return;
- }
- }
- private void QueueFloodFill(Point node, Color targetColor, Color replaceColor)
- {
- Queue<Point> points = new Queue<Point>();
- if (pixels[node.X, node.Y].CellColor != targetColor)
- return;
- points.Enqueue(node);
- while (points.Count > 0)
- {
- Point n = points.Dequeue();
- if (pixels[n.X, n.Y].CellColor == targetColor)
- pixels[n.X, n.Y].CellColor = replaceColor;
- if (n.X != 0)
- {
- if (pixels[n.X - 1, n.Y].CellColor == targetColor)
- points.Enqueue(new Point(n.X - 1, n.Y));
- }
- if (n.X != CANVAS_SIZE - 1)
- {
- if (pixels[n.X + 1, n.Y].CellColor == targetColor)
- points.Enqueue(new Point(n.X + 1, n.Y));
- }
- if (n.Y != 0)
- {
- if (pixels[n.X, n.Y - 1].CellColor == targetColor)
- points.Enqueue(new Point(n.X, n.Y - 1));
- }
- if (n.Y != CANVAS_SIZE - 1)
- {
- if (pixels[n.X, n.Y + 1].CellColor == targetColor)
- points.Enqueue(new Point(n.X, n.Y + 1));
- }
- }
- DrawCanvas();
- return;
- }
- private void RevisedQueueFloodFill(Point node, Color targetColor, Color replaceColor)
- {
- Queue<Point> q = new Queue<Point>();
- if (pixels[node.X, node.Y].CellColor != targetColor)
- return;
- q.Enqueue(node);
- while (q.Count > 0)
- {
- Point n = q.Dequeue();
- if (pixels[n.X, n.Y].CellColor == targetColor)
- {
- Point e = n;
- Point w = n;
- while ((w.X != 0) && (pixels[w.X, w.Y].CellColor == targetColor))
- {
- pixels[w.X, w.Y].CellColor = replaceColor;
- w = new Point(w.X - 1, w.Y);
- }
- while ((e.X != CANVAS_SIZE - 1) && (pixels[e.X, e.Y].CellColor == targetColor))
- {
- pixels[e.X, e.Y].CellColor = replaceColor;
- e = new Point(e.X + 1, e.Y);
- }
- for (int i = w.X; i <= e.X; i++)
- {
- Point x = new Point(i, e.Y);
- if (e.Y + 1 != CANVAS_SIZE - 1)
- {
- if (pixels[x.X, x.Y + 1].CellColor == targetColor)
- q.Enqueue(new Point(x.X, x.Y + 1));
- }
- if (e.Y - 1 != -1)
- {
- if (pixels[x.X, x.Y - 1].CellColor == targetColor)
- q.Enqueue(new Point(x.X, x.Y - 1));
- }
- }
- }
- }
- }
- private void RevisedQueueFloodFill(Point node, Color targetColor, Color replaceColor)
- {
- Queue<Point> q = new Queue<Point>();
- if (pixels[node.X, node.Y].CellColor != targetColor)
- return;
- q.Enqueue(node);
- Point n, e, w, x;
- while (q.Count > 0)
- {
- n = q.Dequeue();
- if (pixels[n.X, n.Y].CellColor == targetColor)
- {
- e = n;
- w = n;
- while ((w.X != 0) && (pixels[w.X, w.Y].CellColor == targetColor))
- {
- pixels[w.X, w.Y].CellColor = replaceColor;
- w = new Point(w.X - 1, w.Y);
- }
- while ((e.X != CANVAS_SIZE - 1) && (pixels[e.X, e.Y].CellColor == targetColor))
- {
- pixels[e.X, e.Y].CellColor = replaceColor;
- e = new Point(e.X + 1, e.Y);
- }
- for (int i = w.X; i <= e.X; i++)
- {
- x = new Point(i, e.Y);
- if (e.Y + 1 != CANVAS_SIZE - 1)
- {
- if (pixels[x.X, x.Y + 1].CellColor == targetColor)
- q.Enqueue(new Point(x.X, x.Y + 1));
- }
- if (e.Y - 1 != -1)
- {
- if (pixels[x.X, x.Y - 1].CellColor == targetColor)
- q.Enqueue(new Point(x.X, x.Y - 1));
- }
- }
- }
- }
- }
- for (int i = w.X; i <= e.X; i++)
- {
- Point x = new Point(i, e.Y);
- }
- Point x;
- for(int i = w.X; i<= e.X; i++)
- {
- x = new Point(i, e.Y);
- }
- private void RevisedQueueFloodFill(Point node, Color targetColor, Color replaceColor)
- {
- if (pixels[node.X, node.Y].CellColor != targetColor) return;
- Queue<Point> Q = new Queue<Point>();
- Q.Enqueue(node);
- while (Q.Count != 0)
- {
- Point n = Q.Dequeue();
- if (pixels[n.X, n.Y].CellColor == targetColor)
- {
- int y = n.Y;
- int w = n.X;
- int e = n.X;
- while (w > 0 && pixels[w - 1, y].CellColor == targetColor) w--;
- while (e < CANVAS_SIZE - 1 && pixels[e + 1, y].CellColor == targetColor) e++;
- for (int x = w; x <= e; x++)
- {
- pixels[x, y].CellColor = replaceColor;
- if (y > 0 && pixels[x, y - 1].CellColor == targetColor)
- {
- Q.Enqueue(new Point(x, y - 1));
- }
- if (y < CANVAS_SIZE - 1 && pixels[x, y + 1].CellColor == targetColor)
- {
- Q.Enqueue(new Point(x, y + 1));
- }
- }
- }
- }
- }
- private void RevisedQueueFloodFill(Point node, Color replaceColor)
- {
- Color targetColor = pixels[node.X, node.Y].CellColor;
- if (targetColor == replaceColor) return;
- Queue<Point> q = new Queue<Point>();
- q.Enqueue(node);
- Point n, t, u;
- while (q.Count > 0)
- {
- n = q.Dequeue();
- if (pixels[n.X, n.Y].CellColor == targetColor)
- {
- t = n;
- while ((t.X > 0) && (pixels[t.X, t.Y].CellColor == targetColor))
- {
- pixels[t.X, t.Y].CellColor = replaceColor;
- t.X--;
- }
- int XMin = t.X + 1;
- t = n;
- t.X++;
- while ((t.X < CANVAS_SIZE - 1) &&
- (pixels[t.X, t.Y].CellColor == targetColor))
- {
- pixels[t.X, t.Y].CellColor = replaceColor;
- t.X++;
- }
- int XMax = t.X - 1;
- t = n;
- t.Y++;
- u = n;
- u.Y--;
- for (int i = XMin; i <= XMax; i++)
- {
- t.X = i;
- u.X = i;
- if ((t.Y < CANVAS_SIZE - 1) &&
- (pixels[t.X, t.Y].CellColor == targetColor)) q.Enqueue(t);
- if ((u.Y >= 0) &&
- (pixels[u.X, u.Y].CellColor == targetColor)) q.Enqueue(u);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment