From 02b4f869b94f6bc9f9b127ce066c8b3dab3868d4 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 30 Nov 2021 01:02:57 +0100 Subject: [PATCH] angling away --- coordinate.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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)]