using System.Collections.Generic;
private const int capacity = 625;
public static void Main() {
int[] range = new int[capacity];
int width = (int)Math.Sqrt(capacity),
height = (int)Math.Sqrt(capacity);
range = DrawCircle(range, 0, 0, width, height, 5);
Print(range, (int)Math.Sqrt(capacity));
for(int j = 0; j < capacity; ++j)
FloodFillHash(range, x, y, width, height, 1, 2, out i);
Print(range, (int)Math.Sqrt(capacity));
Console.WriteLine("Iterations: {0}", i);
private static int[] DrawCircle(int[] source, int x, int y, int width, int height, int r, bool inCenter = true)
for (int u = 0; u < width; ++u) {
for (int v = 0; v < height; ++v) {
if ((x-u)*(x-u) + (y-v)*(y-v) < rSquared) {
source[Pn(u, v, width)] = 1;
Console.WriteLine("Iterations: {0}", i);
private static void Print < T > (T[] objs, int splitEvery = 10) {
foreach(T item in objs) {
Console.WriteLine(); ++i;
public static int P(int x, int y, int w, int h) {
return x + ((h - y - 1) * w);
public static int Pn(int x, int y, int w) {
public static void FloodFill(int[] source, int x, int y, int width, int height, int targetColor, int replacementColor, out int i) {
Stack < int > pixels = new Stack < int > ();
targetColor = source[P(x, y, width, height)];
pixels.Push(Pn(x, y, width));
while (pixels.Count > 0) {
if (source[P(_x, _y, width, height)] == targetColor) {
source[P(_x, _y, width, height)] = replacementColor;
if (_x > 0) pixels.Push(Pn(_x - 1, _y, width));
if (_x < width - 1) pixels.Push(Pn(_x + 1, _y, width));
if (_y > 0) pixels.Push(Pn(_x, _y - 1, width));
if (_y < height - 1) pixels.Push(Pn(_x, _y + 1, width));
public static void FloodFillHash(int[] source, int x, int y, int width, int height, int targetColor, int replacementColor, out int i) {
HashSet < int > queue = new HashSet < int > ();
HashSet < int > processed = new HashSet < int > ();
targetColor = source[P(x, y, width, height)];
if(targetColor == replacementColor)
queue.Add(Pn(x, y, width));
for (int offsetX = -1; offsetX < 2; offsetX++)
for (int offsetY = -1; offsetY < 2; offsetY++)
if (offsetX == 0 && offsetY == 0 || offsetX == offsetY || offsetX == -offsetY || -offsetX == offsetY) continue;
int target = Pn(_x + offsetX, _y + offsetY, width);
int _tx = target % width,
if (_tx < 0 || _ty < 0 || _tx >= width || _ty >= height) continue;
if(!processed.Add(target))
source[target] = replacementColor;