Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 9 additions & 7 deletions arcade/hexagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)]

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_hexagon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading