Skip to content

Commit

Permalink
fix graph nan bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloChecked committed Oct 10, 2024
1 parent 6843242 commit ec53722
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions blog/scripts/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function calculateGraph(
const distance = nodeB.point.distanceTo(nodeA.point);
const minDistance = nodeA.radius + nodeB.radius + MARGIN;

if (distance < minDistance) {
if (distance < minDistance && distance > 0) {
const force = REPULSION_CONSTANT / (distance * distance);
const forcePoint = nodeA.point
.minus(nodeB.point)
Expand All @@ -85,15 +85,17 @@ export function calculateGraph(
}

const distance = nodeA.point.distanceTo(nodeB.point);
const displacement = distance - SPRING_LENGTH;
const force = SPRING_CONSTANT * displacement;
const forcePoint = nodeB.point
.minus(nodeA.point)
.divideScalar(distance)
.multiplyScalar(force);

forces[edge.source] = forces[edge.source].plus(forcePoint);
forces[edge.target] = forces[edge.target].minus(forcePoint);
if (distance > 0) {
const displacement = distance - SPRING_LENGTH;
const force = SPRING_CONSTANT * displacement;
const forcePoint = nodeB.point
.minus(nodeA.point)
.divideScalar(distance)
.multiplyScalar(force);

forces[edge.source] = forces[edge.source].plus(forcePoint);
forces[edge.target] = forces[edge.target].minus(forcePoint);
}
}

return forces;
Expand Down Expand Up @@ -134,7 +136,7 @@ export function calculateGraph(
const distance = nodeB.point.distanceTo(nodeA.point);
const minDistance = nodeA.radius + nodeB.radius + MARGIN;

if (distance < minDistance) {
if (distance < minDistance && distance > 0) {
const overlap = minDistance - distance;

const direction = nodeB.point
Expand Down

0 comments on commit ec53722

Please sign in to comment.