|
| 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