From e700df5059693a5e237cbf44be54e8d40b2ad1db Mon Sep 17 00:00:00 2001 From: Pitchaya Boonsarngsuk <2285135b@student.gla.ac.uk> Date: Wed, 31 Jan 2018 15:59:00 +0000 Subject: [PATCH] Fix bug divide by 0 by adding jiggle --- src/interpolation/interpCommon.js | 10 +++++----- src/link.js | 2 +- src/neighbourSamplingDistance.js | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/interpolation/interpCommon.js b/src/interpolation/interpCommon.js index bf8234b..009a4cb 100644 --- a/src/interpolation/interpCommon.js +++ b/src/interpolation/interpCommon.js @@ -1,4 +1,5 @@ import {pointOnCircle, sumDistError} from "./helpers"; +import jiggle from "../jiggle"; export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleSubset, realDistances) { let @@ -43,8 +44,6 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS return sumDistError(pointOnCircle(nearNeighbour.x, nearNeighbour.y, angle, radius), sampleSubset, realDistances); }); let newPoint = pointOnCircle(nearNeighbour.x, nearNeighbour.y, angle, radius); - - // console.log(newPoint); node.x = newPoint.x; node.y = newPoint.y; @@ -65,9 +64,10 @@ function sumForcesToSample(node, samples, sampleCache) { var sample = samples[i]; if(sample === node) continue; - let x = node.x - sample.x, - y = node.y - sample.y, - l = Math.sqrt(x * x + y * y); + // jiggle so l won't be zero and divide by zero error after this + x = node.x - sample.x || jiggle(); + y = node.y - sample.y || jiggle(); + l = Math.sqrt(x * x + y * y); l = (l - sampleCache[i]) / l; x *= l, y *= l; diff --git a/src/link.js b/src/link.js index f04012b..2316ea1 100644 --- a/src/link.js +++ b/src/link.js @@ -22,7 +22,7 @@ export default function(links) { for (var k = 0, n = nodes.length, source, target, i, j, x, y, l; k < iterations; ++k) { // For each link for (i = 1; i < n; i++) for (j = 0; j < i; j++) { - // jiggle so it wont divide / multiply by zero after this + // jiggle so l won't be zero and divide by zero error after this source = nodes[i]; target = nodes[j]; x = target.x + target.vx - source.x - source.vx || jiggle(); diff --git a/src/neighbourSamplingDistance.js b/src/neighbourSamplingDistance.js index 480153a..890fa5f 100644 --- a/src/neighbourSamplingDistance.js +++ b/src/neighbourSamplingDistance.js @@ -79,7 +79,7 @@ export default function () { */ function setVelocity(source, target, dist, alpha) { let x, y, l; - // jiggle so it wont divide / multiply by zero after this + // jiggle so l won't be zero and divide by zero error after this x = target.x + target.vx - source.x - source.vx || jiggle(); y = target.y + target.vy - source.y - source.vy || jiggle(); l = Math.sqrt(x * x + y * y);