CodeRigor

LESSON

Even placement with Poisson discs

Scatter points that are never closer than a radius yet leave no big gaps: a background grid, annulus candidates, and a 5 by 5 neighbour check, tested by brute force.

Scattering trees, rocks or spawn points with plain random numbers gives clumps and bald patches, because random points ignore each other. A Poisson disc sample fixes that with one rule: no two points are closer than a radius r. Points still look random, but they are spread evenly. This lesson pairs with [Unity] Procedural Object Placement (E01: poisson disc sampling) by Sebastian Lague, linked under Watch & read. The video covers the algorithm as a grid, candidate points and a validation step; here you build it in NumPy and test it against brute force.

The method is Bridson's. Start with one random point and an active list that holds points which may still have room around them. Pick a random active point and draw up to tries candidates in the ring between r and 2r around it. The first candidate that is inside the rectangle and at least r from every stored point is kept and becomes active. If none of the candidates works, the point leaves the active list. When the list is empty the sample is finished.

Checking a candidate against every stored point would cost a pass over all of them each time. The background grid avoids that. Give each cell the side r / √2, so its diagonal is exactly r. Two points in one cell would then be closer than r, so a cell holds at most one point, and the grid can be a plain integer array with -1 for an empty cell and a point index otherwise. To validate a candidate you look only at the cells around its own.

Python
import math

cell = 1 / math.sqrt(2)               # radius 1
print(round(cell * math.sqrt(2), 6))  # the diagonal of a cell
for k in (1, 2, 3):                   # two points k cells apart in x
    print(k, round((k - 1) * cell, 3))  # the closest they can be

The second and third lines of output explain how far to look. Points two cells apart can be 0.707 r apart, which is closer than r, so they matter. Points three cells apart are at least 1.414 r apart and never matter. The neighbourhood is therefore the 5 by 5 block of cells centred on the candidate's cell, clipped at the grid edge.

The 5 by 5 block around a candidate
A 4 by 3 rectangle with radius 1 has 6 by 5 cells. Numbers are the indices of stored points. A candidate landing in the dark cell is compared only with the points in the shaded block: 7, 5, 3, 1, 2, 6 and 4.
The 5 by 5 block around a candidate
A 4 by 3 rectangle with radius 1 has 6 by 5 cells. Numbers are the indices of stored points. A candidate landing in the dark cell is compared only with the points in the shaded block: 7, 5, 3, 1, 2, 6 and 4.

Now the candidate itself. A candidate is placed at an angle drawn uniformly from 0 to 2π and a distance d from the centre. Drawing d uniformly between r and 2r looks natural, but the ring has more area far from the centre than near it, so equal steps in d would put too many candidates close in. To spread them evenly by area, draw d² uniformly between r² and 4r² and take the square root: d = r √(1 + 3u) for u uniform in [0, 1).

Python
import math
import numpy as np

rng = np.random.default_rng(0)
by_area = np.sqrt(rng.uniform(1, 4, 4000))
by_distance = rng.uniform(1, 2, 4000)
cut = math.sqrt(2.5)   # half of the ring's area lies closer than this
print(round(float((by_area < cut).mean()), 2), round(float((by_distance < cut).mean()), 2))

Half of the area-uniform draws fall inside 1.581 r, as they should. With a uniform distance it is 58 percent, so the sample crowds toward the point it grew from.

Share of candidates closer than d
Distance d is measured in units of r. Uniform by area follows (d² - 1) / 3, which stays below the straight line d - 1, so fewer candidates land near the inner edge.
Share of candidates closer than d
Distance d is measured in units of r. Uniform by area follows (d² - 1) / 3, which stays below the straight line d - 1, so fewer candidates land near the inner edge.

Here is a worked case for the validity test, with radius 1. A point is stored at (0.7, 0.1). A candidate at (1.42, 0.1) lies in a different cell, two columns over.

Python
import math

cell = 1 / math.sqrt(2)
stored, cand = (0.7, 0.1), (1.42, 0.1)
print(int(stored[0] / cell), int(cand[0] / cell))
print(round(math.dist(stored, cand), 2))

The cells are 0 and 2, yet the distance is only 0.72, so the candidate must be rejected.

Two mistakes are common. The first is to scan only the 3 by 3 block, the cell and its eight neighbours. It feels complete, and the sample looks fine to the eye, but the example above slips through, and so do many real neighbours, leaving pairs closer than r. The second is the uniform distance described earlier. Its output still respects r, so only a test of the distribution notices it. Other slips are to make the cell side r, which allows two points in one cell, to forget the rectangle bounds, and to drop a point from the active list after one failed candidate, which leaves holes.

The tests check the result against independent facts. Every pair of points is compared by brute force for spacing, and every point must be inside the rectangle. Maximality is measured on a fine grid of locations: over 40 seeds the farthest any location lies from a sample was 1.53 r, below the 2r ceiling, and at most 1.2 percent of locations were uncovered, so the tests allow a gap of 1.8 r and 2 percent. The candidate draws are checked for the half-area rule above. These tests do not show that the pattern is the best possible blue noise, and the thresholds come from measuring this implementation, not from a proof. The sample also depends on the generator, so only the same seed reproduces the same points.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →