Skip to content

Commit cc8fdf1

Browse files
committed
Fixed a problem with big numbers in packEnclose
1 parent 563dd3a commit cc8fdf1

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/pack/enclose.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ function encloseBasis2(a, b) {
9494
}
9595

9696
function encloseBasis3(a, b, c) {
97-
var x1 = a.x, y1 = a.y, r1 = a.r,
98-
x2 = b.x, y2 = b.y, r2 = b.r,
99-
x3 = c.x, y3 = c.y, r3 = c.r,
97+
var cx = (a.x + b.x + c.x) / 3, cy = (a.y + b.y + c.y) / 3;
98+
var x1 = a.x - cx, y1 = a.y - cy, r1 = a.r,
99+
x2 = b.x - cx, y2 = b.y - cy, r2 = b.r,
100+
x3 = c.x - cx, y3 = c.y - cy, r3 = c.r,
100101
a2 = x1 - x2,
101102
a3 = x1 - x3,
102103
b2 = y1 - y2,
@@ -116,8 +117,8 @@ function encloseBasis3(a, b, c) {
116117
C = xa * xa + ya * ya - r1 * r1,
117118
r = -(Math.abs(A) > 1e-6 ? (B + Math.sqrt(B * B - 4 * A * C)) / (2 * A) : C / B);
118119
return {
119-
x: x1 + xa + xb * r,
120-
y: y1 + ya + yb * r,
120+
x: cx + x1 + xa + xb * r,
121+
y: cy + y1 + ya + yb * r,
121122
r: r
122123
};
123124
}

test/pack/enclose-test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ it("packEnclose(circles) handles a tricky case", () => {
1010
{x: 15.5, y: 73.5, r: 8.585}
1111
]),
1212
{
13-
r: 20.790781637717107,
14-
x: 12.80193548387092,
13+
r: 20.790781637717117,
14+
x: 12.801935483870967,
1515
y: 61.59615384615385
1616
}
1717
);
1818
});
19+
20+
it('packEnclose(circles) handles big numbers', () => {
21+
const circles = [
22+
{ "x": 531214.7271532917, "y": 360738.8204573899, "r": 10 },
23+
{ "x": 531242.0429781883, "y": 360764.87727581244, "r": 10 },
24+
{ "x": 531239.7927335791, "y": 360716.54336245544, "r": 10 }
25+
];
26+
assert.doesNotThrow(() => packEnclose(circles));
27+
});

0 commit comments

Comments
 (0)