Class: Algorithms::Containers::KDTree

Inherits:
Object
  • Object
show all
Defined in:
lib/containers/kd_tree.rb

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ KDTree

Points is a hash of id => [coord, coord] pairs.



31
32
33
34
35
36
# File 'lib/containers/kd_tree.rb', line 31

def initialize(points)
  raise "must pass in a hash" unless points.kind_of?(Hash)
  @dimensions = points[ points.keys.first ].size
  @root = build_tree(points.to_a)
  @nearest = []
end

Instance Method Details

#find_nearest(target, k_nearest) ⇒ Object

Find k closest points to given coordinates



39
40
41
42
# File 'lib/containers/kd_tree.rb', line 39

def find_nearest(target, k_nearest)
  @nearest = []
  nearest(@root, target, k_nearest, 0)
end