mapbox / delaunator

An incredibly fast JavaScript library for Delaunay triangulation of 2D points
https://mapbox.github.io/delaunator/
ISC License
2.24k stars 139 forks source link

Add an update method for iterative algorithms #48

Closed mourner closed 5 years ago

mourner commented 5 years ago

Closes #36. @JobLeonard how does this look?

I opted for the simplest approach possible that wouldn't complicate the API much. So now the constructor only allocates all necessary arrays (except for hull which is small but of undetermined size before triangulation), while all the work happens in update:

const delaunay = new Delaunator(coords);
// modify coords
delaunay.update();

Also removes any explicit release of the temporary arrays because it doesn't seem too useful. If needed, this could be handled on the app side by GC:

const {hull, triangles, halfedges} = new Delaunator(coords);
// temporary arrays will be garbage-collected because they're no longer references by anything
JobLeonard commented 5 years ago

how does this look?

It looks great! BRB, going to try it out :p

JobLeonard commented 5 years ago

You know what, just reading from the page it's obvious that this works. And manually modifying the original coordinates array that was passed (or this.coords I guess) seems most sensible to me too: iterative changes means interacting with the raw array anyway.

So I'll just wait until this and d3-delaunay merges and update my Voronoi stippling notebook then :)

mourner commented 5 years ago

Cool! Before you can use it, d3-delaunay will probably also need an equivalent change, since currently it doesn't hold onto the delaunay object, extracting the triangulation info and throwing it out.