Skip to content

Commit

Permalink
voronoi.update
Browse files Browse the repository at this point in the history
fixes #74
  • Loading branch information
Fil committed Jul 9, 2019
1 parent 1adf482 commit 175921f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
16 changes: 13 additions & 3 deletions src/voronoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import Polygon from "./polygon.js";
export default class Voronoi {
constructor(delaunay, [xmin, ymin, xmax, ymax] = [0, 0, 960, 500]) {
if (!((xmax = +xmax) >= (xmin = +xmin)) || !((ymax = +ymax) >= (ymin = +ymin))) throw new Error("invalid bounds");
const {points, hull, triangles} = this.delaunay = delaunay;
const circumcenters = this.circumcenters = new Float64Array(triangles.length / 3 * 2);
const vectors = this.vectors = new Float64Array(points.length * 2);
this.delaunay = delaunay;
this._circumcenters = new Float64Array(delaunay.points.length * 2);
this.vectors = new Float64Array(delaunay.points.length * 2);
this.xmax = xmax, this.xmin = xmin;
this.ymax = ymax, this.ymin = ymin;
this._init();
}
update() {
this.delaunay.update();
this._init();
return this;
}
_init() {
const {delaunay: {points, hull, triangles}, vectors} = this;

// Compute circumcenters.
const circumcenters = this.circumcenters = this._circumcenters.subarray(0, triangles.length / 3 * 2);
for (let i = 0, j = 0, n = triangles.length; i < n; i += 3, j += 2) {
const t1 = triangles[i] * 2;
const t2 = triangles[i + 1] * 2;
Expand Down
13 changes: 5 additions & 8 deletions test/voronoi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ tape("voronoi.contains(i, x, y) is false for coincident points", test => {
test.equal(voronoi.contains(1, 1, 0), true);
});

tape("delaunay.update() updates the voronoi", test => {
tape("voronoi.update() updates the voronoi", test => {
let delaunay = Delaunay.from([[0, 0], [300, 0], [0, 300], [300, 300], [100, 100]]);
let voronoi = delaunay.voronoi([-500, -500, 500, 500]);
for (let i = 0; i < delaunay.points.length; i++) {
delaunay.points[i] = 10 - delaunay.points[i];
}
let voronoi = delaunay.voronoi([-500, -500, 500, 500]);
const p1 = voronoi.cellPolygon(1); // incorrect before delaunay.update
delaunay.update();
const p2 = voronoi.cellPolygon(1); // correct after delaunay.update
test.ok(JSON.stringify(p1) != JSON.stringify(p2));
test.deepEqual(p2, [[-500, 500], [-500, -140], [60, -140], [-140, 60], [-140, 500], [-500, 500]]);
});
const p = voronoi.update().cellPolygon(1); // correct after voronoi.update
test.deepEqual(p, [[-500, 500], [-500, -140], [-240, -140], [-140, 60], [-140, 500], [-500, 500]]);
});

0 comments on commit 175921f

Please sign in to comment.