Class: MagicCloud::Layouter::Place
- Inherits:
-
Object
- Object
- MagicCloud::Layouter::Place
- Defined in:
- lib/magic_cloud/layouter/place.rb
Overview
Incapsulating place lookup process
-
find initial random place
-
at each step, shift in spiral from the previous place
-
always knows, if the place “ready” for shape (empty and inside board)
Instance Method Summary collapse
-
#initialize(layouter, shape) ⇒ Place
constructor
A new instance of Place.
- #next! ⇒ Object
- #ready? ⇒ Boolean
Constructor Details
#initialize(layouter, shape) ⇒ Place
12 13 14 15 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/magic_cloud/layouter/place.rb', line 12 def initialize(layouter, shape) @layouter, @shape = layouter, shape # initial position @start_x = ( @layouter.width/2 + # from center (rand-0.5) * # random shift (@layouter.width - @shape.width) # not more than (cloud width - word width) ).to_i @start_y = ( @layouter.height/2 + # from center (rand-0.5) * # random shift (@layouter.height - @shape.height) # not more than (cloud height - word height) ).to_i # when shift of position is more than max delta (diagonal of cloud) # there is no hope it will eventually found its place @max_delta = Math.sqrt(@layouter.width**2 + @layouter.height**2) # algo of next position calc @spiral = make_spiral(@shape.size) # direction of spiral @dt = rand < 0.5 ? 1 : -1 # initial point of time before we start to look for place @t = -@dt end |
Instance Method Details
#next! ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/magic_cloud/layouter/place.rb', line 41 def next! @t += @dt dx, dy = @spiral.call(@t) fail PlaceNotFound if [dx, dy].map(&:abs).min > @max_delta @shape.x = @start_x + dx @shape.y = @start_y + dy end |
#ready? ⇒ Boolean
51 52 53 |
# File 'lib/magic_cloud/layouter/place.rb', line 51 def ready? !out_of_board? && !@layouter.board.collides?(@shape) end |