Skip to content

Commit 0b71fe2

Browse files
committed
Refactor path segment direction logic to ensure consistent offsets for reversed station pairs
1 parent 8acff22 commit 0b71fe2

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

PROGRESS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@
3939
- Added station-pool generation logic so unique shapes can only appear after the 10th station slot and at most once each per run.
4040
- Updated `ARCHITECTURE.md` to reflect the latest project file structure.
4141
- Fixed passenger wait blink logic so passengers at or past the max wait threshold also blink instead of only pre-timeout passengers.
42+
- Fixed path segment offset direction to use a stable station-pair orientation so reversed A/B segments stay parallel and no longer cross each other.

src/entity/path_segment.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,28 @@
44
from entity.segment import Segment
55
from entity.station import Station
66
from geometry.line import Line
7+
from geometry.point import Point
78
from geometry.utils import direction
89
from shortuuid import uuid # type: ignore
910
from type import Color
1011

1112

1213
class PathSegment(Segment):
14+
@staticmethod
15+
def _canonical_pair_direction(
16+
start_station: Station, end_station: Station
17+
) -> Point:
18+
start_position = start_station.position
19+
end_position = end_station.position
20+
# Use one stable ordering per station pair so offsets are consistent
21+
# for both A->B and B->A segments.
22+
if (
23+
(start_position.left, start_position.top)
24+
<= (end_position.left, end_position.top)
25+
):
26+
return direction(start_position, end_position)
27+
return direction(end_position, start_position)
28+
1329
def __init__(
1430
self,
1531
color: Color,
@@ -23,9 +39,7 @@ def __init__(
2339
self.end_station = end_station
2440
self.path_order = path_order
2541

26-
start_point = start_station.position
27-
end_point = end_station.position
28-
direct = direction(start_point, end_point)
42+
direct = self._canonical_pair_direction(start_station, end_station)
2943
buffer_vector = direct * path_order_shift
3044
buffer_vector = buffer_vector.rotate(90)
3145

test/test_path.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from entity.get_entity import get_random_station, get_random_stations
1212
from entity.metro import Metro
1313
from entity.path import Path
14+
from entity.path_segment import PathSegment
1415
from entity.station import Station
1516
from geometry.circle import Circle
1617
from geometry.point import Point
@@ -178,6 +179,16 @@ def test_move_metro_looped_first_segment_backward_wraps(self):
178179
path.move_metro(metro, 100000)
179180
self.assertEqual(metro.current_segment_idx, len(path.segments) - 1)
180181

182+
def test_path_segment_keeps_parallel_offsets_for_reversed_station_pair(self):
183+
station_a = Station(Circle(station_color, station_size), Point(0, 0))
184+
station_b = Station(Circle(station_color, station_size), Point(100, 40))
185+
segment_ab = PathSegment((10, 10, 10), station_a, station_b, path_order=2)
186+
segment_ba = PathSegment((10, 10, 10), station_b, station_a, path_order=2)
187+
188+
offset_from_a = segment_ab.segment_start - station_a.position
189+
offset_from_b = segment_ba.segment_start - station_b.position
190+
self.assertEqual(offset_from_a, offset_from_b)
191+
181192

182193
if __name__ == "__main__":
183194
unittest.main()

0 commit comments

Comments
 (0)