Merge remote-tracking branch 'origin/master'

This commit is contained in:
Stefan Harmuth 2022-12-03 06:23:16 +01:00
commit c55d36380b
3 changed files with 104 additions and 51 deletions

View File

@ -13,7 +13,6 @@ class DistanceAlgorithm(Enum):
CHEBYSHEV = 2
CHESSBOARD = 2
@dataclass(frozen=True)
class Coordinate:
x: int
@ -154,25 +153,25 @@ class Coordinate:
steps = gcd(diff.x, diff.y)
step_x = diff.x // steps
step_y = diff.y // steps
return [Coordinate(self.x + step_x * i, self.y + step_y * i) for i in range(steps + 1)]
return [self.__class__(self.x + step_x * i, self.y + step_y * i) for i in range(steps + 1)]
else:
steps = gcd(diff.x, diff.y, diff.z)
step_x = diff.x // steps
step_y = diff.y // steps
step_z = diff.z // steps
return [Coordinate(self.x + step_x * i, self.y + step_y * i, self.z + step_z * i) for i in range(steps + 1)]
return [self.__class__(self.x + step_x * i, self.y + step_y * i, self.z + step_z * i) for i in range(steps + 1)]
def __add__(self, other: Coordinate) -> Coordinate:
if self.z is None:
return Coordinate(self.x + other.x, self.y + other.y)
return self.__class__(self.x + other.x, self.y + other.y)
else:
return Coordinate(self.x + other.x, self.y + other.y, self.z + other.z)
return self.__class__(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other: Coordinate) -> Coordinate:
if self.z is None:
return Coordinate(self.x - other.x, self.y - other.y)
return self.__class__(self.x - other.x, self.y - other.y)
else:
return Coordinate(self.x - other.x, self.y - other.y, self.z - other.z)
return self.__class__(self.x - other.x, self.y - other.y, self.z - other.z)
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
@ -217,16 +216,80 @@ class Coordinate:
def generate(from_x: int, to_x: int, from_y: int, to_y: int,
from_z: int = None, to_z: int = None) -> List[Coordinate]:
if from_z is None or to_z is None:
return [Coordinate(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]
return [self.__class__(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]
else:
return [
Coordinate(x, y, z)
self.__class__(x, y, z)
for x in range(from_x, to_x + 1)
for y in range(from_y, to_y + 1)
for z in range(from_z, to_z + 1)
]
class HexCoordinate(Coordinate):
"""
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
Treat as 3d Coordinate
+y -x +z
y x z
yxz
z x y
-z +x -y
"""
neighbour_vectors = {
'ne': Coordinate(-1, 0, 1),
'nw': Coordinate(-1, 1, 0),
'e': Coordinate(0, -1, 1),
'w': Coordinate(0, 1, -1),
'sw': Coordinate(1, 0, -1),
'se': Coordinate(1, -1, 0),
}
def __init__(self, x: int, y: int, z: int):
assert (x + y + z) == 0
super().__init__(x, y, z)
def get_length(self) -> int:
return (abs(self.x) + abs(self.y) + abs(self.z)) // 2
def getDistanceTo(self, target: Coordinate, algorithm: DistanceAlgorithm = DistanceAlgorithm.EUCLIDEAN,
includeDiagonals: bool = True) -> Union[int, float]:
# includeDiagonals makes no sense in a hex grid, it's just here for signature reasons
if algorithm == DistanceAlgorithm.MANHATTAN:
return (self - target).get_length()
def getNeighbours(self, includeDiagonal: bool = True, minX: int = -inf, minY: int = -inf,
maxX: int = inf, maxY: int = inf, minZ: int = -inf, maxZ: int = inf) -> list[Coordinate]:
# includeDiagonals makes no sense in a hex grid, it's just here for signature reasons
return [
self + x for x in self.neighbour_vectors.values()
if minX <= (self + x).x <= maxX and minY <= (self + x).y <= maxY and minZ <= (self + x).z <= maxZ
]
HexCoordinateR = HexCoordinate
class HexCoordinateF(HexCoordinate):
"""
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
Treat as 3d Coordinate
+y -x
y x
-z z yxz z +z
x y
+x -y
"""
neighbour_vectors = {
'ne': Coordinate(-1, 0, 1),
'nw': Coordinate(0, 1, -1),
'n': Coordinate(-1, 1, 0),
's': Coordinate(1, -1, 0),
'sw': Coordinate(1, 0, -1),
'se': Coordinate(0, -1, 1),
}
def __init__(self, x: int, y: int, z: int):
super().__init__(x, y, z)
class Shape:
def __init__(self, top_left: Coordinate, bottom_right: Coordinate):
"""

View File

@ -183,6 +183,19 @@ class Grid:
else:
return list(self.__grid.keys())
def getActiveRegion(self, start: Coordinate, includeDiagonal: bool = False, ignore: List[Coordinate] = None) \
-> List[Coordinate]:
if not self.get(start):
return []
if ignore is None:
ignore = []
ignore.append(start)
for c in self.getNeighboursOf(start, includeDiagonal=includeDiagonal):
if c not in ignore:
ignore = self.getActiveRegion(c, includeDiagonal, ignore)
return ignore
def values(self):
return self.__grid.values()
@ -326,33 +339,3 @@ class Grid:
print(spacer, end="")
print()
class HexGrid(Grid):
"""
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
Treat as 3d Grid
+y -x +z
y x z
yxz
z x y
-z +x -y
"""
def __init__(self, default=False):
super().__init__(default=default)
def getNeighboursOf(self, pos: Coordinate, includeDefault: bool = False, includeDiagonal: bool = None) \
-> List[Coordinate]:
"""
includeDiagonal is just here because of signature reasons, makes no difference in a hex grid
"""
vectors = [
Coordinate(-1, 1, 0), # nw
Coordinate(-1, 0, 1), # ne
Coordinate(0, -1, 1), # e
Coordinate(1, -1, 0), # se
Coordinate(1, 0, -1), # sw
Coordinate(0, 1, -1), # w
]
return [pos + v for v in vectors if includeDefault or self.get(pos + v) != self.__default]

View File

@ -2,16 +2,23 @@ from time import perf_counter_ns
from .types import IntOrNone
def ns_to_string(ns: int) -> str:
def get_time_string_from_ns(ns: int) -> str:
# mis, ns = ns // 1000, ns % 1000
# ms, mis = mis // 1000, mis % 1000
# s, ms = ms // 1000, ms % 1000
# m, s = s // 60, s % 60
# h, m = m // 60, m % 60
# d, h = h // 24, h % 24
units = ['ns', 'µs', 'ms', 's']
unit = 0
while ns > 1_000:
ns /= 1_000
unit += 1
if unit == len(units) - 1:
if unit > 3:
break
ns /= 1000
unit += 1
return f"{ns:1.2f}{units[unit]}"
return "%1.2f%s" % (ns, units[unit])
class StopWatch:
@ -26,26 +33,26 @@ class StopWatch:
self.started = perf_counter_ns()
self.stopped = None
def stop(self) -> int:
def stop(self) -> float:
self.stopped = perf_counter_ns()
return self.elapsed()
reset = start
def elapsed(self) -> int:
def elapsed(self) -> float:
if self.stopped is None:
return perf_counter_ns() - self.started
else:
return self.stopped - self.started
def avg_elapsed(self, divider: int) -> int:
return self.elapsed() // divider # in ns precision loosing some rounding error probably will not hurt
def elapsed_string(self) -> str:
return get_time_string_from_ns(self.elapsed())
def elapsed_string(self):
return ns_to_string(self.elapsed())
def avg_elapsed(self, divider: int) -> float:
return self.elapsed() / divider
def avg_string(self, divider: int) -> str:
return ns_to_string(self.avg_elapsed(divider))
return get_time_string_from_ns(int(self.avg_elapsed(divider)))
def __str__(self):
return self.avg_string(1)