Fix bug divide by 0 by adding jiggle
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user