Class: PictureFrame::Canvas

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

Instance Method Summary collapse

Methods inherited from Raster

#==, #at, #bottom_right, #dimensions, #each, #each_with_index, #height, #slice, #to_s, #top_left, #width

Constructor Details

#initialize(*dimensions) ⇒ Canvas

Returns a new instance of Canvas.



5
6
7
8
9
10
11
12
13
14
# File 'lib/picture_frame/canvas.rb', line 5

def initialize(*dimensions)
  rows, cols = 0, 0
  dimensions.each do |r, c|
    rows += r
    cols += c
  end

  raster = (1..rows).inject([]) { |r| r << ' ' * cols }
  super(raster)
end

Instance Method Details



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

def print_at(pos, raster, start = nil, length = nil)
  case pos
  when Array
    overwrite(pos[0], pos[1], raster)

  when :t, :top
    overwrite(0, make_range(start, length), raster)
  when :r, :right
    overwrite(make_range(start, length), width - raster.width, raster)
  when :b, :bottom
    overwrite(height - raster.height, make_range(start, length), raster)
  when :l, :left
    overwrite(make_range(start, length), 0, raster)

  when :tl, :top_left
    overwrite(0, 0, raster)
  when :tr, :top_right
    overwrite(0, width - raster.width, raster)
  when :br, :bottom_right
    overwrite(height - raster.height, width - raster.width, raster)
  when :bl, :bottom_left
    overwrite(height - raster.height, 0, raster)
  end
end