-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplane.rb
More file actions
51 lines (45 loc) · 1021 Bytes
/
plane.rb
File metadata and controls
51 lines (45 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
class Plane
attr_accessor :nodes
attr_accessor :dimension
def initialize(args = {})
@nodes = args.fetch(:nodes, [])
@dimension = args.fetch(:dimension, 2)
end
def add_node(node)
nodes.push(node) if validate_node_dimension(node)
end
def distances
for i in 0..count do
for j in i..count do
nodeA = nodes[i]
nodeB = nodes[j]
dist = nodeA.distance_to(nodeB)
puts "#{i} - #{j}: #{dist}"
end
end
end
def near(node, radius)
nears = []
for i in 0..count do
iNode = nodes[i]
dist = node.distance_to iNode
if node != iNode && dist < radius
nears.push({node: iNode, distance: dist})
end
end
nears
end
private
def count
count ||= nodes.count - 1
count
end
def validate_node_dimension(node)
if node.dimension != dimension
raise Exception, "Node dimension different from Plane dimension."
false
else
true
end
end
end