-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
97 lines (74 loc) · 3.1 KB
/
utils.py
File metadata and controls
97 lines (74 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import colorsys
import random
from typing import List, Tuple
import numpy as np
from config import passenger_size, station_color, station_shape_type_list, station_size
from geometry.circle import Circle
from geometry.cross import Cross
from geometry.diamond import Diamond
from geometry.pentagon import Pentagon
from geometry.point import Point
from geometry.rect import Rect
from geometry.shape import Shape
from geometry.star import Star
from geometry.triangle import Triangle
from geometry.type import ShapeType
from type import Color
def get_random_position(width: int, height: int) -> Point:
padding_ratio = 0.1
return Point(
left=round(
width * (padding_ratio + np.random.rand() * (1 - padding_ratio * 2))
),
top=round(
height * (padding_ratio + np.random.rand() * (1 - padding_ratio * 2))
),
)
def get_random_color() -> Color:
return hue_to_rgb(np.random.rand())
def hue_to_rgb(hue: float, saturation: float = 1.0, value: float = 1.0) -> Color:
return tuple(255 * np.asarray(colorsys.hsv_to_rgb(hue, saturation, value)))
def hue_circular_distance(hue_a: float, hue_b: float) -> float:
distance = abs(hue_a - hue_b)
return min(distance, 1 - distance)
def pick_distinct_hue(existing_hues: List[float], candidate_hues: List[float]) -> float:
if not candidate_hues:
raise ValueError("candidate_hues must not be empty")
if not existing_hues:
return candidate_hues[0]
return max(
candidate_hues,
key=lambda candidate: min(
hue_circular_distance(candidate, existing_hue)
for existing_hue in existing_hues
),
)
def get_random_shape(
shape_type_list: List[ShapeType], color: Color, size: int
) -> Shape:
shape_type = random.choice(shape_type_list)
return get_shape_from_type(shape_type, color, size)
def get_random_station_shape() -> Shape:
return get_random_shape(station_shape_type_list, station_color, station_size)
def get_random_passenger_shape() -> Shape:
return get_random_shape(station_shape_type_list, get_random_color(), passenger_size)
def tuple_to_point(coords: Tuple[int, int]) -> Point:
return Point(left=coords[0], top=coords[1])
def get_shape_from_type(shape_type: ShapeType, color: Color, size: int) -> Shape:
if shape_type == ShapeType.RECT:
return Rect(color=color, width=2 * size, height=2 * size)
if shape_type == ShapeType.CIRCLE:
return Circle(color=color, radius=size)
if shape_type == ShapeType.TRIANGLE:
return Triangle(color=color, size=size)
if shape_type == ShapeType.CROSS:
return Cross(color=color, size=size)
if shape_type == ShapeType.DIAMOND:
return Diamond(color=color, size=size)
if shape_type == ShapeType.PENTAGON:
return Pentagon(color=color, size=size)
if shape_type == ShapeType.STAR:
return Star(color=color, size=size)
raise ValueError(f"Unsupported shape type: {shape_type}")
def within_time_window(game_time_ms: int, time_mark_ms: int, window_ms: int) -> bool:
return window_ms <= game_time_ms - time_mark_ms < (2 * window_ms)