From a2c970821355895cf4edcef713adb8f16e0d8461 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 15 Apr 2024 21:18:52 +0200 Subject: [PATCH] initial commit --- map_display.py | 30 ++++++++++++++++++++++++++++++ path.py | 30 ++++++++++++++++++++++++++++++ vec.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 map_display.py create mode 100644 path.py create mode 100644 vec.py diff --git a/map_display.py b/map_display.py new file mode 100644 index 0000000..a09e081 --- /dev/null +++ b/map_display.py @@ -0,0 +1,30 @@ +from typing import Optional + +import pygame + + +class MapDisplay: + PATH_WIDTH = 5 + + def __init__(self, surf: pygame.Surface, min_lat: float, max_lat: float, min_lon: float, max_lon: float): + self.surf: pygame.Surface = surf + self.min_lat: float = min_lat + self.max_lat: float = max_lat + self.min_lon: float = min_lon + self.max_lon: float = max_lon + + def real_to_screen(self, lat: float, lon: float) -> tuple[float, float]: + x = (lon - self.min_lon) / (self.max_lon - self.min_lon) + y = (lat - self.min_lat) / (self.max_lat - self.min_lat) + + w, h = self.surf.get_size() + return x * w, y * h + + def draw_path(self, path: list[tuple[int, int]], colors: Optional[list[tuple[int, int, int]]]) -> None: + for i, (lat, lon) in enumerate(path): + x, y = self.real_to_screen(lat, lon) + col = (255, 255, 255) if colors is None else colors[i] + pygame.draw.circle(self.surf, col, (x, y), self.PATH_WIDTH) + + def draw_segment(self, path: list[tuple[int, int]], start_i: int, end_i: int) -> None: + raise NotImplementedError diff --git a/path.py b/path.py new file mode 100644 index 0000000..c3a6ebe --- /dev/null +++ b/path.py @@ -0,0 +1,30 @@ +from vec import Vec2 + + +class Path: + def __init__(self, points: list[Vec2], extra_data: list): + self.points: list[Vec2] = points + self.extra_data = extra_data + self.vecs: list[Vec2] = [] + self._init_vecs() + + def _init_vecs(self) -> None: + for i in range(1, len(self.points) - 1): + pt1 = self.points[i-1] + pt2 = self.points[i] + pt3 = self.points[i+1] + d1 = pt1 - pt2 + d2 = pt3 - pt2 + + l1 = d1.mag + l2 = d2.mag + + d1 = d1.normalized() + d2 = d1.normalized() + + d = d1 + d2 + d = d.normalized() + if d2.cross(d) < 0: + d = -d + + self.vecs.append(d) diff --git a/vec.py b/vec.py new file mode 100644 index 0000000..185e49d --- /dev/null +++ b/vec.py @@ -0,0 +1,35 @@ +from math import sqrt + + +class Vec2: + def __init__(self, x: float = 0, y: float = 0): + self.x: float = x + self.y: float = y + + @property + def mag(self) -> float: + return sqrt(self.x ** 2 + self.y ** 2) + + def normalized(self) -> "Vec2": + mag = self.mag + if mag == 0: + return Vec2() + return self / mag + + def __add__(self, v: "Vec2") -> "Vec2": + return Vec2(self.x + v.x, self.y + v.y) + + def __sub__(self, v: "Vec2") -> "Vec2": + return Vec2(self.x - v.x, self.y - v.y) + + def __mul__(self, f: float) -> "Vec2": + return Vec2(self.x * f, self.y * f) + + def __truediv__(self, f: float) -> "Vec2": + return Vec2(self.x / f, self.y / f) + + def __neg__(self) -> "Vec2": + return Vec2(-self.x, -self.y) + + def cross(self, v: "Vec2") -> float: + return self.x * v.y - self.y * v.x