~/lab/flock/ — zsh
cd ..thomas@parkin ~/lab $ cd flock/ && ./run# emergent flocking from three simple rules
man boids

Craig Reynolds' boids (1987). Each bird follows three local rules and nothing else:separation (don't crowd your neighbours), alignment (steer towards their average heading) andcohesion (steer towards their average position). Flocks, swirls and splits all emerge from that. No boid knows the flock exists.

Your cursor is a predator they flee from. Set cohesion to 0 to watch the flock dissolve into wandering groups, or separation to 0 to watch them collapse into clumps. Colour shows heading.

Checking every pair would be O(n²), so the space is bucketed into a grid of cells as wide as the view radius. Each boid then only checks the 3×3 block of cells around it. Turn on "show neighbours" to see the grid and one boid's view.