diff --git a/coordinate.py b/coordinate.py index 34140b5..3da267a 100644 --- a/coordinate.py +++ b/coordinate.py @@ -1,6 +1,6 @@ from __future__ import annotations from dataclasses import dataclass -from math import sqrt, inf +from math import sqrt, inf, atan2, degrees from typing import Union, List @@ -61,6 +61,19 @@ class Coordinate: return neighbourList + def getAngleTo(self, target: Coordinate, normalized: bool = False) -> float: + """normalized returns an angle going clockwise with 0 starting in the 'north'""" + dx = target.x - self.x + dy = target.y - self.y + if not normalized: + return degrees(atan2(dy, dx)) + else: + angle = degrees(atan2(dx, dy)) + if dx >= 0: + return 180.0 - angle + else: + return 180.0 + abs(angle) + @staticmethod def generate(from_x: int, to_x: int, from_y: int, to_y: int) -> List[Coordinate]: return [Coordinate(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]