Module

Hylograph.Forces.Tick

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

The parts of a tick that are not forces: cooling, and moving the nodes.

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

#defaultAlphaDecay Source

defaultAlphaDecay :: Number

d3's default cooling rate: alpha falls from 1 to 0.001 in 300 ticks.

#defaultVelocityRetention Source

defaultVelocityRetention :: Number

The share of its velocity a node keeps from one tick to the next: what d3 multiplies a velocity by. (d3's velocityDecay(0.4) is the share lost; this is one minus it.)

#cool Source

cool :: { alphaDecay :: Number, alphaTarget :: Number } -> Number -> Number

d3: alpha += (alphaTarget - alpha) * alphaDecay. Called before the forces of a tick, with the tick's forces given the result.

#decay Source

decay :: { alphaDecay :: Number, alphaMin :: Number, alphaTarget :: Number } -> Number -> Number

Cooling as Hylograph.ForceEngine.Core.decayAlpha has it: d3's step, and then zero once alpha has fallen below alphaMin, which is how that loop knows to stop.

#initializeNodes Source

initializeNodes :: forall row. Array (Node (Pinnable row)) -> Effect Unit

d3's initializeNodes: give every node its index; put a pinned node on its pin; give a node with no position a place on d3's phyllotaxis spiral; and stop a node with no velocity.

node.index = i;
if (node.fx != null) node.x = node.fx;
if (node.fy != null) node.y = node.fy;
if (isNaN(node.x) || isNaN(node.y)) {
  var radius = initialRadius * Math.sqrt(0.5 + i), angle = i * initialAngle;
  node.x = radius * Math.cos(angle);
  node.y = radius * Math.sin(angle);
}
if (isNaN(node.vx) || isNaN(node.vy)) node.vx = node.vy = 0;

A field that is missing reads as NaN here, as it does in d3.

#integrate Source

integrate :: forall row. Number -> Array (Node (Pinnable row)) -> Effect Unit

d3's position update, after the forces of a tick:

if (node.fx == null) node.x += node.vx *= velocityDecay;
else node.x = node.fx, node.vx = 0;

and the same for y. The position is written before the velocity, and neither write changes what the other one reads.

#pin Source

pin :: forall row. Array (Node (Pinnable row)) -> Effect Unit

Put every pinned coordinate back on its pin and stop it there: what the Rust kernel does after a hard containment, so that a pin beats a region.