Last active
February 20, 2016 15:34
-
-
Save isomorpheric/8947d61714fd74b50ea1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Image Blur #3 | |
class Blur | |
attr_accessor :array, :blurred_array, :ones | |
def initialize(array) | |
@array = array | |
end | |
def output_image | |
@array.each do |row| | |
puts row.join(" ") | |
end | |
return nil | |
end | |
def one_finder | |
@ones = [] | |
#Cycle through each pixel, store pixel in @ones if pixel == 1 | |
@array.each.with_index do |row, row_i| | |
row.each.with_index do |pixel, col_i| | |
if pixel == 1 | |
@ones << [row_i, col_i] | |
end | |
end | |
end | |
end | |
def blur!(d) | |
one_finder | |
@ones.each do |row, col| | |
y = 0 | |
max = d | |
while (y <= max) | |
x = d | |
while x >= 0 | |
@array[row + y][col - x] = 1 if ((row + y) < (@array.length - 1)) && ((col - x) >= 0) | |
@array[row + y][col + x] = 1 if ((row + y) < (@array.length - 1)) && ((col + x) < @array[0].length - 1) | |
@array[row - y][col - x] = 1 if ((row - y) >= 0) && ((col - x) >= 0) | |
@array[row - y][col + x] = 1 if ((row - y) >= 0) && ((col + x) <= @array[0].length - 1) | |
x -= 1 | |
end | |
d -= 1 | |
y += 1 | |
end | |
d = max #Resets distance | |
end | |
output_image | |
end | |
end | |
image = Blur.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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 1, 0, 0, 0, 0, 0, 0, 0], | |
]) | |
image.blur!(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment