Neighbour change api

This commit is contained in:
Pitchaya Boonsarngsuk
2018-02-05 16:33:46 +00:00
parent e7dc79d97e
commit cf813a58c8
4 changed files with 13 additions and 12 deletions

View File

@@ -20,7 +20,8 @@ export default function () {
sampleSize = 10,
stableVelocity = 0,
stableVeloHandler = null,
dataSizeFactor;
dataSizeFactor,
latestVelocityDiff = 0;
/**
* Apply spring forces at each simulation iteration.
@@ -29,7 +30,7 @@ export default function () {
function force(alpha) {
let n = nodes.length;
// Cache old velocity for comparison later
if (stableVeloHandler!==null && stableVelocity!=0) {
if (stableVeloHandler!==null && stableVelocity>=0) {
for (let i = n-1, node; i>=0; i--) {
node = nodes[i];
node.oldvx = node.vx;
@@ -53,13 +54,14 @@ export default function () {
}
// Calculate velocity changes, aka force applied.
if (stableVeloHandler!==null && stableVelocity!=0) {
if (stableVeloHandler!==null && stableVelocity>=0) {
let velocityDiff = 0;
for (let i = n-1, node; i>=0; i--) {
node = nodes[i];
velocityDiff += Math.abs(Math.hypot(node.vx-node.oldvx, node.vy-node.oldvy));
}
velocityDiff /= n*(neighbourSize+sampleSize);
latestVelocityDiff = velocityDiff;
if(stableVeloHandler!==null && velocityDiff<stableVelocity){
stableVeloHandler();
@@ -191,11 +193,11 @@ export default function () {
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), force) : distance;
};
force.velocity = function () {
return getAvgVelocity();
force.latestAccel = function () {
return latestVelocityDiff;
};
force.stableVeloHandler = function (_) {
force.onStableVelo = function (_) {
return arguments.length ? (stableVeloHandler = _, force) : stableVeloHandler;
};