Class: Fractal::Map

Inherits:
Array
  • Object
show all
Defined in:
lib/fractal/map.rb

Defined Under Namespace

Classes: Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Map

Returns a new instance of Map.



23
24
25
26
# File 'lib/fractal/map.rb', line 23

def initialize(size)
  @width = @height = size
  super(size) { Fractal::Map::Line.new(size) }
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



2
3
4
# File 'lib/fractal/map.rb', line 2

def height
  @height
end

#widthObject

Returns the value of attribute width.



2
3
4
# File 'lib/fractal/map.rb', line 2

def width
  @width
end

Instance Method Details

#[](x, y) ⇒ Object



34
35
36
37
# File 'lib/fractal/map.rb', line 34

def [](x, y)
  raise "Out of bounds: #{y} / #{@height}" if y < 0 || y >= @height
  super(y)[x]
end

#[]=(x, y, value) ⇒ Object



39
40
41
# File 'lib/fractal/map.rb', line 39

def []=(x, y, value)
  row(y)[x] = value
end

#bytesObject

Encodes the map as a grayscale bitmap, with a color depth of 8 bits per pixel.

Returns the bitmap as an array of bytes.



46
47
48
# File 'lib/fractal/map.rb', line 46

def bytes
  flatten.pack("C*").bytes.to_a
end

#rowObject



3
# File 'lib/fractal/map.rb', line 3

alias :row :[]

#truncate(width, height) ⇒ Object



28
29
30
31
32
# File 'lib/fractal/map.rb', line 28

def truncate(width, height)
  pop while length > height
  collect! { |line| line[0...width] }
  @width, @height = width, height
end