Skip to content

Commit b689488

Browse files
authored
Flood fill chapter (#738)
* adding rough draft of flood fill chapter * adding video to chapter
1 parent b537da3 commit b689488

17 files changed

+735
-0
lines changed

SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
* [Split-Operator Method](contents/split-operator_method/split-operator_method.md)
3737
* [Data Compression](contents/data_compression/data_compression.md)
3838
* [Huffman Encoding](contents/huffman_encoding/huffman_encoding.md)
39+
* [Computer Graphics](contents/computer_graphics/computer_graphics.md)
40+
* [Flood Fill](contents/flood_fill/flood_fill.md)
3941
* [Quantum Information](contents/quantum_information/quantum_information.md)
4042
* [Computus](contents/computus/computus.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Computer Graphics
2+
3+
Of all areas of computer science research, none have had more of an immediate impact on multimedia than computer graphics.
4+
This sub-field is distinctly different than computational science in that it focuses on the *appearance* of realistic details, instead of computing those details precisely.
5+
Where a computational scientist might spend years writing software that runs on the fastest computers known to man to simulate climate, the computer graphics researcher might apply machine learning to create fluid simulations that look good enough to the untrained eye.
6+
In the end, the computational scientist will have a plot and the computer graphics researcher will have a beautifully rendered simulation.
7+
8+
Though I may have painted computer graphics to be a bit hand-wavey, that could not be further from the truth!
9+
Instead, I would argue that this field of research provides the closest approximation to realistic visualizations that desktop hardware can currently support.
10+
Many art and video game studios are interested in telling a complete story via computational media, and this simply would not be possible without the rigorous efforts of researchers from around the world.
11+
This is why Pixar hires researchers and will actively publish their findings after their movies are released.
12+
13+
Though the boundary between computer science research fields is a bit vague, for the purposes of the Algorithm Archive, we will broadly classify computer graphics as anything with direct applications to images or fields that can be represented as images.
14+
Convolutions, for example, would not be considered part of computer graphics because they are used widely in all areas of computer science research; however, Canny edge detection will be.
15+
We will also be covering a wide range of applications that are used for rendering high-resolution graphics and computational art.
16+
17+
As with all sections to the Algorithm Archive, this is a work in progress and subject to change, so feel free to let me know what you think!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using DataStructures
2+
using Test
3+
4+
# Function to check to make sure we are on the canvas
5+
function inbounds(canvas_size, loc)
6+
7+
# Make sure we are not beneath or to the left of the canvas
8+
if minimum(Tuple(loc)) < 1
9+
return false
10+
11+
# Make sure we are not to the right of the canvas
12+
elseif loc[2] > canvas_size[2]
13+
return false
14+
15+
# Make sure we are not above the canvas
16+
elseif loc[1] > canvas_size[1]
17+
return false
18+
else
19+
return true
20+
end
21+
end
22+
23+
function color!(canvas, loc::CartesianIndex, old_val, new_val)
24+
# bounds check, do not color if no in bounds!
25+
if !inbounds(size(canvas), loc)
26+
return
27+
end
28+
29+
# Only change color if the element value is the old value
30+
if (canvas[loc] != old_val)
31+
return
32+
else
33+
canvas[loc] = new_val
34+
end
35+
end
36+
37+
function find_neighbors(canvas, loc::CartesianIndex, old_val, new_val)
38+
39+
# Finding north, south, east, west neighbors
40+
possible_neighbors = [loc + CartesianIndex(0, 1),
41+
loc + CartesianIndex(1, 0),
42+
loc + CartesianIndex(0, -1),
43+
loc + CartesianIndex(-1, 0)]
44+
45+
# Exclusing neighbors that should not be colored
46+
neighbors = []
47+
for possible_neighbor in possible_neighbors
48+
if inbounds(size(canvas), possible_neighbor) &&
49+
canvas[possible_neighbor] == old_val
50+
push!(neighbors, possible_neighbor)
51+
end
52+
end
53+
54+
return neighbors
55+
end
56+
57+
function stack_fill!(canvas, loc::CartesianIndex, old_val, new_val)
58+
if new_val == old_val
59+
return
60+
end
61+
62+
s = Stack{CartesianIndex}()
63+
push!(s, loc)
64+
65+
while length(s) > 0
66+
current_loc = pop!(s)
67+
if canvas[current_loc] == old_val
68+
color!(canvas, current_loc, old_val, new_val)
69+
possible_neighbors = find_neighbors(canvas, current_loc,
70+
old_val, new_val)
71+
for neighbor in possible_neighbors
72+
push!(s,neighbor)
73+
end
74+
end
75+
76+
end
77+
end
78+
79+
80+
function queue_fill!(canvas, loc::CartesianIndex, old_val, new_val)
81+
if new_val == old_val
82+
return
83+
end
84+
85+
q = Queue{CartesianIndex}()
86+
enqueue!(q, loc)
87+
88+
# Coloring the initial location
89+
color!(canvas, loc, old_val, new_val)
90+
91+
while length(q) > 0
92+
current_loc = dequeue!(q)
93+
94+
possible_neighbors = find_neighbors(canvas, current_loc,
95+
old_val, new_val)
96+
97+
# Coloring as we are enqueuing neighbors
98+
for neighbor in possible_neighbors
99+
color!(canvas, neighbor, old_val, new_val)
100+
enqueue!(q,neighbor)
101+
end
102+
103+
end
104+
end
105+
106+
function recursive_fill!(canvas, loc::CartesianIndex, old_val, new_val)
107+
108+
if (old_val == new_val)
109+
return
110+
end
111+
112+
color!(canvas, loc, old_val, new_val)
113+
114+
possible_neighbors = find_neighbors(canvas, loc, old_val, new_val)
115+
for possible_neighbor in possible_neighbors
116+
recursive_fill!(canvas, possible_neighbor, old_val, new_val)
117+
end
118+
end
119+
120+
function main()
121+
122+
# Creation of a 5x5 grid with a single row of 1.0 elements
123+
grid = zeros(5,5)
124+
grid[3,:] .= 1
125+
126+
# Create solution grid
127+
answer_grid = zeros(5,5)
128+
answer_grid[1:3, :] .= 1
129+
130+
# Start filling at 1,1
131+
start_loc = CartesianIndex(1,1)
132+
133+
@testset "Fill Methods" begin
134+
# Use recursive method and reinitialize grid
135+
recursive_fill!(grid, start_loc, 0.0, 1.0)
136+
@test grid == answer_grid
137+
138+
grid[1:2,:] .= 0
139+
140+
# Use queue method and reinitialize grid
141+
queue_fill!(grid, start_loc, 0.0, 1.0)
142+
@test grid == answer_grid
143+
144+
grid[1:2,:] .= 0
145+
146+
# Use stack method and reinitialize grid
147+
stack_fill!(grid, start_loc, 0.0, 1.0)
148+
@test grid == answer_grid
149+
end
150+
151+
end
152+
153+
main()

0 commit comments

Comments
 (0)