class Graphed::ImageEditor

Attributes

cols[R]
rows[R]

Public Class Methods

new(m, n, default_color = 'O') click to toggle source
Calls superclass method
# File lib/graphed/imageeditor.rb, line 6
def initialize(m, n,  default_color = 'O')
  super(n) {Array.new(m){default_color}} #Create an M * N 2 dimensional matrix with 'white'
  @cols = m
  @rows = n
end

Public Instance Methods

F(x,y, color)
Alias for: fill
H(x1, x2, y, color)
Alias for: horizontal
L(x, y, color)
Alias for: set
S()
Alias for: show
V(x, y1, y2, color)
Alias for: vertical
fill(x,y, color) click to toggle source
# File lib/graphed/imageeditor.rb, line 48
def fill(x,y, color)
  flood_fill(x, y, get(x,y), color)
end
Also aliased as: F
flood_fill(x, y, target_color, replacement_color ) click to toggle source

This recursive algorithm from Ruby Forum: www.ruby-forum.com/topic/184567

# File lib/graphed/imageeditor.rb, line 33
def flood_fill(x, y, target_color, replacement_color )
  return unless valid_on?(x,y)
  #puts "x=#{x}, y=#{y}, target_color=#{target_color}, replacement_color=#{replacement_color}, get=#{get(x,y)}"
  #show; sleep(0.2)

  return if get(x,y) != target_color
  return if get(x,y) == replacement_color

  set(x, y, replacement_color)
  flood_fill(x+1, y, target_color, replacement_color)
  flood_fill(x-1, y, target_color, replacement_color)
  flood_fill(x, y+1, target_color, replacement_color)
  flood_fill(x, y-1, target_color, replacement_color)
end
get(x, y) click to toggle source
# File lib/graphed/imageeditor.rb, line 20
def get(x, y)
  self[y-1][x-1]
end
horizontal(x1, x2, y, color) click to toggle source
# File lib/graphed/imageeditor.rb, line 28
def horizontal(x1, x2, y, color)
  (x1..x2).each{|x| set(x, y, color)}
end
Also aliased as: H
set(x, y, color) click to toggle source
# File lib/graphed/imageeditor.rb, line 16
def set(x, y, color)
  self[y-1][x-1] = color
end
Also aliased as: L
show() click to toggle source
# File lib/graphed/imageeditor.rb, line 52
def show
  puts "\n=>"
  each {|line| puts line.join}
  puts "\n"
end
Also aliased as: S
valid_on?(x,y) click to toggle source
# File lib/graphed/imageeditor.rb, line 12
def valid_on?(x,y)
  x>0 && y>0 && x<=@cols && y<=@rows ? true : false
end
vertical(x, y1, y2, color) click to toggle source
# File lib/graphed/imageeditor.rb, line 24
def vertical(x, y1, y2, color)
  (y1..y2).each{|y| set(x, y, color)}
end
Also aliased as: V