Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created February 16, 2017 21:09
Show Gist options
  • Save kenmazaika/9aec86526dd85afbc81fbe7e9e795564 to your computer and use it in GitHub Desktop.
Save kenmazaika/9aec86526dd85afbc81fbe7e9e795564 to your computer and use it in GitHub Desktop.
class Image
def initialize(image_array)
@image_array = image_array
end
def output
@image_array.each do |row|
puts row.join(' ')
end
end
def rows
rows = @image_array.count - 1
end
def columns
columns = @image_array[0].count - 1
end
def blur!(n = 1)
generate_blur_pixels(n).each do |index|
@image_array[index[0]][index[1]] = 1 if
index[0] >= 0 && index[0] <= rows && index[1] >= 0 && index[1] <= columns
end
return Image.new(@image_array)
end
private
def generate_distance_pairs(n)
distance_pair_array = []
(1..n).each do |x|
(0..x).each do |y|
z = x - y
distance_pair_array.push([y, z], [-y, -z])
distance_pair_array.push([y, -z], [-y, z]) unless y == 0 || z == 0
end
end
return distance_pair_array
end
def generate_blur_pixels(n)
distance_pair_array = generate_distance_pairs(n)
blur_pixel_array = []
@image_array.each.with_index do |row, row_index|
row.each.with_index do |pixel, column_index|
if pixel == 1
distance_pair_array.each do |delta|
blur_pixel_array.push([row_index + delta[0], column_index + delta[1]])
end
end
end
end
return blur_pixel_array
end
end
image = Image.new([
[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, 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, 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, 0, 0, 0, 0, 0, 1]
])
image_blur = image.blur!(3)
image_blur.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment