*real* start to hex coordinates

This commit is contained in:
Stefan Harmuth 2022-11-29 20:37:46 +01:00
parent d92686dd28
commit c16bc0d1cf
2 changed files with 72 additions and 39 deletions

View File

@ -13,7 +13,6 @@ class DistanceAlgorithm(Enum):
CHEBYSHEV = 2 CHEBYSHEV = 2
CHESSBOARD = 2 CHESSBOARD = 2
@dataclass(frozen=True) @dataclass(frozen=True)
class Coordinate: class Coordinate:
x: int x: int
@ -154,25 +153,25 @@ class Coordinate:
steps = gcd(diff.x, diff.y) steps = gcd(diff.x, diff.y)
step_x = diff.x // steps step_x = diff.x // steps
step_y = diff.y // 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: else:
steps = gcd(diff.x, diff.y, diff.z) steps = gcd(diff.x, diff.y, diff.z)
step_x = diff.x // steps step_x = diff.x // steps
step_y = diff.y // steps step_y = diff.y // steps
step_z = diff.z // 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: def __add__(self, other: Coordinate) -> Coordinate:
if self.z is None: 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: 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: def __sub__(self, other: Coordinate) -> Coordinate:
if self.z is None: 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: 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): def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z 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, def generate(from_x: int, to_x: int, from_y: int, to_y: int,
from_z: int = None, to_z: int = None) -> List[Coordinate]: from_z: int = None, to_z: int = None) -> List[Coordinate]:
if from_z is None or to_z is None: 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: else:
return [ return [
Coordinate(x, y, z) self.__class__(x, y, z)
for x in range(from_x, to_x + 1) for x in range(from_x, to_x + 1)
for y in range(from_y, to_y + 1) for y in range(from_y, to_y + 1)
for z in range(from_z, to_z + 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: class Shape:
def __init__(self, top_left: Coordinate, bottom_right: Coordinate): def __init__(self, top_left: Coordinate, bottom_right: Coordinate):
""" """

View File

@ -326,33 +326,3 @@ class Grid:
print(spacer, end="") print(spacer, end="")
print() 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]