Latent Lane
Murmuration

Seven birds

Seven birds follow three rules: keep apart, stay close, turn like the birds next to you1. Real starlings turn slowly, one after another3. Each bird bends the grid as it flies, and leaves a line of code behind it. Your cursor is a hawk. The birds run from it.

Neighbours
Ni={j≠i:the k nearest to i},k=6

The six nearest, near or far: what starlings do2. With seven birds, each sees all the others.

Separation
si=−∑j∈Ni,dij<rsxj−xidij2,dij=‖xj−xi‖

Keep apart. A neighbour inside the radius pushes, harder the closer it is1.

Cohesion
ci=1k∑j∈Nixj−xi

Stay together: a pull toward the neighbours' middle.

The hawk
ei=xi−xhdih2[dih<re]

The cursor. A bird inside the radius flees it, harder the closer it comes. A cursor at rest for three seconds is no hawk; a finger is one while it touches.

The roost
bi=−max(0,ρi−ρ0)1−ρ0ρ^i
ρi=‖(xi−xcW/2,yi−ycH/2)‖

The window is a roost. Past its middle, a pull back toward the centre that grows to the edge, as a starling model holds a flock over its roost4. The words are not in its way: the birds fly free and dim behind them.

The force
Fi=clipFmax(wssi+wcci+weei+wbbi)

The four pulls, weighted, cut to a ceiling1. It does not move the bird: it asks for a turn and a change of pace.

The turn
dωidt=η(Fi·n^iui−ωi)+Jk∑j∈Nisin(φj−φi)+σξi,dφidt=ωi

The turn rate has inertia. The sideways force asks for a rate and the rate eases toward it; the neighbours' headings pull on it too, so a turn crosses the flock as a wave, as measured on starlings3. The noise is on the rate, so the heading stays smooth.

The pace
duidt=Fi·t^i−γ(ui−u0),t^i=(cosφi,sinφi),n^i=(−sinφi,cosφi)

The forward force speeds the bird or slows it; a spring pulls the pace back to cruise4. Nothing is clipped. Every second on the dials is two on the clock: the flock flies these equations at half speed.

The step
ωi←ωi+ω˙iΔt+σξiΔt,φi←φi+ωiΔt
ui←ui+u˙iΔt,xi←xi+uit^iΔt

Once a frame, in this order, each from the value just set. The noise is a unit normal draw times the root of the step, so the flock is the same at any frame rate.

The fold

The grid is a sheet seen from above. A weight dents it: every line bends toward it. The weights are the seven birds and the hawk.

The pull
u(p)=∑iAsiR2(xi−p)‖xi−p‖2+R2,p↦p+u(p)

A grid point is drawn where every weight's pull moves it. The pull peaks one radius out and vanishes at the centre, and its slope never reaches one, so lines squeeze and never cross.

The trace

A cell a bird crosses keeps a character for six seconds. The characters come in order, one bird one line, so a flight writes its program across the grid. Over words the pen lifts.

1N = 6 NEAREST 2S = -SUM (XJ-XI)/D^2 3C = MEAN(XJ) - XI 4F = WS*S + WC*C + WE*E + WB*B 5DW = ETA*(F.N/U - W) + J*MEAN SIN(PJ-PI) 6DU = F.T - GAMMA*(U - U0) 7X = X + U*(COS P, SIN P)*DT