coordinate.py: predefine neighbouring coordinates

This commit is contained in:
Stefan Harmuth 2024-12-12 09:04:36 +01:00
parent 6adb1ff457
commit 270866387e
2 changed files with 21 additions and 25 deletions

View File

@ -6,6 +6,21 @@ from typing import Union, List, Iterable
from .tools import minmax
NEIGHBOURS = [(0, -1), (1, 0), (0, 1), (-1, 0)]
NEIGHBOURS_3D = [(0, -1, 0), (0, 0, -1), (1, 0, 0), (0, 0, -1), (0, 1, 0), (-1, 0, 0)]
DIAGONAL_NEIGHBOURS = [(-1, -1), (1, -1), (-1, 1), (1, 1)]
DIAGONAL_NEIGHBOURS_3D = [
(-1, -1, -1),
(1, -1, -1),
(-1, -1, 1),
(1, -1, 1),
(-1, 1, -1),
(-1, 1, 1),
(1, 1, -1),
(1, 1, 1),
]
class DistanceAlgorithm(Enum):
MANHATTAN = 0
EUCLIDEAN = 1
@ -157,36 +172,17 @@ class Coordinate(tuple):
:return: list of Coordinate
"""
if self[2] is None:
nb_list = [x * dist for x in NEIGHBOURS]
if includeDiagonal:
nb_list = [
(-dist, -dist),
(-dist, 0),
(-dist, dist),
(0, -dist),
(0, dist),
(dist, -dist),
(dist, 0),
(dist, dist),
]
else:
nb_list = [(-dist, 0), (dist, 0), (0, -dist), (0, dist)]
nb_list += [x * dist for x in DIAGONAL_NEIGHBOURS]
for dx, dy in nb_list:
if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY:
yield self.__class__(self[0] + dx, self[1] + dy)
else:
nb_list = [x * dist for x in NEIGHBOURS_3D]
if includeDiagonal:
nb_list = [(x, y, z) for x in [-dist, 0, dist] for y in [-dist, 0, dist] for z in [-dist, 0, dist]]
nb_list.remove((0, 0, 0))
else:
nb_list = [
(-dist, 0, 0),
(0, -dist, 0),
(dist, 0, 0),
(0, dist, 0),
(0, 0, dist),
(0, 0, -dist),
]
nb_list += [x * dist for x in DIAGONAL_NEIGHBOURS_3D]
for dx, dy, dz in nb_list:
if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY and minZ <= self[2] + dz <= maxZ:

View File

@ -6,8 +6,8 @@ from typing import Any
@dataclass
class Node:
value: Any
next: "Node" = None
prev: "Node" = None
next: Node = None
prev: Node = None
class LinkedList: