Skip to content

Keep q + r + s at 0 in hexagon.lerp() and line() - #3003

Open
SwastikTripathi wants to merge 1 commit into
pythonarcade:developmentfrom
SwastikTripathi:fix/hexagon-line-cube-sum
Open

SwastikTripathi wants to merge 1 commit into
pythonarcade:developmentfrom
SwastikTripathi:fix/hexagon-line-cube-sum

Conversation

@SwastikTripathi

Copy link
Copy Markdown

Fixes #3002.

hexagon.lerp() interpolated q, r and s separately, and line() nudged all three, so for coordinates past about 20 the three floats could sum to a rounding error like 1.4e-14. HexTile.__post_init__ only accepts a sum within 1e-14 of 0, so line() and HexTile.lerp_between() raised AssertionError: q + r + s must be 0 for most pairs of hexagons on a map of any real size.

Both now build the tile from q and r and derive s = -q - r (a small _from_qr helper), which makes q + r + s exactly 0. The values are the same as before up to that rounding, so round() and the line's hexes don't change.

Tests:

  • test_hex_line_draw_far_apart: the line between (22, 47, -69) and (-42, -18, 60) starts and ends on them, has distance + 1 hexes and steps one hex at a time, and a line to (5000, -1234, -3766) works too.
  • test_hex_lerp_keeps_cube_sum: lerp_between at t = 0, 0.1, ..., 1 keeps the sum at 0.
  • Both fail on development and pass with the change. tests/unit/test_hexagon.py passes (18 tests). With random hexagon pairs, line() failed for 51% of pairs with coordinates up to 50 and 99.9% up to 500 before, and 0 of 4000 after.
  • ruff check, ruff format --check and mypy arcade/hexagon.py are clean. I added a line to the changelog.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hexagon.line() raises AssertionError for hexagons with larger coordinates

1 participant