coordinates are essentially just tuples, so let's treat them as such. Double the speed for no cost :)
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 41s

This commit is contained in:
Stefan Harmuth 2023-10-19 06:02:25 +02:00
parent bd31a062cc
commit b0c6986511

View File

@ -1,9 +1,8 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from enum import Enum from enum import Enum
from math import gcd, sqrt, inf, atan2, degrees from math import gcd, sqrt, inf, atan2, degrees
from .math import round_half_up from .math import round_half_up
from typing import Union, List, Optional from typing import Union, List
class DistanceAlgorithm(Enum): class DistanceAlgorithm(Enum):
@ -14,11 +13,24 @@ class DistanceAlgorithm(Enum):
CHESSBOARD = 2 CHESSBOARD = 2
@dataclass(frozen=True) class Coordinate(tuple):
class Coordinate: def __new__(cls, x: int, y: int, z: int = None) -> Coordinate:
x: int return tuple.__new__(Coordinate, (x, y, z))
y: int
z: Optional[int] = None @property
def x(self):
return self[0]
@property
def y(self):
return self[1]
@property
def z(self):
return self[2]
def is3D(self) -> bool:
return self.z is not None
def getDistanceTo(self, target: Coordinate, algorithm: DistanceAlgorithm = DistanceAlgorithm.EUCLIDEAN, def getDistanceTo(self, target: Coordinate, algorithm: DistanceAlgorithm = DistanceAlgorithm.EUCLIDEAN,
includeDiagonals: bool = False) -> Union[int, float]: includeDiagonals: bool = False) -> Union[int, float]:
@ -156,7 +168,11 @@ class Coordinate:
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 [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: def reverse(self) -> Coordinate:
if self.z is None: if self.z is None:
@ -191,11 +207,6 @@ class Coordinate:
def __truediv__(self, other): def __truediv__(self, other):
return self // other return self // other
def __eq__(self, other):
if not isinstance(other, Coordinate):
return False
return self.x == other.x and self.y == other.y and self.z == other.z
def __gt__(self, other): def __gt__(self, other):
if self.z is None: if self.z is None:
return self.x > other.x and self.y > other.y return self.x > other.x and self.y > other.y