Skip to content

Instantly share code, notes, and snippets.

@spilledmilk
Created January 3, 2016 22:58
Show Gist options
  • Select an option

  • Save spilledmilk/37c75d1740035f77d1d6 to your computer and use it in GitHub Desktop.

Select an option

Save spilledmilk/37c75d1740035f77d1d6 to your computer and use it in GitHub Desktop.

Revisions

  1. Jane Lundgren created this gist Jan 3, 2016.
    61 changes: 61 additions & 0 deletions imgblur-refactor.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    class Image
    attr_accessor :array

    def initialize(array)
    @array = array
    end

    def blur(distance = 1)
    distance.times do
    transform
    end
    end

    def output_image
    @array.each {|item| puts "#{item.join()}"}
    end

    private

    def transform
    # length of one secondary array
    width = array[0].length

    # number of secondary arrays (rows)
    height = array.length

    ones_array = []

    array.each_with_index do |row, y|
    row.each_with_index do |value, x|
    ones_array << [x, y] if value == 1
    end
    end

    ones_array.each do |coordinate|
    x, y = coordinate

    array[y][x + 1] = 1 unless (x + 1 >= width)
    array[y][x - 1] = 1 unless (x - 1 < 0)
    array[y + 1][x] = 1 unless (y + 1 >= height)
    array[y - 1][x] = 1 unless (y - 1 < 0)
    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, 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]
    ])

    image.blur(3)
    image.output_image