From 012dbdf805196f83dd3cc521b1187ad3f045d35b Mon Sep 17 00:00:00 2001 From: Swastik Tripathi Date: Thu, 26 Nov 2026 08:57:32 +0530 Subject: [PATCH] Keep q + r + s at 0 in hexagon.lerp() and line() --- CHANGELOG.md | 1 + arcade/hexagon.py | 16 +++++++++------- tests/unit/test_hexagon.py | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0d392685..847a0f627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Arcade [PyPi Release History](https://pypi.org/project/arcade/#history) page. ## Unreleased ### Fixes +- Fixed `arcade.hexagon.line()` and `arcade.hexagon.lerp()` raising `AssertionError: q + r + s must be 0` for most pairs of hexagons with coordinates past about 20, such as `line(HexTile(22, 47, -69), HexTile(-42, -18, 60))` ([#3002](https://github.com/pythonarcade/arcade/issues/3002)). Interpolating `s` on its own left `q + r + s` a rounding error away from 0, over the tolerance `HexTile` checks. `s` is now derived from `q` and `r`. - Fixed `DefaultTextureAtlas` raising `KeyError` when a texture was added while the garbage collector freed the last other texture with the same image and transform. The collector could run in the middle of adding the new texture, and the freed texture's finalizer removed the slot the new one was about to share. It showed up in CI on Python 3.14, whose garbage collector runs at different times, when loading a tile map. - Fixed blocky, uneven text on scaled (HiDPI) displays, such as Windows set to 125% or 150%. Arcade's windows use pyglet's `"stretch"` scaling, so text was rasterized at the window's size and then stretched to fill the larger framebuffer, giving strokes of uneven width. It looked worse after the default font changed to Liberation Sans, which shows the stretching more than Calibri did. `Text`, `draw_text` and GUI text now lay their glyphs out at the framebuffer's resolution and draw them back at the same size, so they're sharp. Nothing changes at a scale of 100%. - Positions and sizes on `Text`, such as `x`, `content_width` and `left`, are still in window units. The pyglet label in `Text.label` uses framebuffer pixels on a scaled display. diff --git a/arcade/hexagon.py b/arcade/hexagon.py index 318a85927..78ce9139a 100644 --- a/arcade/hexagon.py +++ b/arcade/hexagon.py @@ -205,13 +205,15 @@ def to_offset(self, system: offset_system) -> "OffsetCoord": raise ValueError(msg) +def _from_qr(q: float, r: float) -> HexTile: + # Deriving s from q and r keeps q + r + s at exactly 0. Computing s on its own + # can leave the sum a rounding error away from 0, which HexTile rejects. + return HexTile(q, r, -q - r) + + def lerp(a: HexTile, b: HexTile, t: float) -> HexTile: """Perform a linear interpolation between two hexagons.""" - return HexTile( - a.q * (1.0 - t) + b.q * t, - a.r * (1.0 - t) + b.r * t, - a.s * (1.0 - t) + b.s * t, - ) + return _from_qr(a.q * (1.0 - t) + b.q * t, a.r * (1.0 - t) + b.r * t) def distance(a: HexTile, b: HexTile) -> int: @@ -223,8 +225,8 @@ def line(a: HexTile, b: HexTile) -> list[HexTile]: """Return a list of hexagons between two hexagons.""" n = distance(a, b) # epsilon to nudge points by to falling on an edge - a_nudge = HexTile(a.q + 1e-06, a.r + 1e-06, a.s - 2e-06) - b_nudge = HexTile(b.q + 1e-06, b.r + 1e-06, b.s - 2e-06) + a_nudge = _from_qr(a.q + 1e-06, a.r + 1e-06) + b_nudge = _from_qr(b.q + 1e-06, b.r + 1e-06) step = 1.0 / max(n, 1) return [round(lerp(a_nudge, b_nudge, step * i)) for i in range(n + 1)] diff --git a/tests/unit/test_hexagon.py b/tests/unit/test_hexagon.py index 19e73283f..ba683a8e4 100644 --- a/tests/unit/test_hexagon.py +++ b/tests/unit/test_hexagon.py @@ -127,6 +127,29 @@ def test_hex_line_draw(): ] == line(HexTile(0, 0, 0), HexTile(1, -5, 4)) +def test_hex_line_draw_far_apart(): + # The interpolated points used to be rejected by HexTile, because + # q + r + s came out a rounding error away from 0 + a = HexTile(22, 47, -69) + b = HexTile(-42, -18, 60) + hexes = line(a, b) + assert hexes[0] == a + assert hexes[-1] == b + assert len(hexes) == a.distance_to(b) + 1 + assert all(hexes[i].distance_to(hexes[i + 1]) == 1 for i in range(len(hexes) - 1)) + + far = HexTile(5000, -1234, -3766) + assert line(HexTile(0, 0, 0), far)[-1] == far + + +def test_hex_lerp_keeps_cube_sum(): + a = HexTile(22, 47, -69) + b = HexTile(-42, -18, 60) + for i in range(11): + h = a.lerp_between(b, i / 10) + assert h.q + h.r + h.s == 0 + + def test_layout(): h = HexTile(3, 4, -7) flat = Layout(flat_orientation, Vec2(10.0, 15.0), Vec2(35.0, 71.0))