Module

Hylograph.Forces.ThreeD.Center

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

The centring force in three dimensions: shift every node so that their mean position sits at a given point. It moves positions, not velocities.

Ported from d3-force-3d 3.0.6, center.js (MIT, Copyright 2017 Vasco Asturiano; itself from d3-force, ISC, Mike Bostock); see LICENSE.

Not d3-force's center.js with a third coordinate: d3-force-3d counts a coordinate that is NaN as zero in the sum (sx += node.x || 0), and leaves an axis alone when its shift is zero or NaN (if (sx) node.x -= sx), which also keeps a -0 coordinate -0.

#Config Source

type Config = { strength :: Number, x :: Number, y :: Number, z :: Number }

d3-force-3d's forceCenter settings.

#defaultConfig Source

defaultConfig :: Config

d3-force-3d's defaults: the origin, at full strength.

#Center Source

newtype Center :: Row Type -> Typenewtype Center row

An initialized centring force.

#initialize Source

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

d3's force.initialize(nodes).

#apply Source

apply :: forall row. Center row -> Effect Unit

d3-force-3d's force():

for (i = 0; i < n; ++i) node = nodes[i], sx += node.x || 0, sy += node.y || 0, sz += node.z || 0;
for (sx = (sx / n - x) * strength, sy = ..., sz = ..., i = 0; i < n; ++i) {
  node = nodes[i];
  if (sx) { node.x -= sx }
  if (sy) { node.y -= sy; }
  if (sz) { node.z -= sz; }
}