-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
75 lines (61 loc) · 2.38 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
67
68
69
70
71
72
73
74
75
import math
class Node:
def __init__(self, identifier: int, interface: "Interface object"):
self.connections = []
self.identifier = identifier
self.interface = interface
self.centre = (0, 0)
self.radius = None
self.visits = 0
def __int__(self):
return int(self.identifier)
@property
def border_positions(self):
return (self.centre[0] - self.radius, self.centre[1] - self.radius), (self.centre[0] + self.radius, self.centre[1] + self.radius)
@property
def x(self):
return self.centre[0]
@property
def y(self):
return self.centre[1]
def connect_to(self, node: "Node object"):
if node.identifier not in self.connections and self.identifier not in node.connections:
self.connections.append(node.identifier)
node.connections.append(self.identifier)
else:
raise PermissionError(f"Path connection already exists between nodes {self.identifier} and {node.identifier}")
def disconnect_from(self, node: "Node object"):
if node.identifier in self.connections and self.identifier in node.connections:
self.connections.pop(self.connections.index(node.identifier))
node.connections.pop(node.connections.index(self.identifier))
else:
raise PermissionError(f"No initial path connection found between nodes {self.identifier} and {node.identifier}")
def distance_from(self, identifier: "Node identifier"=None):
"""
Gets the distance (in pixels) between the node this method is being called from
and a target node.
:param node:
:return:
"""
try:
node = self.interface.graph.get_node(identifier)
return math.sqrt((self.x-node.x)**2 + (self.y-node.y)**2)
except:
return min([v for k, v in self.get_all_distances().items() if k != self.identifier])
def get_all_distances(self):
return {k.identifier: self.distance_from(k.identifier) for k in self.interface.graph.nodes}
def set_position(self, x, y):
"""
Sets the centre position of the node.
:param x:
:param y:
:return:
"""
self.centre = (x, y)
def set_radius(self, r):
"""
Sets radius of the node.
:param r:
:return:
"""
self.radius = r