Compare commits
No commits in common. "1e43a2ff0d680461c1f875eea88c359312a1036f" and "14d535911c392171ece9468ba99705781eb2c20d" have entirely different histories.
1e43a2ff0d
...
14d535911c
@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
from enum import Enum
|
||||
from math import gcd, sqrt, inf, atan2, degrees, isclose
|
||||
from math import gcd, sqrt, inf, atan2, degrees
|
||||
from .math import round_half_up
|
||||
from typing import Union, List
|
||||
|
||||
@ -14,19 +14,19 @@ class DistanceAlgorithm(Enum):
|
||||
|
||||
|
||||
class Coordinate(tuple):
|
||||
def __new__(cls, x: int | float, y: int | float, z: int | float | None = None):
|
||||
return tuple.__new__(cls, (x, y, z))
|
||||
def __new__(cls, x: int, y: int, z: int = None) -> Coordinate:
|
||||
return tuple.__new__(Coordinate, (x, y, z))
|
||||
|
||||
@property
|
||||
def x(self) -> int | float:
|
||||
def x(self):
|
||||
return self[0]
|
||||
|
||||
@property
|
||||
def y(self) -> int | float:
|
||||
def y(self):
|
||||
return self[1]
|
||||
|
||||
@property
|
||||
def z(self) -> int | float:
|
||||
def z(self):
|
||||
return self[2]
|
||||
|
||||
def is3D(self) -> bool:
|
||||
@ -37,7 +37,7 @@ class Coordinate(tuple):
|
||||
target: Coordinate | tuple,
|
||||
algorithm: DistanceAlgorithm = DistanceAlgorithm.EUCLIDEAN,
|
||||
includeDiagonals: bool = False,
|
||||
) -> int | float:
|
||||
) -> Union[int, float]:
|
||||
"""
|
||||
Get distance to target Coordinate
|
||||
|
||||
@ -49,9 +49,7 @@ class Coordinate(tuple):
|
||||
"""
|
||||
if algorithm == DistanceAlgorithm.EUCLIDEAN:
|
||||
if self[2] is None:
|
||||
return sqrt(
|
||||
abs(self[0] - target[0]) ** 2 + abs(self[1] - target[1]) ** 2
|
||||
)
|
||||
return sqrt(abs(self[0] - target[0]) ** 2 + abs(self[1] - target[1]) ** 2)
|
||||
else:
|
||||
return sqrt(
|
||||
abs(self[0] - target[0]) ** 2
|
||||
@ -92,12 +90,12 @@ class Coordinate(tuple):
|
||||
|
||||
def inBoundaries(
|
||||
self,
|
||||
minX: int | float,
|
||||
minY: int | float,
|
||||
maxX: int | float,
|
||||
maxY: int | float,
|
||||
minZ: int | float = -inf,
|
||||
maxZ: int | float = inf,
|
||||
minX: int,
|
||||
minY: int,
|
||||
maxX: int,
|
||||
maxY: int,
|
||||
minZ: int = -inf,
|
||||
maxZ: int = inf,
|
||||
) -> bool:
|
||||
if self[2] is None:
|
||||
return minX <= self[0] <= maxX and minY <= self[1] <= maxY
|
||||
@ -110,14 +108,14 @@ class Coordinate(tuple):
|
||||
|
||||
def getCircle(
|
||||
self,
|
||||
radius: int | float = 1,
|
||||
radius: int = 1,
|
||||
algorithm: DistanceAlgorithm = DistanceAlgorithm.EUCLIDEAN,
|
||||
minX: int | float = -inf,
|
||||
minY: int | float = -inf,
|
||||
maxX: int | float = inf,
|
||||
maxY: int | float = inf,
|
||||
minZ: int | float = -inf,
|
||||
maxZ: int | float = inf,
|
||||
minX: int = -inf,
|
||||
minY: int = -inf,
|
||||
maxX: int = inf,
|
||||
maxY: int = inf,
|
||||
minZ: int = -inf,
|
||||
maxZ: int = inf,
|
||||
) -> list[Coordinate]:
|
||||
ret = []
|
||||
if self[2] is None: # mode 2D
|
||||
@ -154,13 +152,12 @@ class Coordinate(tuple):
|
||||
def getNeighbours(
|
||||
self,
|
||||
includeDiagonal: bool = True,
|
||||
minX: int | float = -inf,
|
||||
minY: int | float = -inf,
|
||||
maxX: int | float = inf,
|
||||
maxY: int | float = inf,
|
||||
minZ: int | float = -inf,
|
||||
maxZ: int | float = inf,
|
||||
dist: int | float = 1,
|
||||
minX: int = -inf,
|
||||
minY: int = -inf,
|
||||
maxX: int = inf,
|
||||
maxY: int = inf,
|
||||
minZ: int = -inf,
|
||||
maxZ: int = inf,
|
||||
) -> list[Coordinate]:
|
||||
"""
|
||||
Get a list of neighbouring coordinates.
|
||||
@ -172,23 +169,22 @@ class Coordinate(tuple):
|
||||
:param maxX: ignore all neighbours that would have an X value above this
|
||||
:param maxY: ignore all neighbours that would have an Y value above this
|
||||
:param maxZ: ignore all neighbours that would have an Z value above this
|
||||
:param dist: distance to neighbour coordinates
|
||||
:return: list of Coordinate
|
||||
"""
|
||||
if self[2] is None:
|
||||
if includeDiagonal:
|
||||
nb_list = [
|
||||
(-dist, -dist),
|
||||
(-dist, 0),
|
||||
(-dist, dist),
|
||||
(0, -dist),
|
||||
(0, dist),
|
||||
(dist, -dist),
|
||||
(dist, 0),
|
||||
(dist, dist),
|
||||
(-1, -1),
|
||||
(-1, 0),
|
||||
(-1, 1),
|
||||
(0, -1),
|
||||
(0, 1),
|
||||
(1, -1),
|
||||
(1, 0),
|
||||
(1, 1),
|
||||
]
|
||||
else:
|
||||
nb_list = [(-dist, 0), (dist, 0), (0, -dist), (0, dist)]
|
||||
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
||||
|
||||
for dx, dy in nb_list:
|
||||
if minX <= self[0] + dx <= maxX and minY <= self[1] + dy <= maxY:
|
||||
@ -197,19 +193,19 @@ class Coordinate(tuple):
|
||||
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]
|
||||
for x in [-1, 0, 1]
|
||||
for y in [-1, 0, 1]
|
||||
for z in [-1, 0, 1]
|
||||
]
|
||||
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),
|
||||
(-1, 0, 0),
|
||||
(0, -1, 0),
|
||||
(1, 0, 0),
|
||||
(0, 1, 0),
|
||||
(0, 0, 1),
|
||||
(0, 0, -1),
|
||||
]
|
||||
|
||||
for dx, dy, dz in nb_list:
|
||||
@ -237,7 +233,6 @@ class Coordinate(tuple):
|
||||
return 180.0 + abs(angle)
|
||||
|
||||
def getLineTo(self, target: Coordinate | tuple) -> List[Coordinate]:
|
||||
"""this will probably not yield what you expect, when using float coordinates"""
|
||||
if target == self:
|
||||
return [self]
|
||||
diff = target - self
|
||||
@ -281,25 +276,21 @@ class Coordinate(tuple):
|
||||
if self[2] is None:
|
||||
return self.__class__(self[0] + other[0], self[1] + other[1])
|
||||
else:
|
||||
return self.__class__(
|
||||
self[0] + other[0], self[1] + other[1], self[2] + other[2]
|
||||
)
|
||||
return self.__class__(self[0] + other[0], self[1] + other[1], self[2] + other[2])
|
||||
|
||||
def __sub__(self, other: Coordinate | tuple) -> Coordinate:
|
||||
if self[2] is None:
|
||||
return self.__class__(self[0] - other[0], self[1] - other[1])
|
||||
else:
|
||||
return self.__class__(
|
||||
self[0] - other[0], self[1] - other[1], self[2] - other[2]
|
||||
)
|
||||
return self.__class__(self[0] - other[0], self[1] - other[1], self[2] - other[2])
|
||||
|
||||
def __mul__(self, other: int | float) -> Coordinate:
|
||||
def __mul__(self, other: int) -> Coordinate:
|
||||
if self[2] is None:
|
||||
return self.__class__(self[0] * other, self[1] * other)
|
||||
else:
|
||||
return self.__class__(self[0] * other, self[1] * other, self[2] * other)
|
||||
|
||||
def __mod__(self, other: int | float) -> Coordinate:
|
||||
def __mod__(self, other: int) -> Coordinate:
|
||||
if self[2] is None:
|
||||
return self.__class__(self[0] % other, self[1] % other)
|
||||
else:
|
||||
@ -312,22 +303,19 @@ class Coordinate(tuple):
|
||||
return self.__class__(self[0] // other, self[1] // other, self[2] // other)
|
||||
|
||||
def __truediv__(self, other: int | float) -> Coordinate:
|
||||
if self[2] is None:
|
||||
return self.__class__(self[0] / other, self[1] / other)
|
||||
else:
|
||||
return self.__class__(self[0] / other, self[1] / other, self[2] / other)
|
||||
return self // other
|
||||
|
||||
def __str__(self):
|
||||
if self[2] is None:
|
||||
return "({},{})".format(self[0], self[1])
|
||||
return "(%d,%d)" % (self[0], self[1])
|
||||
else:
|
||||
return "({},{},{})".format(self[0], self[1], self[2])
|
||||
return "(%d,%d,%d)" % (self[0], self[1], self[2])
|
||||
|
||||
def __repr__(self):
|
||||
if self[2] is None:
|
||||
return "{}(x={}, y={})".format(self.__class__.__name__, self[0], self[1])
|
||||
return "%s(x=%d, y=%d)" % (self.__class__.__name__, self[0], self[1])
|
||||
else:
|
||||
return "{}(x={}, y={}, z={})".format(
|
||||
return "%s(x=%d, y=%d, z=%d)" % (
|
||||
self.__class__.__name__,
|
||||
self[0],
|
||||
self[1],
|
||||
@ -337,29 +325,112 @@ class Coordinate(tuple):
|
||||
@classmethod
|
||||
def generate(
|
||||
cls,
|
||||
from_x: int | float,
|
||||
to_x: int | float,
|
||||
from_y: int | float,
|
||||
to_y: int | float,
|
||||
from_z: int | float = None,
|
||||
to_z: int | float = None,
|
||||
step: int | float = 1,
|
||||
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 [
|
||||
cls(x, y)
|
||||
for x in range(from_x, to_x + step, step)
|
||||
for y in range(from_y, to_y + step, step)
|
||||
for x in range(from_x, to_x + 1)
|
||||
for y in range(from_y, to_y + 1)
|
||||
]
|
||||
else:
|
||||
return [
|
||||
cls(x, y, z)
|
||||
for x in range(from_x, to_x + step, step)
|
||||
for y in range(from_y, to_y + step, step)
|
||||
for z in range(from_z, to_z + step, step)
|
||||
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(HexCoordinate, self).__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(HexCoordinateF, self).__init__(x, y, z)
|
||||
|
||||
|
||||
class Shape:
|
||||
def __init__(self, top_left: Coordinate, bottom_right: Coordinate):
|
||||
"""
|
||||
@ -468,48 +539,3 @@ class Cube(Shape):
|
||||
if top_left.z is None or bottom_right.z is None:
|
||||
raise ValueError("Both Coordinates need to be 3D")
|
||||
super(Cube, self).__init__(top_left, bottom_right)
|
||||
|
||||
|
||||
class Line:
|
||||
def __init__(self, start: Coordinate, end: Coordinate):
|
||||
if start[2] is not None or end[2] is not None:
|
||||
raise NotImplementedError("3D Lines are hard(er)")
|
||||
self.start = start
|
||||
self.end = end
|
||||
|
||||
def contains(self, point: Coordinate | tuple) -> bool:
|
||||
return isclose(
|
||||
self.start.getDistanceTo(self.end),
|
||||
self.start.getDistanceTo(point) + self.end.getDistanceTo(point),
|
||||
)
|
||||
|
||||
def intersects(self, other: Line, strict: bool = True) -> bool:
|
||||
try:
|
||||
self.get_intersection(other, strict=strict)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
def get_intersection(self, other: Line, strict: bool = True) -> Coordinate:
|
||||
xdiff = (self.start[0] - self.end[0], other.start[0] - other.end[0])
|
||||
ydiff = (self.start[1] - self.end[1], other.start[1] - other.end[1])
|
||||
|
||||
def det(a, b):
|
||||
return a[0] * b[1] - a[1] * b[0]
|
||||
|
||||
div = det(xdiff, ydiff)
|
||||
if div == 0:
|
||||
raise ValueError("lines do not intersect")
|
||||
|
||||
d = (det(self.start, self.end), det(other.start, other.end))
|
||||
x = det(d, xdiff) / div
|
||||
y = det(d, ydiff) / div
|
||||
ret = Coordinate(x, y)
|
||||
|
||||
if not strict:
|
||||
return ret
|
||||
else:
|
||||
if self.contains(ret) and other.contains(ret):
|
||||
return ret
|
||||
else:
|
||||
raise ValueError("intersection out of bounds")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user