Skip to content

Commit

Permalink
Simplify collision resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 3, 2019
1 parent 8830950 commit 78c71f3
Showing 1 changed file with 6 additions and 34 deletions.
40 changes: 6 additions & 34 deletions src/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ export default function Sankey() {
//
initializeNodeBreadth();
resolveCollisionsTopToBottom(1);
for (var alpha = 0.95, n = iterations; n > 0; --n, alpha *= 0.95) {
for (var alpha = 1, n = iterations; n > 0; --n, alpha *= 0.9) {
relaxRightToLeft(alpha);
resolveCollisionsTopToBottom(1);
relaxLeftToRight(alpha);
resolveCollisionsTopToBottom(0.5);
resolveCollisionsBottomToTop(0.5);
resolveCollisionsTopToBottom(1);
resolveCollisionsBottomToTop(1);
}

Expand Down Expand Up @@ -246,69 +248,39 @@ export default function Sankey() {
});
}

// Push any overlapping nodes down.
function resolveCollisionsTopToBottom(alpha) {
columns.forEach(function(nodes) {
var node,
dy,
y = y0,
n = nodes.length,
i;

// Push any overlapping nodes down.
if (sort === undefined) nodes.sort(ascendingBreadth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = (y - node.y0) * alpha;
if (dy > 0) node.y0 += dy, node.y1 += dy;
y = node.y1 + py;
}

// If the bottommost node goes outside the bounds, push it back up.
dy = (y - py - y1) * alpha;
if (dy > 0) {
y = (node.y0 -= dy), node.y1 -= dy;

// Push any overlapping nodes back up.
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = (node.y1 + py - y) * alpha;
if (dy > 0) node.y0 -= dy, node.y1 -= dy;
y = node.y0;
}
}
});
}

// Push any overlapping nodes up.
function resolveCollisionsBottomToTop(alpha) {
columns.forEach(function(nodes) {
var node,
dy,
y = y1,
n = nodes.length,
i;

// Push any overlapping nodes up.
if (sort === undefined) nodes.sort(ascendingBreadth);
for (i = n - 1; i >= 0; --i) {
node = nodes[i];
dy = (y - node.y1) * alpha;
if (dy < 0) node.y0 += dy, node.y1 += dy;
y = node.y0 - py;
}

// If the topmost node goes outside the bounds, push it back down.
dy = (y + py - y0) * alpha;
if (dy < 0) {
node.y0 -= dy, y = (node.y1 -= dy);

// Push any overlapping nodes back down.
for (i = 1; i < n; ++i) {
node = nodes[i];
dy = (node.y0 - py - y) * alpha;
if (dy < 0) node.y0 -= dy, node.y1 -= dy;
y = node.y1;
}
}
});
}
}
Expand Down

0 comments on commit 78c71f3

Please sign in to comment.