Class: BDF::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/bdf/canvas.rb

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Canvas

Returns a new instance of Canvas.



3
4
5
6
7
# File 'lib/bdf/canvas.rb', line 3

def initialize(width, height)
  @width = width
  @height = height
  @array = Array.new(height){Array.new(width, 0)}
end

Instance Method Details

#resize(width, height) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bdf/canvas.rb', line 18

def resize(width, height)
  @array.map! do |line|
    if width > line.size
      line + [0]*(width - line.size)
    else
      line
    end
  end

  @width = width

  if height > @array.size
    # This is a loop, because the multiplication op on arrays does not work
    # with non-scalars. You have been warned.
    (height - @array.size).times do
      @array += [[0]*@width]
    end
  end

  @height = height
end

#set(x, y) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/bdf/canvas.rb', line 9

def set(x, y)
  unless x < @width && y < @height
    # TODO: Maybe resize to other size to improve speed.
    resize(x+1, y+1)
  end

  @array[y][x] = 1
end

#to_aObject



40
41
42
# File 'lib/bdf/canvas.rb', line 40

def to_a
  @array.dup
end