Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over each voronoi lines #91

Closed
Allakazan opened this issue Oct 2, 2019 · 3 comments
Closed

Iterate over each voronoi lines #91

Allakazan opened this issue Oct 2, 2019 · 3 comments

Comments

@Allakazan
Copy link

I'm making a procedural street pattern with Delaunay, and using voronoi.cellPolygons() to get each polygon. But this give me duplicated lines.

There's a way to iterate over each line separately ? Thanks !

@mbostock
Copy link
Member

mbostock commented Oct 2, 2019

There is no method for iterating over the edges, but you can use voronoi.render to render all of the edges as a mesh, and you can look at the source if you want to access the edges directly yourself.

d3-delaunay/src/voronoi.js

Lines 75 to 101 in b7df1f2

render(context) {
const buffer = context == null ? context = new Path : undefined;
const {delaunay: {halfedges, inedges, hull}, circumcenters, vectors} = this;
if (hull.length <= 1) return null;
for (let i = 0, n = halfedges.length; i < n; ++i) {
const j = halfedges[i];
if (j < i) continue;
const ti = Math.floor(i / 3) * 2;
const tj = Math.floor(j / 3) * 2;
const xi = circumcenters[ti];
const yi = circumcenters[ti + 1];
const xj = circumcenters[tj];
const yj = circumcenters[tj + 1];
this._renderSegment(xi, yi, xj, yj, context);
}
let h0, h1 = hull[hull.length - 1];
for (let i = 0; i < hull.length; ++i) {
h0 = h1, h1 = hull[i];
const t = Math.floor(inedges[h1] / 3) * 2;
const x = circumcenters[t];
const y = circumcenters[t + 1];
const v = h0 * 4;
const p = this._project(x, y, vectors[v + 2], vectors[v + 3]);
if (p) this._renderSegment(x, y, p[0], p[1], context);
}
return buffer && buffer.value();
}

@Fil
Copy link
Member

Fil commented Oct 22, 2019

This notebook might clarify how this can be done:
https://observablehq.com/d/045ae1b3e80973dc

@Fil
Copy link
Member

Fil commented Oct 22, 2019

A much simpler method is to use voronoi.render().split(/M/).slice(1)
https://observablehq.com/d/58f6cc721fa26757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants