-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnode.py
66 lines (49 loc) · 1.71 KB
/
node.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import math_utils
class Node:
def __init__(self, position, cost, parent):
self._position = position
self._cost = cost
self._parent = parent
def cost(self):
return self._cost
def setCost(self, c):
self._cost = c
def costTo(self, destination):
return self.cost() + self.distance(destination)
def parent(self):
return self._parent
def setParent(self, n):
self._parent = n
def position(self):
return self._position
def distance(self, other):
return self.vector(other).norm()
def vector(self, to):
return self.position().vectorTo(to.position())
class ClosestResult:
def __init__(self, closest, distance):
self._closest = closest
self._distance = distance
def closest(self):
return self._closest
def distance(self):
return self._distance
def closest(self, node_list):
smallest_distance = self.distance(node_list[0])
closest_node = node_list[0]
if len(node_list) > 1:
for node in node_list[1:]:
distance = self.distance(node)
if distance < smallest_distance:
closest_node = node
smallest_distance = distance
return Node.ClosestResult(closest_node, smallest_distance)
def print(self):
if self.parent():
print("POSITION {}\nNodeGrid.COST{}\nNodeGrid.PARENT_POS {}\n".format(
self.position(),
self.cost(),
self.parent().position()
))
else:
print("ROOT: POSITION {}\nNodeGrid.COST{}\n".format(self.position(), self.cost()))