Class: SlippyTilesScorer::MaxSquare

Inherits:
Object
  • Object
show all
Defined in:
lib/slippy_tiles_scorer/max_square.rb

Overview

finds the maximum square in a collection/tiles_x_y of points (x, y)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles_x_y: Set.new) ⇒ MaxSquare

Returns a new instance of MaxSquare.



10
11
12
13
# File 'lib/slippy_tiles_scorer/max_square.rb', line 10

def initialize(tiles_x_y: Set.new)
  @tiles_x_y = tiles_x_y
  @tiles_lut = nil
end

Instance Attribute Details

#tiles_x_yObject

Returns the value of attribute tiles_x_y.



8
9
10
# File 'lib/slippy_tiles_scorer/max_square.rb', line 8

def tiles_x_y
  @tiles_x_y
end

Instance Method Details

#max_square(x:, y:) ⇒ Integer

Returns The size of the square.

Parameters:

  • x (Integer)

    The x coordinate of the top left tile.

  • y (Integer)

    The y coordinate of the top left tile.

Returns:

  • (Integer)

    The size of the square.



44
45
46
47
48
# File 'lib/slippy_tiles_scorer/max_square.rb', line 44

def max_square(x:, y:)
  steps = 1
  steps += 1 while steps_fulfilled?(x: x, y: y, steps: steps)
  steps
end

#max_square_result(min_size: 3) ⇒ Hash

Returns The size of the square and the points of the square.

Parameters:

  • min_size (Integer) (defaults to: 3)

    The minimum size of the square, should be 3 or greater.

Returns:

  • (Hash)

    The size of the square and the points of the square.



30
31
32
33
34
35
36
37
38
39
# File 'lib/slippy_tiles_scorer/max_square.rb', line 30

def max_square_result(min_size: 3)
  result = { size: @max_size_found, top_left_tile_x_y: Set.new }
  @tiles_x_y.each do |(x, y)|
    raise ArgumentError, "x and y must be greater than or equal to 0" if x.negative? || y.negative?

    steps = max_square(x: x, y: y)
    result = track_result(result: result, steps: steps, x: x, y: y) if steps >= min_size
  end
  result
end

#max_squares(min_size: 3) ⇒ Hash

Returns The size of the square and the points of the square.

Parameters:

  • min_size (Integer) (defaults to: 3)

    The minimum size of the square, should be 3 or greater. As the smallest square is 3x3 and therefor a cluster tile needs to be the center.

Returns:

  • (Hash)

    The size of the square and the points of the square.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
# File 'lib/slippy_tiles_scorer/max_square.rb', line 18

def max_squares(min_size: 3)
  @max_size_found = 0
  raise ArgumentError, "min_size must be 2 or greater" if min_size < 2

  result = max_square_result(min_size: min_size)
  @max_size_found = 0
  @tiles_lut = nil
  result
end

#steps_fulfilled?(x:, y:, steps:) ⇒ Boolean

Returns True if all steps are fulfilled.

Parameters:

  • x (Integer)

    The x coordinate of the top left tile.

  • y (Integer)

    The y coordinate of the top left tile.

  • steps (Integer)

    The size of the square.

Returns:

  • (Boolean)

    True if all steps are fulfilled.



54
55
56
57
58
59
60
# File 'lib/slippy_tiles_scorer/max_square.rb', line 54

def steps_fulfilled?(x:, y:, steps:)
  in_lut?(x: x + steps, y: y + steps) &&
    (0...steps).all? do |i|
      in_lut?(x: x + steps, y: y + i) &&
        in_lut?(x: x + i, y: y + steps)
    end
end