added cities

This commit is contained in:
Louis Heredero 2024-04-15 23:26:41 +02:00
parent 3784973647
commit 22bd82fb56
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
3 changed files with 43 additions and 2 deletions

View File

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

View File

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

3
vec.py
View File

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