-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathFloodFill.cs
More file actions
190 lines (163 loc) · 6.21 KB
/
FloodFill.cs
File metadata and controls
190 lines (163 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
namespace Graphics
{
abstract class FloodFill
{
// A simple point on the 2 dimensional integer plane.
private class Point2I
{
// Coordinates
public int x;
public int y;
public Point2I(int x, int y)
{
this.x = x;
this.y = y;
}
public override bool Equals(object o)
{
if(!(o is Point2I other))
return false;
return this.x.Equals(other.x) && this.y.Equals(other.y);
}
public override int GetHashCode()
{
int hash = 17;
hash = 31 * hash + x;
hash = 31 * hash + y;
return hash;
}
}
// Utility object for comparing List<List<float>> objects.
private class FloatListEqualityComparer : IEqualityComparer<List<float>>
{
public bool Equals(List<float> one, List<float> two)
{
if (ReferenceEquals(one, two))
return true;
if (one == null || two == null)
return false;
return one.SequenceEqual(two);
}
public int GetHashCode(List<float> list) {
return list.GetHashCode();
}
}
private static bool InBounds(Point2I size, Point2I loc)
{
if (loc.x < 0 || loc.y < 0) {
return false;
}
return loc.x < size.x && loc.y < size.y;
}
private static List<Point2I> FindNeighbors(ref List<List<float>> grid, Point2I loc, float oldValue)
{
var possibleNeighbors = new List<Point2I>
{
new Point2I(loc.x, loc.y + 1),
new Point2I(loc.x + 1, loc.y),
new Point2I(loc.x, loc.y - 1),
new Point2I(loc.x - 1, loc.y)
};
var neighbors = new List<Point2I>();
foreach (Point2I possibleNeighbor in possibleNeighbors)
{
var size = new Point2I(grid[0].Count, grid.Count);
var x = possibleNeighbor.x;
var y = possibleNeighbor.y;
if (!InBounds(size, possibleNeighbor)) {
continue;
}
if (grid[x][y].Equals(oldValue)) {
neighbors.Add(possibleNeighbor);
}
}
return neighbors;
}
private static void RecursiveFill(ref List<List<float>> grid, Point2I loc, float oldValue, float newValue)
{
if (oldValue.Equals(newValue)) {
return;
}
grid[loc.x][loc.y] = newValue;
var possibleNeighbors = FindNeighbors(ref grid, loc, oldValue);
foreach (Point2I possibleNeighbor in possibleNeighbors) {
RecursiveFill(ref grid, possibleNeighbor, oldValue, newValue);
}
}
private static void QueueFill(ref List<List<float>> grid, Point2I loc, float oldValue, float newValue)
{
if (oldValue.Equals(newValue)) {
return;
}
var queue = new Queue<Point2I>();
queue.Enqueue(loc);
grid[loc.x][loc.y] = newValue;
while (queue.Count > 0)
{
var currentLoc = queue.Dequeue();
var possibleNeighbors = FindNeighbors(ref grid, currentLoc, oldValue);
foreach (Point2I neighbor in possibleNeighbors)
{
grid[neighbor.x][neighbor.y] = newValue;
queue.Enqueue(neighbor);
}
}
}
private static void StackFill(ref List<List<float>> grid, Point2I loc, float oldValue, float newValue)
{
if (oldValue.Equals(newValue)) {
return;
}
var stack = new Stack<Point2I>();
stack.Push(loc);
while (stack.Count > 0)
{
var currentLoc = stack.Pop();
var x = currentLoc.x;
var y = currentLoc.y;
if (grid[x][y].Equals(oldValue))
{
grid[x][y] = newValue;
var possibleNeighbors = FindNeighbors(ref grid, currentLoc, oldValue);
foreach (Point2I neighbor in possibleNeighbors) {
stack.Push(neighbor);
}
}
}
}
private static void Main(string[] args)
{
// All neighbouring zeros, adjacent to (1, 1), must be replaced with 3 in the end result.
var grid = new List<List<float>>
{
new List<float>(){0, 0, 1, 0, 0},
new List<float>(){0, 0, 1, 0, 0},
new List<float>(){0, 0, 1, 0, 0},
new List<float>(){8, 0, 1, 1, 1},
new List<float>(){0, 0, 0, 0, 0}
};
var solutionGrid = new List<List<float>>
{
new List<float>(){3, 3, 1, 0, 0},
new List<float>(){3, 3, 1, 0, 0},
new List<float>(){3, 3, 1, 0, 0},
new List<float>(){8, 3, 1, 1, 1},
new List<float>(){3, 3, 3, 3, 3}
};
var startingPoint = new Point2I(1, 1);
var gridComparator = new FloatListEqualityComparer();
var testGrid = new List<List<float>>(grid);
RecursiveFill(ref testGrid, startingPoint, 0, 3);
Debug.Assert(testGrid.SequenceEqual(solutionGrid, gridComparator), "Recursive Flood Fill Failed");
testGrid = new List<List<float>>(grid);
QueueFill(ref testGrid, startingPoint, 0, 3);
Debug.Assert(testGrid.SequenceEqual(solutionGrid, gridComparator), "Queue Flood Fill Failed");
testGrid = new List<List<float>>(grid);
StackFill(ref testGrid, startingPoint, 0, 3);
Debug.Assert(testGrid.SequenceEqual(solutionGrid, gridComparator), "Stack Flood Fill Failed");
}
}
}