Repository navigation
Use even-r layout for HexGrid cell positions - #3959
Conversation
|
Performance benchmarks:
|
|
Performance benchmarks:
|
|
I checked this branch independently, and it looks right to me. For HexGrids of several sizes, with and without torus, I compared each cell's neighbors with the cells exactly one hexagon spacing away (distance sqrt(3)). For the torus I included the wrapped images, which the test in this PR skips. On While doing this I found a separate problem in the same function, |
|
Thanks for checking it, and good catch on the torus case. I tried #3979 on top of this branch: the grid.py changes merge cleanly, and with both in, |
HexGrid connects cells using even-r offsets (even rows shifted right), as its docstring says, and the hex mesh in mpl_space_drawing uses the same layout. cell.position was computed with odd rows shifted instead, so on every cell some connected neighbours were not touching hexagons when drawn at cell.position. Shift even rows in _init_hex_geometry so positions agree with the connections. Connectivity itself is unchanged.
736447d to
109c11d
Compare
|
Rebased onto current main, no conflicts. The earlier (Disclosure: I used Claude to write this comment.) |
There was a problem hiding this comment.
Checked this locally. On main every cell's cell.position disagrees with its connections (36/36 on 6x6, 30/30 on 6x5, 28/28 on 7x4). With the one-line change it is 0 for all of them, and the hexagons from HexSpaceDrawer still fit inside its plot limits for odd and even heights.
I also stacked my #3952 (agents drawn at cell.position) on top: its hexagon-centre test passes on both backends, and the full suite passes apart from test_hex_grid_lookups, whose old odd-r values this PR updates. So with both in, hexagons, agents and connections line up, as the description says.
On the changed positions: EwoutH's #3940 lists "cell centers change directly, we're in alpha, so no deprecation cycle" as a decision, so this fits that. A line in the migration guide might still help anyone who stored hex positions.
flowchart LR
A["_connect_cells_2d<br/>even-r: even rows shifted"] --> C{"do positions<br/>match connections?"}
B["cell.position<br/>main: odd rows shifted"] --> C
C -->|main| D["no: neighbours drawn<br/>apart, find_nearest_cell off"]
B2["cell.position<br/>PR: even rows shifted"] --> C2{"match?"}
A --> C2
C2 -->|PR| E["yes: hexagons, agents<br/>and connections line up"]
E --> F["+ #3952:<br/>agents at cell.position"]
LGTM.
|
Thanks @AshNicolus for checking it locally and for stacking #3952 on top, good to know hexagons, agents and connections line up with both in. I agree a migration guide line would help anyone who stored hex positions. I can add one to (Disclosure: I used Claude to write this comment.) |
HexGrid connects cells using even-r offsets (even rows shifted right), as its docstring says, and the hex mesh in mpl_space_drawing uses the same layout. cell.position was computed with odd rows shifted instead, so on every cell some connected neighbours were not touching hexagons when drawn at cell.position. Shift even rows in _init_hex_geometry so positions agree with the connections. Connectivity itself is unchanged. Co-authored-by: Jan Kwakkel <[email protected]> (cherry picked from commit e1ca9ea)
Pre-PR Checklist
Summary
HexGridbuildscell.positionwith odd rows shifted right, but connects cells as even-r (even rows shifted). Drawn atcell.position, every cell has connections that aren't touching hexagons. This makes the positions follow the connections.Bug / Issue
Fixes #3958. Picture and repro are in the issue.
Implementation
One line in
HexGrid._init_hex_geometry: the row offset is now0.5 * ((row + 1) % 2)instead of0.5 * (row % 2), so even rows are the shifted ones._connect_cells_2dis not touched, so neighbours, movement andHexGridMovingAgentbehave exactly as before. Onlycell.position(and sofind_nearest_celland the hex drawing) changes. The new positions are the same centres the old mesh inmpl_space_drawing.pyuses.Testing
test_hex_grid_lookupspinned the odd-r positions (its comment even said "Odd-R"), so I updated the three expected positions to even-r.test_hex_grid_positions_match_connections: every pair of touching hexagons (centressqrt(3)apart) must be connected, for torus and non-torus. It fails on main for all cells and passes here.ruff formatandruff checkare clean.Additional Notes