reversable coordinates; grid.print() can now mark important spots
This commit is contained in:
parent
35972bffe2
commit
5df82d2359
@ -13,6 +13,7 @@ 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
|
||||||
@ -161,6 +162,12 @@ class Coordinate:
|
|||||||
step_z = diff.z // steps
|
step_z = diff.z // steps
|
||||||
return [self.__class__(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 reverse(self) -> Coordinate:
|
||||||
|
if self.z is None:
|
||||||
|
return self.__class__(-self.x, -self.y)
|
||||||
|
else:
|
||||||
|
return self.__class__(-self.x, -self.y, -self.z)
|
||||||
|
|
||||||
def __add__(self, other: Coordinate) -> Coordinate:
|
def __add__(self, other: Coordinate) -> Coordinate:
|
||||||
if self.z is None:
|
if self.z is None:
|
||||||
return self.__class__(self.x + other.x, self.y + other.y)
|
return self.__class__(self.x + other.x, self.y + other.y)
|
||||||
@ -212,14 +219,14 @@ class Coordinate:
|
|||||||
else:
|
else:
|
||||||
return "%s(x=%d, y=%d, z=%d)" % (self.__class__.__name__, self.x, self.y, self.z)
|
return "%s(x=%d, y=%d, z=%d)" % (self.__class__.__name__, self.x, self.y, self.z)
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def generate(from_x: int, to_x: int, from_y: int, to_y: int,
|
def generate(cls, 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 [self.__class__(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]
|
return [cls(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]
|
||||||
else:
|
else:
|
||||||
return [
|
return [
|
||||||
self.__class__(x, y, z)
|
cls(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)
|
||||||
@ -266,7 +273,10 @@ class HexCoordinate(Coordinate):
|
|||||||
if minX <= (self + x).x <= maxX and minY <= (self + x).y <= maxY and minZ <= (self + x).z <= maxZ
|
if minX <= (self + x).x <= maxX and minY <= (self + x).y <= maxY and minZ <= (self + x).z <= maxZ
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
HexCoordinateR = HexCoordinate
|
HexCoordinateR = HexCoordinate
|
||||||
|
|
||||||
|
|
||||||
class HexCoordinateF(HexCoordinate):
|
class HexCoordinateF(HexCoordinate):
|
||||||
"""
|
"""
|
||||||
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
|
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
|
||||||
|
|||||||
@ -357,10 +357,12 @@ class Grid:
|
|||||||
put_y = y
|
put_y = y
|
||||||
put_x += 1
|
put_x += 1
|
||||||
|
|
||||||
def print(self, spacer: str = "", true_char: str = None, false_char: str = " "):
|
def print(self, spacer: str = "", true_char: str = None, false_char: str = " ", mark: list = None):
|
||||||
for y in range(self.minY, self.maxY + 1):
|
for y in range(self.minY, self.maxY + 1):
|
||||||
for x in range(self.minX, self.maxX + 1):
|
for x in range(self.minX, self.maxX + 1):
|
||||||
if true_char:
|
if mark and Coordinate(x, y) in mark:
|
||||||
|
print("X", end="")
|
||||||
|
elif true_char:
|
||||||
print(true_char if self.get(Coordinate(x, y)) else false_char, end="")
|
print(true_char if self.get(Coordinate(x, y)) else false_char, end="")
|
||||||
else:
|
else:
|
||||||
print(self.get(Coordinate(x, y)), end="")
|
print(self.get(Coordinate(x, y)), end="")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user