Skip to content

Commit

Permalink
Fixed a problem with big numbers in packEnclose
Browse files Browse the repository at this point in the history
  • Loading branch information
w8r committed Jun 21, 2024
1 parent 563dd3a commit cc8fdf1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/pack/enclose.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ function encloseBasis2(a, b) {
}

function encloseBasis3(a, b, c) {
var x1 = a.x, y1 = a.y, r1 = a.r,
x2 = b.x, y2 = b.y, r2 = b.r,
x3 = c.x, y3 = c.y, r3 = c.r,
var cx = (a.x + b.x + c.x) / 3, cy = (a.y + b.y + c.y) / 3;
var x1 = a.x - cx, y1 = a.y - cy, r1 = a.r,
x2 = b.x - cx, y2 = b.y - cy, r2 = b.r,
x3 = c.x - cx, y3 = c.y - cy, r3 = c.r,
a2 = x1 - x2,
a3 = x1 - x3,
b2 = y1 - y2,
Expand All @@ -116,8 +117,8 @@ function encloseBasis3(a, b, c) {
C = xa * xa + ya * ya - r1 * r1,
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,
x: cx + x1 + xa + xb * r,
y: cy + y1 + ya + yb * r,
r: r
};
}
13 changes: 11 additions & 2 deletions test/pack/enclose-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ it("packEnclose(circles) handles a tricky case", () => {
{x: 15.5, y: 73.5, r: 8.585}
]),
{
r: 20.790781637717107,
x: 12.80193548387092,
r: 20.790781637717117,
x: 12.801935483870967,
y: 61.59615384615385
}
);
});

it('packEnclose(circles) handles big numbers', () => {
const circles = [
{ "x": 531214.7271532917, "y": 360738.8204573899, "r": 10 },
{ "x": 531242.0429781883, "y": 360764.87727581244, "r": 10 },
{ "x": 531239.7927335791, "y": 360716.54336245544, "r": 10 }
];
assert.doesNotThrow(() => packEnclose(circles));
});

0 comments on commit cc8fdf1

Please sign in to comment.