A search outward in growing square rings is not nearest-first
Fact
Searching a grid outward in square rings (every cell at Chebyshev distance r, then r + 1) is the usual way to find the nearest cell that qualifies. Returning the first match is not nearest-first, in two ways:
- Within a ring, the loop order decides. A corner of ring
risr√2away and its edge midpoints onlyr, and a loop that starts at(-r, -r)meets the corner first. - Across rings, ring
r + 1holds cellsr + 1away, which is nearer than ringr's corners oncer ≥ 3, sincer√2 > r + 1.
Why it matters
The answer is always close and always valid, so it looks right. It shows up as something placed a little to one side, such as a creature appearing at the far corner of a pool instead of at the edge that was tapped. That reads as a placement quirk, not as a search bug.
How to apply
- Keep the best match by true (Euclidean) distance, skip cells that cannot beat it, and stop only once the ring index is past the best distance found.
- Test with a target whose nearest qualifying cell is straight ahead on a ring's edge while the same ring also has qualifying corners.