Skip to content

Instantly share code, notes, and snippets.

@pdbradley
Created December 16, 2016 22:12
Show Gist options
  • Save pdbradley/8861aefd699ead6494062358c2cf3eda to your computer and use it in GitHub Desktop.
Save pdbradley/8861aefd699ead6494062358c2cf3eda to your computer and use it in GitHub Desktop.
image_blur2.rb
require 'pry'
class Image
def initialize(image)
@image = image
end
def output_image
@image.each do |row|
row.each do |cell|
print cell # always puts a newline
end
puts ""
end
end
def empty_image
new_array = []
image_height.times do
row_of_zeros = []
image_width.times do
row_of_zeros << 0
end
new_array << row_of_zeros
end
new_array
end
def blur
new_image = empty_image
@image.each_index do |y|
@image[y].each_index do |x|
if @image[y][x] == 1
new_image[y][x] = 1
blur_around_this_pixel(new_image, y, x)
end
end
end
@image = new_image
end
def blur_around_this_pixel(image, y, x)
unless too_far_up?(y-1)
image[y-1][x] = 1
end
unless too_far_down?(y+1)
image[y+1][x] = 1
end
unless too_far_left?(x-1)
image[y][x-1] = 1
end
unless too_far_right?(x+1)
image[y][x+1] = 1
end
end
def image_width
@image.first.size # image.first grabs the first row; that's all we need to know the width
end
def image_height
@image.size # this is actually counting the number of rows in the image
end
def too_far_right?(column_coordinate)
column_coordinate >= image_width
end
def too_far_left?(column_coordinate)
column_coordinate < 0
end
def too_far_down?(row_coordinate)
row_coordinate >= image_height
end
def too_far_up?(row_coordinate)
row_coordinate < 0
end
end
# image = Image.new([
# [0, 0, 0, 0],
# [0, 1, 0, 0],
# [0, 0, 0, 1],
# [0, 0, 0, 0]
# ])
image = Image.new([
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]
])
# image = Image.new([
# [0, 0, 0, 0, 1],
# [0, 1, 0, 0, 0],
# [0, 0, 0, 1, 0],
# [0, 0, 0, 0, 0]
# ])
image.output_image
binding.pry
puts "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment