class MagicCloud::Layouter

Main magic of magic cloud - layouting shapes without collisions. Also, alongside with CollisionBoard - the slowest and algorithmically trickiest part.

Attributes

board[R]

Public Class Methods

new(w, h, options = {}) click to toggle source
# File lib/magic_cloud/layouter.rb, line 9
def initialize(w, h, options = {})
  @board = CollisionBoard.new(w, h)

  @options = options
end

Public Instance Methods

height() click to toggle source
# File lib/magic_cloud/layouter.rb, line 21
def height
  board.height
end
layout!(shapes) click to toggle source
# File lib/magic_cloud/layouter.rb, line 25
def layout!(shapes)
  visible_shapes = []

  shapes.each do |shape|
    next unless find_place(shape)

    visible_shapes.push(shape)
  end

  visible_shapes
end
width() click to toggle source
# File lib/magic_cloud/layouter.rb, line 17
def width
  board.width
end

Private Instance Methods

find_place(shape) click to toggle source
# File lib/magic_cloud/layouter.rb, line 39
def find_place(shape)
  place = Place.new(self, shape)
  start = Time.now
  steps = 0
  
  loop do
    steps += 1
    place.next!

    next unless place.ready?

    board.add(shape)
    Debug.logger.info 'Place for %p found in %i steps (%.2f sec)' %
      [shape, steps, Time.now-start]

    break
  end

  true
rescue PlaceNotFound
  Debug.logger.warn 'No place for %p found in %i steps (%.2f sec)' %
    [shape, steps, Time.now-start]

  false
end