Class: SlippyTilesScorer::Cluster

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

Overview

Finds connected clusters in a collection of x/y tiles.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles_x_y: Set.new) ⇒ Cluster

Returns a new instance of Cluster.



13
14
15
# File 'lib/slippy_tiles_scorer/cluster.rb', line 13

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

Instance Attribute Details

#tiles_x_yObject

Returns the value of attribute tiles_x_y.



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

def tiles_x_y
  @tiles_x_y
end

Instance Method Details

#clustersHash

Returns The clusters and the tiles surrounded on all four sides.

Returns:

  • (Hash)

    The clusters and the tiles surrounded on all four sides.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/slippy_tiles_scorer/cluster.rb', line 18

def clusters # rubocop:disable Metrics/MethodLength
  tile_index = build_tile_index

  clusters = []
  cluster_tiles = Set.new

  @tiles_x_y.each do |start|
    x = start[0]
    y = start[1]

    row = tile_index[y]

    # nil means this tile was already visited.
    next unless row && row[x]

    row[x] = nil

    cluster = [start]
    todo = [start]

    broad_search!(
      todo,
      cluster,
      cluster_tiles,
      tile_index
    )

    clusters << cluster
  end

  {
    clusters: clusters,
    cluster_tiles: cluster_tiles
  }
end