-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphNode.py
97 lines (69 loc) · 3.75 KB
/
GraphNode.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class GraphNode():
MINIMUMSPEED = 0.1
MAXIMUMSPEED = 100
def __init__(self, label, pos=(0, 0)):
self.label = label
self.position = [pos[0], pos[1]]
self.velocity = [0, 0]
self.externalForce = [0, 0]
def get_label(self):
'''Returns the node's label.'''
return self.label
def set_label(self, L):
'''Sets the node's label.'''
self.label = L
def get_position(self):
'''Return the position of the node.'''
return self.position
def set_position(self, pos):
'''Sets the position of the node to the given coordinates in pos.'''
self.position[0] = pos[0]
self.position[1] = pos[1]
def set_externalForce(self, force):
'''Set the external force acting on the node.'''
self.externalForce = force.copy()
def add_externalForce(self, force):
'''Adds to the node's force.'''
self.externalForce[0] += force[0]
self.externalForce[1] += force[1]
def distance_to(self, node):
'''Returns the Euclidian distance between this node and the given node.'''
return ((self.position[0]-node.get_position()[0])**2 +
(self.position[1]-node.get_position()[1])**2)**0.5
def direction_to(self, node):
'''Returns a vector representing the direction to a given node from this node.'''
return (node.get_position()[0] - self.position[0],
node.get_position()[1] - self.position[1])
def apply_friction(self, f):
'''Applies friction dampening to the node.'''
friction = [self.velocity[0]*f, self.velocity[1]*f]
self.externalForce[0] -= friction[0]
self.externalForce[1] -= friction[1]
def update_position(self, boundingLengths=None, boundingOffset=(0, 0), boundingRadius=0):
'''Uses the external force acting on the node to update its position.'''
self.velocity[0] += self.externalForce[0]
self.velocity[1] += self.externalForce[1]
if abs(self.velocity[0]) < GraphNode.MINIMUMSPEED:
self.velocity[0] = 0
elif self.velocity[0] > GraphNode.MAXIMUMSPEED:
self.velocity[0] = GraphNode.MAXIMUMSPEED
elif self.velocity[0] < -GraphNode.MAXIMUMSPEED:
self.velocity[0] = -GraphNode.MAXIMUMSPEED
if abs(self.velocity[1]) < GraphNode.MINIMUMSPEED:
self.velocity[1] = 0
elif self.velocity[1] > GraphNode.MAXIMUMSPEED:
self.velocity[1] = GraphNode.MAXIMUMSPEED
elif self.velocity[1] < -GraphNode.MAXIMUMSPEED:
self.velocity[1] = -GraphNode.MAXIMUMSPEED
self.position[0] += self.velocity[0]
self.position[1] += self.velocity[1]
#Keeping nodes within some bounding box, if needed
if boundingLengths != None:
if self.position[0] - boundingRadius < boundingOffset[0]:
self.position[0] = boundingOffset[0] + boundingRadius
elif self.position[0] + boundingRadius > boundingLengths[0] + boundingOffset[0]:
self.position[0] = boundingLengths[0] + boundingOffset[0] - boundingRadius
if self.position[1] - boundingRadius < boundingOffset[1]:
self.position[1] = boundingOffset[1] + boundingRadius
elif self.position[1] + boundingRadius > boundingLengths[1] + boundingOffset[1]:
self.position[1] = boundingLengths[1] + boundingOffset[1] - boundingRadius