Class: Amaze::Grid::Polar

Inherits:
Amaze::Grid show all
Defined in:
lib/amaze/grid/polar.rb

Instance Attribute Summary

Attributes inherited from Amaze::Grid

#grid, #rows

Instance Method Summary collapse

Methods inherited from Amaze::Grid

#deadends, #each_cell, #each_row

Constructor Details

#initialize(rows) ⇒ Polar

Returns a new instance of Polar.



4
5
6
# File 'lib/amaze/grid/polar.rb', line 4

def initialize rows
  super rows, 1
end

Instance Method Details

#[](row, column) ⇒ Object



45
46
47
48
# File 'lib/amaze/grid/polar.rb', line 45

def [](row, column)
  return nil unless row.between?(0, rows-1)
  grid[row][column % columns(row).size]
end

#columns(row) ⇒ Object



41
42
43
# File 'lib/amaze/grid/polar.rb', line 41

def columns row
  grid[row]
end

#configure_cellObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/amaze/grid/polar.rb', line 26

def configure_cell
  each_cell do |cell|
    row, column = cell.row, cell.column
    next if row == 0
    
    cell.cw = self[row, column+1]
    cell.ccw = self[row, column-1]
    
    ratio = grid[row].size / grid[row-1].size
    parent = self[row-1, column/ratio]
    parent.outward << cell
    cell.inward = parent
  end
end

#prepare_gridObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/amaze/grid/polar.rb', line 8

def prepare_grid
  @grid = Array.new(rows)
  @grid[0] = [Amaze::Cell::Polar.new(0, 0)]
  
  row_height = 1.0 / rows
  (1...rows).each do |row|
    radius = row.to_f / rows
    circumference = 2 * Math::PI * radius
    
    previous_count = @grid[row-1].size
    estimated_cell_width = circumference / previous_count
    ratio = (estimated_cell_width / row_height).round
    
    cells = previous_count * ratio
    @grid[row] = Array.new(cells) {|column| Amaze::Cell::Polar.new(row, column) }
  end
end

#random_cellObject



50
51
52
53
54
# File 'lib/amaze/grid/polar.rb', line 50

def random_cell
  row = rand rows
  column = rand grid[row].size
  self[row, column]
end

#sizeObject



56
57
58
59
60
# File 'lib/amaze/grid/polar.rb', line 56

def size
  count = 0
  each_row {|row| count += row.size }
  count
end