Skip to content

Commit

Permalink
Slightly cleaner.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 7, 2016
1 parent a65deef commit f5ba7f1
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions src/treemap/binary.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
export default function(parent, x0, y0, x1, y1) {
var nodes = parent.children;
var sums = [0];
for (var i = 0; i < nodes.length; i++) {
sums[i+1] = sums[i] + nodes[i].value;
var nodes = parent.children,
i, n = nodes.length,
sum, sums = new Array(n + 1);

for (sums[0] = sum = i = 0; i < n; ++i) {
sums[i + 1] = sum += nodes[i].value;
}
partition(nodes, sums, 0, nodes.length, parent.value, x0, y0, x1, y1);
}

partition(0, n, parent.value, x0, y0, x1, y1);

function partition(nodes, sums, i, j, value, x0, y0, x1, y1) {
if (i >= j - 1) {
nodes = nodes[i];
nodes.x0 = x0, nodes.y0 = y0;
nodes.x1 = x1, nodes.y1 = y1;
return;
}
function partition(i, j, value, x0, y0, x1, y1) {
if (i >= j - 1) {
var node = nodes[i];
node.x0 = x0, node.y0 = y0;
node.x1 = x1, node.y1 = y1;
return;
}

var offset = sums[i];
var goal = (value / 2) + offset;
var valueOffset = sums[i],
valueTarget = (value / 2) + valueOffset,
k = i + 1,
hi = j - 1;

var k = i+1, ub = j-1, mid;
while(k < ub) {
mid = (k + ub) >>> 1;
if (sums[mid] < goal) {
k = mid+1;
} else {
ub = mid;
while (k < hi) {
var mid = k + hi >>> 1;
if (sums[mid] < valueTarget) k = mid + 1;
else hi = mid;
}
}

var valueLeft = sums[k] - offset;
var valueRight = value - valueLeft;
var valueLeft = sums[k] - valueOffset,
valueRight = value - valueLeft;

if ((y1 - y0) > (x1 - x0)) {
var yk = (y0 * valueRight + y1 * valueLeft) / value;
partition(nodes, sums, i, k, valueLeft, x0, y0, x1, yk);
partition(nodes, sums, k, j, valueRight, x0, yk, x1, y1);
} else {
var xk = (x0 * valueRight + x1 * valueLeft) / value;
partition(nodes, sums, i, k, valueLeft, x0, y0, xk, y1);
partition(nodes, sums, k, j, valueRight, xk, y0, x1, y1);
if ((y1 - y0) > (x1 - x0)) {
var yk = (y0 * valueRight + y1 * valueLeft) / value;
partition(i, k, valueLeft, x0, y0, x1, yk);
partition(k, j, valueRight, x0, yk, x1, y1);
} else {
var xk = (x0 * valueRight + x1 * valueLeft) / value;
partition(i, k, valueLeft, x0, y0, xk, y1);
partition(k, j, valueRight, xk, y0, x1, y1);
}
}
}

0 comments on commit f5ba7f1

Please sign in to comment.