coordinates \o/
This commit is contained in:
commit
fb30236fd8
62
coordinate.py
Normal file
62
coordinate.py
Normal file
@ -0,0 +1,62 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from math import sqrt, inf
|
||||
from typing import Union
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class Coordinate:
|
||||
x: int
|
||||
y: int
|
||||
|
||||
def getDistanceTo(self, target: Coordinate, mode: int = 1, includeDiagonals: bool = False) -> Union[int, float]:
|
||||
"""
|
||||
Get distance to target Coordinate
|
||||
|
||||
:param target:
|
||||
:param mode: Calculation Mode (0 = Manhattan, 1 = Pythagoras)
|
||||
:param includeDiagonals: in Manhattan Mode specify if diagonal movements are allowed (counts as 1.4)
|
||||
:return: Distance to Target
|
||||
"""
|
||||
assert isinstance(target, Coordinate)
|
||||
assert mode in [0, 1]
|
||||
assert isinstance(includeDiagonals, bool)
|
||||
|
||||
if mode == 1:
|
||||
return sqrt(abs(self.x - target.x) ** 2 + abs(self.y - target.y) ** 2)
|
||||
elif mode == 0:
|
||||
if not includeDiagonals:
|
||||
return abs(self.x - target.x) + abs(self.y - target.y)
|
||||
else:
|
||||
x_dist = abs(self.x - target.x)
|
||||
y_dist = abs(self.y - target.y)
|
||||
o_dist = max(x_dist, y_dist) - min(x_dist, y_dist)
|
||||
return o_dist + 1.4 * min(x_dist, y_dist)
|
||||
|
||||
def getNeighbours(self, includeDiagonal: bool = True, minX: int = -inf, minY: int = -inf,
|
||||
maxX: int = inf, maxY: int = inf) -> list[Coordinate]:
|
||||
"""
|
||||
Get a list of neighbouring coordinates
|
||||
|
||||
:param includeDiagonal: include diagonal neighbours
|
||||
:param minX: ignore all neighbours that would have an X value below this
|
||||
:param minY: ignore all neighbours that would have an Y value below this
|
||||
: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
|
||||
:return: list of Coordinate
|
||||
"""
|
||||
neighbourList = []
|
||||
if includeDiagonal:
|
||||
nb_list = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
|
||||
else:
|
||||
nb_list = [(-1, 0), (1, 0), (0, -1), (0, 1)]
|
||||
|
||||
for dx, dy in nb_list:
|
||||
tx = self.x + dx
|
||||
ty = self.y + dy
|
||||
if tx < minX or tx > maxX or ty < minY or ty > maxY:
|
||||
continue
|
||||
|
||||
neighbourList.append(Coordinate(tx, ty))
|
||||
|
||||
return neighbourList
|
||||
Loading…
Reference in New Issue
Block a user