Module

Hylograph.Forces.Collide

Package
purescript-hylograph-forces
Repository
afcondon/purescript-hylograph-forces

The collision force: treat each node as a circle and push overlapping circles apart.

Ported from d3-force 3.0.0, collide.js (ISC, Copyright 2010-2021 Mike Bostock); see LICENSE.

The shape is the many-body force's. Each iteration builds a quadtree of where the nodes are about to be (x + vx), turns it into a chain of Regions that each know the largest radius inside them (d3's prepare), and walks the chain once per node. A walk is pure: it returns the node's new velocity and the new velocities of the nodes it pushed, and those are written when the walk has returned. That is the same as d3 writing them as it goes, because a walk meets each other node at most once and never reads back a velocity it has changed.

Like d3, this looks only at the newest of several nodes at exactly the same place; the older ones are not collided with until something else moves them.

#Config Source

type Config :: Row Type -> Typetype Config row = { alphaScaled :: Boolean, include :: Node row -> Boolean, iterations :: Int, radius :: Node row -> Number, strength :: Number }

d3's forceCollide settings. radius is asked once per node when the force is initialized.

#defaultConfig Source

defaultConfig :: forall row. Config row

d3's defaults: radius 1, full strength, one iteration; and the two things d3 does not have turned off.

Those two are the Rust kernel's "yielding collision" (apply_collide in force-kernel/src/forces.rs), held to it by test/differential.mjs:

  • alphaScaled: the strength used is strength * alpha, so the collision weakens as the simulation cools and gives way to forces that hold a node at a place;
  • include: a pair is tested only if both of its nodes are included. A node left out neither pushes nor is pushed. (It is still in the quadtree, where it can only widen the search.)

#Collide Source

newtype Collide :: Row Type -> Typenewtype Collide row

An initialized collision force.

#initialize Source

initialize :: forall row. Config row -> Source -> Array (Node row) -> Collide row

d3's force.initialize(nodes, random). A node's index is its place in the array, which is what d3's simulation sets node.index to.

#apply Source

apply :: forall row. Collide row -> Number -> Effect Unit

d3's force(): resolve overlaps, iterations times over. Alpha is used only by a collision whose strength is scaled by it.