0
私はImage Blurという問題に取り組んでいます。私は自分のコードを2D配列からアイテム(1sか0s)を取って、1つごとに隣接する0を1に変更する必要があります。これまでのところ、私のコードはこれを最初の1に渡していますが、何らかの理由で他のものよりもループしません。このループをどのようにして各項目に適用するのですか?
class Image
def initialize(image)
@values = image
end
def find_ones
ones = []
@values.each_with_index do |row, row_index|
row.each_with_index do |pixel, column_index|
if pixel == 1
coord = [row_index, column_index]
ones << coord
end
puts "#{pixel} #{row_index} #{column_index}" if pixel == 1
end
end
ones
end
def transform
ones = find_ones
ri = ones[0][0]
ci = ones[0][1]
ones.each do
@values[ri + 1][ci] = 1 if (ri + 1) <= 3
@values[ri - 1][ci] = 1 if (ri - 1) >= 0
@values[ri][ci + 1] = 1 if (ci + 1) <= 3
@values[ri][ci - 1] = 1 if (ci - 1) >= 0
end
end
def output_image
@values.each do |row|
puts row.join
end
end
end
image = Image.new([
[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0]
])
image.transform
image.output_image
あらかじめご協力いただきありがとうございます。