Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module BlurImage
- def blur
- table2 = []
- @table.each_with_index do |a, k|
- table2[k] = []
- a.each do |b|
- table2[k] << b
- end
- end
- table2.each_with_index do |x, i|
- x.each_with_index do |y, j|
- if y == 1
- @table[i][j + 1] = 1 if j + 1 < @num_of_columns
- @table[i][j - 1] = 1 if j - 1 >= 0
- @table[i + 1][j] = 1 if i + 1 < @num_of_rows
- @table[i - 1][j] = 1 if i - 1 >= 0
- end
- end
- end
- end
- def manhattan(n)
- n.times do
- self.blur
- end
- end
- end
- # instance variables are used across the entire class. don't need it for a single method.
- class Image
- attr_accessor :table, :n
- include BlurImage
- def initialize(table)
- @table = table
- @num_of_columns = table[0].length
- @num_of_rows = table.length
- end
- def output_image
- @table.each do |x|
- x.each do |y|
- print y
- end
- puts
- end
- end
- end
- image = Image.new([
- [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, 1, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 1]
- ])
- image.output_image
- puts
- image.manhattan(2)
- image.output_image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement