angling away

This commit is contained in:
Stefan Harmuth 2021-11-30 01:02:57 +01:00
parent fc0178288e
commit 02b4f869b9

View File

@ -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)]