Back to problems Simulation / proximity

Uniform Grid Spatial Hash

AeroSpace • O(1) cell lookup • 3×3 neighborhood stencil

Partition the plane into square cells of side R. Each point maps to cell indices (⌊x/R⌋, ⌊y/R⌋). Store buckets in a hash map. A neighbor query draws a disk of radius R around the query point: we only return other points inside that disk. The 3×3 cells are a cheap superset of candidates (when cell size = R)—some checked points can still lie outside the disk; those are skipped for the result, not removed from the map. Then filter with a real distance test (e.g. hypot). Avoids comparing every pair O(N²) for dense local queries.

InsertO(1)
NeighborsO(k)k pts in 9 cells
SpaceO(N)
Step0/0
Ready
Press Play or Step. Demo uses cell size / radius R = 10 and integer-ish coordinates.