Created
November 24, 2015 22:08
-
-
Save jeremyf/f35e81028f3fd1db71b5 to your computer and use it in GitHub Desktop.
image_blur_3.rb
This file contains 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 Image | |
def initialize(two_dimensional_array, marker = 1) | |
@data = two_dimensional_array | |
@marker = marker | |
end | |
attr_reader :data, :marker | |
def output_image | |
data.each { |row| puts row.join } | |
end | |
def blur(distance) | |
coordinates_to_blur = [] | |
coordinates_to_start_blurring_out_from do |row_index_to_blur_out_from, column_index_to_blur_out_from| | |
# puts "#{row_index_to_blur_out_from},#{column_index_to_blur_out_from}" | |
coordinates_that_could_be_blurred_based_on( | |
row_index: row_index_to_blur_out_from, column_index: column_index_to_blur_out_from, distance: distance | |
) do |row_index, column_index| | |
coordinates_to_blur << [row_index, column_index] if coordinates_are_in_the_image?(row_index: row_index, column_index: column_index) | |
end | |
end | |
coordinates_to_blur.each do |row_index, column_index| | |
data[row_index][column_index] = 1 | |
end | |
end | |
private | |
def coordinates_to_start_blurring_out_from | |
data.each_with_index do |row, row_index| | |
row.each_with_index do |cell, column_index| | |
yield(row_index, column_index) if cell == marker | |
end | |
end | |
end | |
def coordinates_that_could_be_blurred_based_on(row_index:, column_index:, distance:) | |
(-1 * distance..distance).each do |delta_row_index| | |
(-1 * distance..distance).each do |delta_column_index| | |
next if delta_row_index.abs + delta_column_index.abs > distance | |
yield(row_index + delta_row_index, column_index + delta_column_index) | |
end | |
end | |
end | |
def coordinates_are_in_the_image?(row_index:, column_index:) | |
return false if row_index < 0 | |
return false if column_index < 0 | |
return false if data[row_index].nil? | |
return false if data[row_index][column_index].nil? | |
return true | |
end | |
end | |
image = Image.new([ | |
[1,0,0,0,0,0,0,0,1], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0], | |
[1,0,0,0,0,0,0,0,1] | |
]) | |
image.blur(3) | |
image.output_image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment