Skip to content

Commit

Permalink
fix #188; floating point error in encloseBasis3
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 2, 2022
1 parent c3833aa commit a071e70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pack/enclose.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function encloseBasis3(a, b, c) {
A = xb * xb + yb * yb - 1,
B = 2 * (r1 + xa * xb + ya * yb),
C = xa * xa + ya * ya - r1 * r1,
r = -(A ? (B + Math.sqrt(B * B - 4 * A * C)) / (2 * A) : C / B);
r = -(Math.abs(A) > 1e-6 ? (B + Math.sqrt(B * B - 4 * A * C)) / (2 * A) : C / B);
return {
x: x1 + xa + xb * r,
y: y1 + ya + yb * r,
Expand Down
18 changes: 18 additions & 0 deletions test/pack/enclose-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assert from "assert";
import {packEnclose} from "../../src/index.js";

// https://github.com/d3/d3-hierarchy/issues/188
it("packEnclose(circles) handles a tricky case", () => {
assert.deepStrictEqual(
packEnclose([
{x: 14.5, y: 48.5, r: 7.585},
{x: 9.5, y: 79.5, r: 2.585},
{x: 15.5, y: 73.5, r: 8.585}
]),
{
r: 20.790781637717107,
x: 12.80193548387092,
y: 61.59615384615385
}
);
});

0 comments on commit a071e70

Please sign in to comment.