Last active
August 29, 2015 14:26
-
-
Save vincentchin/d131b76616433268a2e1 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 Image | |
def initialize(array) | |
@array = array | |
@new_array = @array.map {|e| Array.new(e.size) } | |
end | |
def blur | |
@array.each_with_index do |row,x| | |
row.each_with_index do |cell,y| | |
last_x_pixel = row.size - 1 | |
last_y_pixel = @array.size - 1 | |
if @array[x][y] == 1 | |
@new_array[x][y] ||= 1 | |
@new_array[x-1][y] = 1 if x != 0 | |
@new_array[x][y-1] = 1 if y != 0 | |
@new_array[x][y+1] = 1 if y != last_y_pixel | |
@new_array[x+1][y] = 1 if x != last_x_pixel | |
else | |
@new_array[x][y] ||= @array[x][y] | |
end | |
end | |
end | |
return Image.new(@new_array).output_image | |
end | |
def output_image | |
@array.each do |i| | |
puts i.join("") | |
end | |
end | |
end | |
image = Image.new([ | |
[0, 0, 0, 0], | |
[0, 0, 1, 0], | |
[0, 0, 0, 0], | |
[1, 1, 0, 0] | |
]) | |
image.blur | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment