Module: VectorBeWinding::SpatialTree

Included in:
Path, SubPath
Defined in:
lib/vector_be_winding/spatial_tree.rb

Instance Method Summary collapse

Instance Method Details

#childrenObject



3
4
5
# File 'lib/vector_be_winding/spatial_tree.rb', line 3

def children
  @children ||= []
end

#depthObject



21
22
23
24
25
26
27
# File 'lib/vector_be_winding/spatial_tree.rb', line 21

def depth
  if children.empty?
    1
  else
    children.map(&:depth).max + 1
  end
end

#dump(indent = 0) ⇒ Object



34
35
36
37
# File 'lib/vector_be_winding/spatial_tree.rb', line 34

def dump(indent = 0)
  puts ' ' * (indent * 2) + (area < 0 ? '-' : '+') + inspect
  children.each {|node| node.dump(indent + 1)}
end

#each(&pr) ⇒ Object



29
30
31
32
# File 'lib/vector_be_winding/spatial_tree.rb', line 29

def each &pr
  pr.call(self)
  children.each {|node| node.each(&pr) }
end

#insert_to_tree(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vector_be_winding/spatial_tree.rb', line 7

def insert_to_tree(node)
  raise "#{node} is same node: " if equal?(node)

  containers = children.select {|n1| n1.contains?(node)}
  if containers.empty?
    components = children.select {|n1| node.contains?(n1)}
    components.each{|n1| children.delete(n1)}
    node.children.concat(components)
    children << node
  else
    containers.first.insert_to_tree(node)
  end
end