Sha256: a1454aa3ef628d54490fe132e2bee66f90d30e9b2b40a9777faec1257024ed17

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'bitmapped/commands/base_command'
require 'bitmapped/commands/commands_helper'
require 'bitmapped/exceptions'

module Bitmapped
  module Commands
    class FillCommand < BaseCommand
      include CommandsHelper

      def command_id
        "F"
      end

      def process_command(bitmap, input)
        Validators::ValidateBitmapInitialised.parse_and_validate(bitmap)
        column, row, color = Validators::ValidateFillInput.parse_and_validate(input)
        fill_command(bitmap, column, row, color)
      end

      private
        def fill_command(bitmap, x, y, replacement_color)
          x, y = coordinates_to_array_indexes(bitmap, x, y)
          target_color = bitmap.pixels[x][y]
          queue = [[x,y]]

          until queue.empty?
            x, y = queue.pop
            next if (!valid_cooridinates(bitmap, x+1, y+1) || bitmap.pixels[x][y] != target_color)
            bitmap.pixels[x][y] = replacement_color
            queue << [x+1, y] # east
            queue << [x-1, y] # west
            queue << [x, y+1] # south
            queue << [x, y-1] # north

            # and if we are doing 8-direction flood-fill...
            # queue << [x+1, y-1] # north-east
            # queue << [x-1, y-1] # north-west
            # queue << [x+1, y+1] # south-east
            # queue << [x-1, y+1] # south-west
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bitmapped-0.2.0 lib/bitmapped/commands/fill_command.rb