Fix bug divide by 0 by adding jiggle
This commit is contained in:
@@ -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,9 +64,10 @@ 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();
|
||||||
l = Math.sqrt(x * x + y * y);
|
y = node.y - sample.y || jiggle();
|
||||||
|
l = Math.sqrt(x * x + y * y);
|
||||||
|
|
||||||
l = (l - sampleCache[i]) / l;
|
l = (l - sampleCache[i]) / l;
|
||||||
x *= l, y *= 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 (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();
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user