Skip to content

Instantly share code, notes, and snippets.

@framallo
Forked from vincentchin/imageblur.rb
Last active August 29, 2015 14:26
Show Gist options
  • Save framallo/dc92320031c34410aca4 to your computer and use it in GitHub Desktop.
Save framallo/dc92320031c34410aca4 to your computer and use it in GitHub Desktop.
class Image
attr_accessor :array
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
def blur(n)
@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-n][y] = 1 if x != 0
@new_array[x][y-n] = 1 if y != 0
@new_array[x][y+n] = 1 if y != last_y_pixel
@new_array[x+n][y] = 1 if x != last_x_pixel
else
@new_array[x][y] ||= @array[x][y]
end
end
end
return Image.new(@new_array)
end
def square(x,y,radius)
#puts step(x,y,radius,radius).inspect
#puts step(x,y,-radius,-radius).inspect
#puts step(x,y,+radius,-radius).inspect
#puts step(x,y,-radius,+radius).inspect
walk(*step(x,y,-radius,-radius), *step(x,y,+radius,-radius)) +
[
[0,1],step(x,y,-radius),[0,3],
[4,1],step(x,y,+radius),[4,3],
[0,4],[1,4],step(x,y,0,+radius),[3,4],[4,4]
]
end
def walk(x,y,x2,y2)
delta_x = x2 - x
delta_y = y2 - x
result = []
for i in x..x2
for j in (y..y2)
result << [i,j]
end
end
return result
end
def step(x,y,delta_x=0,delta_y=0)
last_x_pixel = @array.first.size - 1
last_y_pixel = @array.size - 1
x2 = x+delta_x
y2 = y+delta_y
[x2,y2] if x2 <= last_x_pixel && y2 <= last_y_pixel
end
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
require 'rspec'
require_relative 'imageblur'
describe Image do
describe '#blur' do
it 'adds a rectangle replaceing cells content with 1 and with radius 2' do
image = Image.new([
[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]
])
new_image = Image.new([
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 1, 0, 1],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0]
])
expect(image.blur(2).array).to eql(new_image.array)
end
describe '#square' do
it 'returns the cell coordenates of the square' do
image = Image.new([
[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]
])
square = [
[0,0],[1,0],[2,0],[3,0],[4,0],
[0,1],[0,2],[0,3],
[4,1],[4,2],[4,3],
[0,4],[1,4],[2,4],[3,4],[4,4]
]
expect(image.square(2,2,2)).to eq(square)
end
end
describe '#step' do
it 'returns the next cell on the right' do
cell = Image.new([
[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]
]).step(2,2,1)
expect(cell).to eq([3,2])
end
context 'when you arrive the limit' do
it 'returns 3 cells on the right' do
cell = Image.new([
[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]
]).step(2,2,3)
expect(cell).to eq(nil)
end
end
end
describe '#walk' do
it 'walks in straight line' do
path = Image.new([
[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]
]).walk(2,2,4,2)
expect(path).to eq([[2,2],[3,2],[4,2]])
end
it 'walks in another straight line' do
path = Image.new([
[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]
]).walk(0,0,4,0)
expect(path).to eq([[0,0],[1,0],[2,0],[3,0],[4,0]])
end
end
end
end
require 'rspec'
require_relative 'imageblur'
describe Image do
describe 'output_image' do
it 'returns the correct output on output_image' do
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
output = "0100\n1111\n0111\n0001\n"
expect { image.output_image }.to output(output).to_stdout
end
end
describe 'blur' do
it 'adds 1 around an array with a 1 on a single cell' do
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
])
new_image = Image.new([
[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]
])
expect(image.blur.array).to eql(new_image.array)
end
it 'adds 1 to a border cell'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment