Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void FloodFill(PixelManipulator manipulator, Point position, Pixel targetColor, Pixel replaceColor, byte threshold)
- {
- unchecked
- {
- manipulator.Position = position;
- if (!manipulator.Current.Similar(targetColor, threshold))
- return;
- Queue<Point> q = new Queue<Point>((manipulator.Width * manipulator.Height));
- q.Enqueue(manipulator.Position);
- HashSet<Point> visitedNodes = new HashSet<Point>();
- while (q.Count > 0)
- {
- Point n = q.Dequeue();
- if (n.X < 0 || n.Y < 0 || n.X > manipulator.Width || n.Y > manipulator.Height || visitedNodes.Contains(n))
- {
- continue;
- }
- visitedNodes.Add(n);
- manipulator.Position = n;
- if (!manipulator.Current.Similar(targetColor, threshold))
- {
- continue;
- }
- manipulator.Current = replaceColor;
- q.Enqueue(new Point(n.X - 1, n.Y)); //Left
- q.Enqueue(new Point(n.X + 1, n.Y)); //Right
- q.Enqueue(new Point(n.X, n.Y - 1)); //Top
- q.Enqueue(new Point(n.X, n.Y + 1)); //Bottom
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement