Skip to content

Instantly share code, notes, and snippets.

@MasoodGit
Last active December 19, 2015 01:33
Show Gist options
  • Save MasoodGit/b01c2f963b9c8cdcd75b to your computer and use it in GitHub Desktop.
Save MasoodGit/b01c2f963b9c8cdcd75b to your computer and use it in GitHub Desktop.
image_blur#1
class Image
def initialize(data)
@data = data
end
def output_image
@data.each do |row|
str = ""
row.each do |col|
str += col.inspect
end
puts str
end
end
def output_image_map
puts @data.map{ |row| row.join("") }.join("\n")
end
end
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
#image.output_image
image.output_image_map
#class Image
class Image
def initialize(data)
@data = data
end
def output_image_map
puts @data.map{ |row| row.join("") }.join("\n")
end
def image_blur
pixels_with_one=[]
rows = @data.length
cols = @data[0].length
@data.each_with_index { |row,x|
row.each_with_index { |col,y|
if col == 1
pixels_with_one << [x, y]
end
}
}
pixels_with_one.each do |point|
x = point[0]
y = point[1]
if x - 1 >= 0
@data[x-1][y] = 1
end
if x + 1 < rows
@data[x+1][y] = 1
end
if y - 1 >= 0
@data[x][y-1] = 1
end
if y + 1 < cols
@data[x][y+1] = 1
end
end
puts @data.map{ |row| row.join("") }.join("\n")
end
end
#end of class
image1 = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
image1.image_blur
puts '############################# \n'
image2 = Image.new([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
])
image2.image_blur
puts '############################# \n'
image3 = Image.new([
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
])
image3.image_blur
puts '############################# \n'
image4 = Image.new([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 0]
])
image4.image_blur
puts '############################# \n'
image5 = Image.new([
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0]
])
image5.image_blur
@binhngoc17
Copy link

  • output_image_map should be call at the end of the image_blur call
  • Does the spec require you to not modifying the current image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment