Skip to content

Commit

Permalink
a test and a (very feeble) fix for collinear values
Browse files Browse the repository at this point in the history
closes #146
  • Loading branch information
Fil committed Jul 12, 2024
1 parent dc894a1 commit 3f71678
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/delaunay.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Delaunay {
.sort((i, j) => points[2 * i] - points[2 * j] || points[2 * i + 1] - points[2 * j + 1]); // for exact neighbors
const e = this.collinear[0], f = this.collinear[this.collinear.length - 1],
bounds = [ points[2 * e], points[2 * e + 1], points[2 * f], points[2 * f + 1] ],
r = 1e-8 * Math.hypot(bounds[3] - bounds[1], bounds[2] - bounds[0]);
r = 1.5e-8 * Math.hypot(bounds[3] - bounds[1], bounds[2] - bounds[0]);
for (let i = 0, n = points.length / 2; i < n; ++i) {
const p = jitter(points[2 * i], points[2 * i + 1], r);
points[2 * i] = p[0];
Expand Down
6 changes: 6 additions & 0 deletions test/voronoi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ it("voronoi returns the expected result (#141)", () => {
const voronoi = Delaunay.from(points).voronoi([1, 1, 499, 199]);
assert.deepEqual(Array.from(voronoi.cellPolygons(), (d) => d.length), [7, 5, 5, 5, 6, 5, 5, 5]);
});

it("voronoi returns the expected result (#146)", () => {
const points = [[25,20], [75,20], [125,20], [175,20], [225,20]];
const voronoi = Delaunay.from(points).voronoi([0, 0, 250, 40]);
assert.deepStrictEqual(Array.from(voronoi.cellPolygons(), (d) => d.map((u) => u.map((d) => Math.round(d)))), [[[0,40],[0,0],[50,0],[50,40],[0,40]],[[100,0],[100,40],[50,40],[50,0],[100,0]],[[150,0],[150,40],[100,40],[100,0],[150,0]],[[200,0],[200,40],[150,40],[150,0],[200,0]],[[200,40],[200,0],[250,0],[250,40],[200,40]]]);
});

0 comments on commit 3f71678

Please sign in to comment.