-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
40 lines (33 loc) · 1.18 KB
/
node.py
File metadata and controls
40 lines (33 loc) · 1.18 KB
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
import random
# Node class
class Node:
def __init__(self, sim, propScale, occupied):
self.sim = sim
self.x = random.uniform(0, propScale)
self.y = random.uniform(0, propScale)
while ((self.x, self.y) in occupied):
self.x = random.uniform(0, propScale)
self.y = random.uniform(0, propScale)
self.links = []
def distanceTo(self, x, y):
return (self.x - x) ** 2 + (self.y - y) ** 2
def distanceTo(self, node):
return (self.x - node.x) ** 2 + (self.y - node.y) ** 2
def linkTo(self, node):
from link import Link
link = Link(self.sim, self, node)
self.links.append(link)
node.links.append(link)
def getLinkTo(self, neighbor):
for link in self.links:
if link.node1 == neighbor or link.node2 == neighbor:
return link
return None
def longStr(self):
data = self.__str__() + ": Links: "
for link in self.links:
other = link.getOther(self)
data += f"{other} "
return data
def __str__(self):
return f"{type(self).__name__} at ({self.x:.2f}, {self.y:.2f})"