Created
June 30, 2017 00:09
-
-
Save pdbradley/7d87b4e959a761783a2233177b992939 to your computer and use it in GitHub Desktop.
blur3.rb
This file contains hidden or 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
| require 'pry' | |
| class Image | |
| def initialize(image) | |
| @image = Marshal.load(Marshal.dump(image)) | |
| end | |
| def output_image | |
| @image.each do |x| | |
| puts x.join | |
| end | |
| end | |
| def blur(distance) | |
| distance.times do | |
| pixels_to_blur_from.each do |x_y_pair| | |
| blur_pixel(x_y_pair[0],x_y_pair[1]) | |
| end | |
| end | |
| end | |
| def blur_pixel(x, y) | |
| blur_candidates = pixel_neighbors(x,y) | |
| blur_candidates.each do |tuple| | |
| if blurrable?(tuple[0], tuple[1]) | |
| @image[tuple[0]][tuple[1]] = 1 | |
| end | |
| end | |
| end | |
| def pixels_to_blur_from | |
| result = [] | |
| @image.each_with_index do |i, row_index| | |
| i.each_with_index do |a, col_index| | |
| if a == 1 | |
| result << [row_index, col_index] | |
| end | |
| end | |
| end | |
| result | |
| end | |
| def blurrable?(x,y) | |
| pixel_east_of_the_edge?(x) && pixel_west_of_the_edge?(x) && | |
| pixel_higher_than_edge?(y) && pixel_lower_than_edge?(y) | |
| end | |
| def pixel_neighbors(x,y) | |
| [ | |
| [x+1,y], | |
| [x-1,y], | |
| [x,y+1], | |
| [x,y-1] | |
| ] | |
| end | |
| def pixel_east_of_the_edge?(x) | |
| x <= max_columns | |
| end | |
| def pixel_west_of_the_edge?(x) | |
| x >= 0 | |
| end | |
| def pixel_higher_than_edge?(y) | |
| y <= max_rows | |
| end | |
| def pixel_lower_than_edge?(y) | |
| y >= 0 | |
| end | |
| def max_rows | |
| @image.length - 1 | |
| end | |
| def max_columns | |
| @image.first.length - 1 | |
| 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, 0, 0, 0], | |
| [0, 0, 0, 0, 0, 0, 1] | |
| ]) | |
| image.output_image | |
| puts "" | |
| image.blur(5) | |
| image.output_image | |
| # def touples(n) | |
| # s = n + 1 | |
| # touple_values = [] | |
| # s.times do |x| | |
| # s.times do |y| | |
| # touple_values.push([x, y]) | |
| # end | |
| # end | |
| # filtered_array = [] | |
| # touple_values.each do |touple| | |
| # # touple = [1, 1] | |
| # x, y = touple | |
| # if x + y <= n | |
| # filtered_array.push([x, y]) | |
| # filtered_array.push([-x, y]) | |
| # filtered_array.push([x, -y]) | |
| # filtered_array.push([-x, -y]) | |
| # end | |
| # end | |
| # return filtered_array | |
| # end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment