-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
63 lines (56 loc) · 1.67 KB
/
node.py
File metadata and controls
63 lines (56 loc) · 1.67 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import annotations
class QuadtreeNode:
def __init__(self, x, y, width, height, color=None, children=None):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.children = children
def is_leaf(self) -> bool:
return self.color is not None and self.children is None
def to_dict(self) -> dict:
if self.is_leaf():
return {
"x": self.x,
"y": self.y,
"width": self.width,
"height": self.height,
"color": list(self.color),
"children": None
}
else:
return {
"x": self.x,
"y": self.y,
"width": self.width,
"height": self.height,
"color": None,
"children": [
child.to_dict()
for child in self.children
]
}
@classmethod
def from_dict(cls, data: dict) -> QuadtreeNode:
if data["children"] is None:
return cls(
x=data["x"],
y=data["y"],
width=data["width"],
height=data["height"],
color=tuple(data["color"]),
children=None,
)
children = tuple(
cls.from_dict(child_data)
for child_data in data["children"]
)
return cls(
x=data["x"],
y=data["y"],
width=data["width"],
height=data["height"],
color=None,
children=children,
)