From 22bd82fb56f84bf42fa902a76740d07bdca16225 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 15 Apr 2024 23:26:41 +0200 Subject: [PATCH] added cities --- map_display.py | 39 ++++++++++++++++++++++++++++++++++++++- speed_map_display.py | 3 ++- vec.py | 3 +++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/map_display.py b/map_display.py index ad28418..e55fd34 100644 --- a/map_display.py +++ b/map_display.py @@ -16,12 +16,19 @@ class MapDisplay: min_lon: float, max_lon: float, min_lat: float, - max_lat: float): + max_lat: float, + cities: list[tuple[Vec2, str, str]]): self.surf: pygame.Surface = surf self.min_lon: float = min_lon self.max_lon: float = max_lon self.min_lat: float = min_lat self.max_lat: float = max_lat + self.cities: list[tuple[Vec2, str, str]] = cities + + self.font = pygame.font.SysFont("ubuntu", 20) + + for city in self.cities: + self.draw_city(*city) def real_to_screen(self, lon: float, lat: float) -> tuple[float, float]: x = (lon - self.min_lon) / (self.max_lon - self.min_lon) @@ -57,3 +64,33 @@ class MapDisplay: points.append((pt2.x, pt2.y)) pygame.draw.lines(self.surf, (255, 255, 255), True, points) + + def draw_city(self, pos: Vec2, name: str, label_side: str) -> None: + pos2 = Vec2(*self.real_to_screen(pos.x, pos.y)) + + pygame.draw.circle(self.surf, (180, 180, 180), (pos2.x, pos2.y), 10) + + label = self.font.render(name, True, (255, 255, 255)) + label_size = Vec2(*label.get_size()) + + line_end = pos2 + label_pos = line_end - label_size / 2 + + if label_side == "above": + line_end -= Vec2(0, 20) + label_pos = line_end - label_size.scale(Vec2(0.5, 1)) + + elif label_side == "below": + line_end += Vec2(0, 20) + label_pos = line_end - label_size.scale(Vec2(0.5, 0)) + + elif label_side == "left": + line_end -= Vec2(20, 0) + label_pos = line_end - label_size.scale(Vec2(1, 0.5)) + + elif label_side == "right": + line_end += Vec2(20, 0) + label_pos = line_end - label_size.scale(Vec2(0, 0.5)) + + pygame.draw.line(self.surf, (255, 255, 255), (pos2.x, pos2.y), (line_end.x, line_end.y)) + self.surf.blit(label, (label_pos.x, label_pos.y)) diff --git a/speed_map_display.py b/speed_map_display.py index ae4125c..61d8740 100644 --- a/speed_map_display.py +++ b/speed_map_display.py @@ -12,10 +12,11 @@ class SpeedMapDisplay(MapDisplay): max_lon: float, min_lat: float, max_lat: float, + cities: list[tuple[Vec2, str, str]], min_speed_col: tuple[int, int, int], max_speed_col: tuple[int, int, int], segment_threshold: float): - super().__init__(surf, min_lon, max_lon, min_lat, max_lat) + super().__init__(surf, min_lon, max_lon, min_lat, max_lat, cities) self.min_speed_col: tuple[int, int, int] = min_speed_col self.max_speed_col: tuple[int, int, int] = max_speed_col self.segment_threshold: float = segment_threshold diff --git a/vec.py b/vec.py index 185e49d..64132fd 100644 --- a/vec.py +++ b/vec.py @@ -33,3 +33,6 @@ class Vec2: def cross(self, v: "Vec2") -> float: return self.x * v.y - self.y * v.x + + def scale(self, v: "Vec2") -> "Vec2": + return Vec2(self.x * v.x, self.y * v.y)