Fix bug divide by 0 by adding jiggle

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-31 15:59:00 +00:00
parent 8422175f0a
commit e700df5059
3 changed files with 7 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
import {pointOnCircle, sumDistError} from "./helpers"; import {pointOnCircle, sumDistError} from "./helpers";
import jiggle from "../jiggle";
export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleSubset, realDistances) { export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleSubset, realDistances) {
let let
@@ -43,8 +44,6 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS
return sumDistError(pointOnCircle(nearNeighbour.x, nearNeighbour.y, angle, radius), sampleSubset, realDistances); return sumDistError(pointOnCircle(nearNeighbour.x, nearNeighbour.y, angle, radius), sampleSubset, realDistances);
}); });
let newPoint = pointOnCircle(nearNeighbour.x, nearNeighbour.y, angle, radius); let newPoint = pointOnCircle(nearNeighbour.x, nearNeighbour.y, angle, radius);
// console.log(newPoint);
node.x = newPoint.x; node.x = newPoint.x;
node.y = newPoint.y; node.y = newPoint.y;
@@ -65,8 +64,9 @@ function sumForcesToSample(node, samples, sampleCache) {
var sample = samples[i]; var sample = samples[i];
if(sample === node) continue; if(sample === node) continue;
let x = node.x - sample.x, // jiggle so l won't be zero and divide by zero error after this
y = node.y - sample.y, x = node.x - sample.x || jiggle();
y = node.y - sample.y || jiggle();
l = Math.sqrt(x * x + y * y); l = Math.sqrt(x * x + y * y);
l = (l - sampleCache[i]) / l; l = (l - sampleCache[i]) / l;

View File

@@ -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 (var k = 0, n = nodes.length, source, target, i, j, x, y, l; k < iterations; ++k) {
// For each link // For each link
for (i = 1; i < n; i++) for (j = 0; j < i; j++) { 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]; source = nodes[i];
target = nodes[j]; target = nodes[j];
x = target.x + target.vx - source.x - source.vx || jiggle(); x = target.x + target.vx - source.x - source.vx || jiggle();

View File

@@ -79,7 +79,7 @@ export default function () {
*/ */
function setVelocity(source, target, dist, alpha) { function setVelocity(source, target, dist, alpha) {
let x, y, l; 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(); x = target.x + target.vx - source.x - source.vx || jiggle();
y = target.y + target.vy - source.y - source.vy || jiggle(); y = target.y + target.vy - source.y - source.vy || jiggle();
l = Math.sqrt(x * x + y * y); l = Math.sqrt(x * x + y * y);