Skip to content

Instantly share code, notes, and snippets.

@umasenthil
Created November 27, 2015 23:58
Show Gist options
  • Save umasenthil/ac793ee41f1bb444bdb0 to your computer and use it in GitHub Desktop.
Save umasenthil/ac793ee41f1bb444bdb0 to your computer and use it in GitHub Desktop.
class Image
def initialize(darray)
@arr = darray
end
def get_one_indices
# Get one indices
@ones_indices = []
@arr.each_index do |i|
@arr.each_index do |j|
if @arr[i][j] == 1
temp = [i,j]
@ones_indices.push(temp)
end
end
end
return
end
def one_radius_change(i, j)
#change left position to 1
if (j != 0)
#puts "change left position to 1"
left = [i, j-1]
@arr[i][j-1] = 1
end
#change top position to 1
if (i != 0)
#puts "change top position to 1"
@arr[i-1][j] = 1
end
#change right position to 1
if (i != @width-1)
#puts "change right position to 1"
@arr[i+1][j] = 1
end
#change bottom position to 1
if (j != @height-1)
# puts "change bottom position to 1"
@arr[i][j+1] = 1
end
end
def blur(distance)
str = []
@height = @arr.length
@width = @arr[0].length
get_one_indices
# Modify the elements around the indeces
@ones_indices.each_index do |n|
root = @ones_indices[n]
# one_radius_change of root
#puts "i of the location where there is 1 #{index_of_one[0]} #{index_of_one[1]}"
i = root[0]
j = root[1]
one_radius_change(i,j)
# Basically n - 1 loop top of root
top_index = i
distance.times do
top_index = top_index - 1
if(top_index >= 0)
one_radius_change(top_index,j)
else
break
end
end
# Basically n - 1 loop bottom of root
bottom_index = i
distance.times do
bottom_index = bottom_index+1
if (bottom_index < @height)
one_radius_change(bottom_index,j)
else
break
end
end
# Basically n - 1 loop right of root
right_index = j
distance.times do
right_index = right_index+1
if (right_index < @width)
one_radius_change(i,right_index)
else
break
end
end
# Basically n - 1 loop left of root
left_index = j
distance.times do
left_index = left_index-1
if (left_index >= 0)
one_radius_change(i,left_index)
else
break
end
end
end
end
def print
@arr.each do |sub_array|
puts sub_array.join('')
end
end
end
image = Image.new([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[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, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
])
image.blur(2)
image.print
@umasenthil
Copy link
Author

Hi Chad,
I completed img_blur3. It looks lot like procedural programming language. But it works.
I will try to use more ruby syntax.
Thank you

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