Skip to content

Instantly share code, notes, and snippets.

@framallo
Created July 1, 2016 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save framallo/594245ce06f7d6d4484680ffc24b6f2e to your computer and use it in GitHub Desktop.
Save framallo/594245ce06f7d6d4484680ffc24b6f2e to your computer and use it in GitHub Desktop.
class Image
attr_accessor :original_image, :image
def initialize(image)
@original_image = image
end
def image
@image ||= @original_image.dup
end
def blur
cells_with_one.each do |row, column|
blur_one_cell row, column
end
self
end
# problems:
# 1. add 1 to a cell
# am I within the cell?
# can I write a 1 here?
# 2. blur one cell given an x and y
# 3. find all cells with number one
# 4. find all cells with one and blur them
def write(row,column,value = 1)
if row >= 0 && column >= 0 && image.length > 0 && row < image.length && column < image[0].length
image[row][column] = value
end
end
def blur_one_cell(row, column)
write row, column
write row + 1, column
write row - 1, column
write row, column + 1
write row, column - 1
self
end
def cells_with_one
cells_with_one = []
image.each_with_index do |row, row_index|
row.each_with_index do |column, column_index|
if image[row_index][column_index] == 1
cells_with_one << [row_index, column_index]
end
end
end
cells_with_one
end
def output_image
image.each_with_index do |row, row_index|
puts row.join
end
end
private
def blur_pixels(row_index,column_index)
if @image[row_index][column_index] ==1 && @image_copy[row_index][column_index] == 0
@image_copy[row_index][column_index] = 1
@image_copy[row_index + 1][column_index] = 1
@image_copy[row_index - 1][column_index] = 1
@image_copy[row_index][column_index + 1] = 1
@image_copy[row_index][column_index - 1] = 1
end
end
end
require_relative 'blur2'
require 'pp'
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
])
# image.blur
# image.output_image
##this only works for blurs that don't overlap :/ How do I correct it to work with overlapping blurs?
# 6 elements, row can be up to 5 because it starts on 0
puts 'image.write(0,0,1) == 1'
puts image.write(0,0,1) == 1
puts 'image.write(6,0,1) == nil'
puts image.write(6,0,1) == nil
# expect(image.write(6,0,1)).to eq(nil)
puts 'image.write(0,6,1) == nil'
puts image.write(0,6,1) == nil
puts 'image.write(-1,-1,1) == nil'
puts image.write(-1,-1,1) == nil
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],
])
puts 'image.blur_one_cell(1,1)'
puts image.blur_one_cell(1,1)
image.output_image
puts image.image == [
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]
# 0100
# 1110
# 0100
# 0000
# 0000
# 0000
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],
])
puts 'image.blur_one_cell(1,1)'
puts image.blur_one_cell(0,0)
image.output_image
puts image.image == [
[1, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]
# 0100
# 1110
# 0100
# 0000
# 0000
# 0000
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
])
puts 'cells_with_one'
pp image.cells_with_one
puts image.cells_with_one == [
[1,1],
[4,2]
]
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
])
puts 'blur'
image.blur
puts image.image == [
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 1, 1, 1],
[0, 0, 1, 0],
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment