Class: SpatialHash

Inherits:
Object
  • Object
show all
Defined in:
lib/spatial_hash.rb,
lib/spatial_hash/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cell_size) ⇒ SpatialHash

Returns a new instance of SpatialHash.



7
8
9
10
# File 'lib/spatial_hash.rb', line 7

def initialize(cell_size)
  @cell_size = cell_size
  @cells = {}
end

Instance Attribute Details

#cell_sizeObject (readonly)

Returns the value of attribute cell_size.



5
6
7
# File 'lib/spatial_hash.rb', line 5

def cell_size
  @cell_size
end

#cellsObject (readonly)

Returns the value of attribute cells.



5
6
7
# File 'lib/spatial_hash.rb', line 5

def cells
  @cells
end

Instance Method Details

#insert(object) ⇒ Object



12
13
14
15
16
17
# File 'lib/spatial_hash.rb', line 12

def insert(object)
  get_cells(object.box).each do |cell|
    @cells[cell] ||= []
    @cells[cell].push object
  end
end

#query(box) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/spatial_hash.rb', line 19

def query(box)
  objects = []
  get_cells(box).each do |cell|
    objects.push @cells[cell]
  end
  objects.flatten.compact.uniq
end