Skip to content

Commit

Permalink
Add d3.packEnclose test script.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 8, 2017
1 parent ece66e3 commit 98f0f6a
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/pack/find-enclose-bugs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var d3 = require("../../");

var n = 0, r = randomNormal();

while (true) {
if (!(n % 100)) process.stdout.write(".");
if (!(n % 10000)) process.stdout.write("\n" + n + " ");
++n;
var radii = new Array(20).fill().map(r).map(Math.abs),
circles = d3.packSiblings(radii.map(r => ({r: r}))),
enclose = d3.packEnclose(circles);
if (circles.some(circle => !encloses(enclose, circle))) {
process.stdout.write("\n");
process.stdout.write(JSON.stringify(radii));
process.stdout.write("\n");
}
}

function randomNormal(mu, sigma) {
var x, r;
mu = mu == null ? 0 : +mu;
sigma = sigma == null ? 1 : +sigma;
return function() {
var y;

// If available, use the second previously-generated uniform random.
if (x != null) y = x, x = null;

// Otherwise, generate a new x and y.
else do {
x = Math.random() * 2 - 1;
y = Math.random() * 2 - 1;
r = x * x + y * y;
} while (!r || r > 1);

return mu + sigma * y * Math.sqrt(-2 * Math.log(r) / r);
};
}

function encloses(a, b) {
var dx = b.x - a.x,
dy = b.y - a.y,
dr = a.r - b.r;
return dr * dr + 1e-6 > dx * dx + dy * dy;
}

0 comments on commit 98f0f6a

Please sign in to comment.