-
Notifications
You must be signed in to change notification settings - Fork 0
/
area_mask.py
67 lines (58 loc) · 2.25 KB
/
area_mask.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
import math
from collections import defaultdict
from client.message.extra_types import XY
from .constants import DIRECTIONS, X, Y
class AreaMask:
def __init__(self, radius: int):
self.radius: int = radius
self.mask: list[XY] = self._generate_mask(radius)
self.outside_edge: dict[XY, list[XY]] = self._generate_outside_edge(
mask_edge=self._get_mask_edge(radius, self.mask),
mask_edge_diagonal=self._get_mask_edge_diagonal(set(self.mask))
)
def __iter__(self):
return self.mask.__iter__()
@staticmethod
def _generate_mask(radius: int) -> list[XY]:
mask = []
for x in range(-radius, radius + 1):
for y in range(-radius, radius + 1):
if math.hypot(x, y) <= radius:
mask.append((x, y))
return mask
@staticmethod
def _generate_outside_edge(
mask_edge: list[XY],
mask_edge_diagonal: list[XY]
) -> dict[XY, list[XY]]:
outside_edge: dict[XY, list[XY]] = defaultdict(list)
for direction in DIRECTIONS:
if direction[X] == 0 or direction[Y] == 0:
outside_edge[direction] = [
(pt[X], direction[Y] * (pt[Y] + 1)) if direction[X] == 0
else (direction[X] * (pt[Y] + 1), pt[X])
for pt in mask_edge
]
else:
outside_edge[direction] = [
(direction[X] * (pt[X] + 1), direction[Y] * (pt[Y] + 1))
for pt in mask_edge_diagonal
]
return outside_edge
@staticmethod
def _get_mask_edge(radius: int, mask: list[XY]) -> list[XY]:
mask_edge: list[XY] = []
for i in range(radius + 1):
mask_edge.append(max(v for v in mask if v[X] == i))
for pt in mask_edge[1:]:
mask_edge.append((-pt[X], pt[Y]))
return mask_edge
@staticmethod
def _get_mask_edge_diagonal(mask_set: set[XY]) -> list[XY]:
mask_edge_diagonal: list[XY] = []
for pt in mask_set:
if pt[X] < 0 and pt[Y] < 0:
continue
if (pt[X] + 1, pt[Y] + 1) not in mask_set:
mask_edge_diagonal.append(pt)
return mask_edge_diagonal