Skip to content

Commit 15e46fa

Browse files
Filmbostock
andauthored
robust intercept (#139)
Co-authored-by: Mike Bostock <[email protected]>
1 parent 9caef5c commit 15e46fa

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/voronoi.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,16 @@ export default class Voronoi {
152152
return this.delaunay._step(i, x, y) === i;
153153
}
154154
*neighbors(i) {
155-
const epsilon = 1e-12;
156155
const ci = this._clip(i);
157156
if (ci) for (const j of this.delaunay.neighbors(i)) {
158157
const cj = this._clip(j);
159158
// find the common edge
160159
if (cj) loop: for (let ai = 0, li = ci.length; ai < li; ai += 2) {
161160
for (let aj = 0, lj = cj.length; aj < lj; aj += 2) {
162-
if (Math.abs(ci[ai] - cj[aj]) < epsilon
163-
&& Math.abs(ci[ai + 1] - cj[aj + 1]) < epsilon
164-
&& Math.abs(ci[(ai + 2) % li] - cj[(aj + lj - 2) % lj]) < epsilon
165-
&& Math.abs(ci[(ai + 3) % li] - cj[(aj + lj - 1) % lj]) < epsilon) {
161+
if (ci[ai] === cj[aj]
162+
&& ci[ai + 1] === cj[aj + 1]
163+
&& ci[(ai + 2) % li] === cj[(aj + lj - 2) % lj]
164+
&& ci[(ai + 3) % li] === cj[(aj + lj - 1) % lj]) {
166165
yield j;
167166
break loop;
168167
}
@@ -239,8 +238,11 @@ export default class Voronoi {
239238
return P;
240239
}
241240
_clipSegment(x0, y0, x1, y1, c0, c1) {
241+
// for more robustness, always consider the segment in the same order
242+
const flip = c0 < c1;
243+
if (flip) [x0, y0, x1, y1, c0, c1] = [x1, y1, x0, y0, c1, c0];
242244
while (true) {
243-
if (c0 === 0 && c1 === 0) return [x0, y0, x1, y1];
245+
if (c0 === 0 && c1 === 0) return flip ? [x1, y1, x0, y0] : [x0, y0, x1, y1];
244246
if (c0 & c1) return null;
245247
let x, y, c = c0 || c1;
246248
if (c & 0b1000) x = x0 + (x1 - x0) * (this.ymax - y0) / (y1 - y0), y = this.ymax;

test/voronoi-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,21 @@ it("voronoi.neighbors returns the correct neighbors", () => {
8888
const voronoi = Delaunay.from(points).voronoi([0, 0, 100, 90]);
8989
assert.deepStrictEqual([0, 1, 2, 3].map(i => [...voronoi.neighbors(i)].sort()), [[1], [0, 2, 3], [1, 3], [1, 2]]);
9090
});
91+
92+
it("voronoi.neighbors returns the correct neighbors, flipped vertically", () => {
93+
const points = [[10, -10], [36, -27], [90, -19], [50, -75]];
94+
const voronoi = Delaunay.from(points).voronoi([0, -90, 100, 0]);
95+
assert.deepStrictEqual([0, 1, 2, 3].map(i => [...voronoi.neighbors(i)].sort()), [[1], [0, 2, 3], [1, 3], [1, 2]]);
96+
});
97+
98+
it("voronoi.neighbors returns the correct neighbors, flipped horizontally", () => {
99+
const points = [[-10, 10], [-36, 27], [-90, 19], [-50, 75]];
100+
const voronoi = Delaunay.from(points).voronoi([-100, 0, 0, 90]);
101+
assert.deepStrictEqual([0, 1, 2, 3].map(i => [...voronoi.neighbors(i)].sort()), [[1], [0, 2, 3], [1, 3], [1, 2]]);
102+
});
103+
104+
it("voronoi.neighbors returns the correct neighbors, rotated", () => {
105+
const points = [[-10, -10], [-36, -27], [-90, -19], [-50, -75]];
106+
const voronoi = Delaunay.from(points).voronoi([-100, -90, 0, 0]);
107+
assert.deepStrictEqual([0, 1, 2, 3].map(i => [...voronoi.neighbors(i)].sort()), [[1], [0, 2, 3], [1, 3], [1, 2]]);
108+
});

0 commit comments

Comments
 (0)