Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. module BlurImage
  2.  
  3. def blur
  4. table2 = []
  5. @table.each_with_index do |a, k|
  6. table2[k] = []
  7. a.each do |b|
  8. table2[k] << b
  9. end
  10. end
  11.  
  12. table2.each_with_index do |x, i|
  13. x.each_with_index do |y, j|
  14. if y == 1
  15. @table[i][j + 1] = 1 if j + 1 < @num_of_columns
  16. @table[i][j - 1] = 1 if j - 1 >= 0
  17. @table[i + 1][j] = 1 if i + 1 < @num_of_rows
  18. @table[i - 1][j] = 1 if i - 1 >= 0
  19. end
  20. end
  21. end
  22. end
  23.  
  24. def manhattan(n)
  25. n.times do
  26. self.blur
  27. end
  28. end
  29. end
  30. # instance variables are used across the entire class. don't need it for a single method.
  31. class Image
  32. attr_accessor :table, :n
  33. include BlurImage
  34.  
  35. def initialize(table)
  36. @table = table
  37. @num_of_columns = table[0].length
  38. @num_of_rows = table.length
  39. end
  40.  
  41. def output_image
  42. @table.each do |x|
  43. x.each do |y|
  44. print y
  45. end
  46. puts
  47. end
  48. end
  49. end
  50.  
  51.  
  52. image = Image.new([
  53. [0, 0, 0, 0, 0, 0],
  54. [0, 0, 0, 0, 0, 0],
  55. [0, 0, 0, 1, 0, 0],
  56. [0, 0, 0, 0, 0, 0],
  57. [0, 0, 0, 0, 0, 0],
  58. [0, 0, 0, 0, 0, 0],
  59. [0, 1, 0, 0, 0, 0],
  60. [0, 0, 0, 0, 0, 1]
  61. ])
  62.  
  63. image.output_image
  64.  
  65. puts
  66.  
  67. image.manhattan(2)
  68.  
  69. image.output_image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement