Created
January 20, 2014 01:22
-
-
Save octosteve/8513445 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Grid | |
| attr_reader :coordinate_list | |
| def initialize(coordinate_list) | |
| @coordinate_list = coordinate_list | |
| end | |
| def find_all_matches(x, y) | |
| find_target_pixels(x,y).each do |t_x, t_y| | |
| if eligable_for_painting?([t_x, t_y], [x,y]) | |
| matches << [t_x, t_y] | |
| find_all_matches(t_x, t_y) | |
| end | |
| end | |
| end | |
| def eligable_for_painting?(q_coords, coords) | |
| value_at(q_coords) == value_at(coords) && !matches.include?(q_coords) | |
| end | |
| def find_target_pixels(x, y) | |
| [x-1, x, x+1].product([y-1, y, y+1]) | |
| end | |
| def value_at(coords) | |
| coordinate_list[coords[0]][coords[1]] | |
| end | |
| def matches | |
| @matches ||= [] | |
| end | |
| def update_matches(color) | |
| matches.each do |x, y| | |
| coordinate_list[x][y] = color | |
| end | |
| end | |
| def apply_color(fill_type, x, y, color) | |
| find_all_matches(x-1, y-1) | |
| update_matches(color) | |
| end | |
| end | |
| class Image | |
| def initialize(width, height, pixels) | |
| @width = width | |
| @height = height | |
| @pixels = pixels | |
| end | |
| def flood_fill(x, y, color) | |
| grid.apply_color(:flood, x, y, color) | |
| end | |
| def display | |
| grid.coordinate_list | |
| end | |
| private | |
| def grid | |
| @grid ||= Grid.new(grouped_pixels) | |
| end | |
| def grouped_pixels | |
| @pixels.each_slice(@width).to_a | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sandi Metz would be proud.