Class: SpatialHash
- Inherits:
-
Object
- Object
- SpatialHash
- Defined in:
- lib/spatial_hash.rb,
lib/spatial_hash/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#cell_size ⇒ Object
readonly
Returns the value of attribute cell_size.
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
Instance Method Summary collapse
-
#initialize(cell_size) ⇒ SpatialHash
constructor
A new instance of SpatialHash.
- #insert(object) ⇒ Object
- #query(box) ⇒ Object
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_size ⇒ Object (readonly)
Returns the value of attribute cell_size.
5 6 7 |
# File 'lib/spatial_hash.rb', line 5 def cell_size @cell_size end |
#cells ⇒ Object (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 |