Skip to content

Commit

Permalink
avoid spurious points in the corners (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Sep 4, 2019
1 parent 056387f commit 452c31f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/voronoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ export default class Voronoi {
P.splice(j, 0, x, y), j += 2;
}
}
if (P.length > 4) {
for (let i = 0; i < P.length; i+= 2) {
const j = (i + 2) % P.length, k = (i + 4) % P.length;
if (P[i] === P[j] && P[j] === P[k]
|| P[i + 1] === P[j + 1] && P[j + 1] === P[k + 1])
P.splice(j, 2), i -= 2;
}
}
return j;
}
_project(x0, y0, vx, vy) {
Expand Down
11 changes: 11 additions & 0 deletions test/voronoi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ tape("zero-length edges are removed", test => {
test.deepEqual(voronoi2.cellPolygon(0), [[15, 20], [0, 20], [0, 0], [15, 0], [15, 20]]);
});

tape("unnecessary points on the corners are avoided (#88)", test => {
for (const [points, lengths] of [
[ [[289,25],[3,22],[93,165],[282,184],[65,89]], [ 6, 4, 6, 5, 6 ] ],
[ [[189,13],[197,26],[47,133],[125,77],[288,15]], [ 4, 6, 5, 6, 5 ] ],
[ [[44,42],[210,193],[113,103],[185,43],[184,37]], [ 5, 5, 7, 5, 6 ]]
]) {
const voronoi = Delaunay.from(points).voronoi([0, 0, 290, 190]);
test.deepEqual([...voronoi.cellPolygons()].map(d => d.length), lengths);
}
});

tape("a degenerate triangle is avoided", test => {
const pts = [[424.75, 253.75],[424.75, 253.74999999999997],[407.17640687119285, 296.17640687119285],[364.75, 313.75],[322.32359312880715, 296.17640687119285],[304.75, 253.75],[322.32359312880715, 211.32359312880715],[364.75, 193.75],[407.17640687119285, 211.32359312880715],[624.75, 253.75],[607.1764068711929, 296.17640687119285],[564.75, 313.75],[522.3235931288071, 296.17640687119285],[504.75, 253.75],[564.75, 193.75]
]
Expand Down

0 comments on commit 452c31f

Please sign in to comment.