initial commit

This commit is contained in:
Louis Heredero 2024-04-15 21:18:52 +02:00
parent 48011668e5
commit a2c9708213
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
3 changed files with 95 additions and 0 deletions

30
map_display.py Normal file
View File

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

30
path.py Normal file
View File

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

35
vec.py Normal file
View File

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