Name: d3-delaunay
Owner: D3
Description: Compute the Voronoi diagram of a set of two-dimensional points.
Created: 2018-01-11 16:54:16.0
Updated: 2018-03-22 08:42:00.0
Pushed: 2018-03-22 04:52:01.0
Homepage: https://beta.observablehq.com/@mbostock/the-delaunays-dual
Size: 4880
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Georgy ?The Voronator? Voronoy
This is a fast, no-dependency library for computing the Voronoi diagram of a set of two-dimensional points. It is based on Delaunator, a fast library for computing the Delaunay triangulation using sweep algorithms. The Voronoi diagram is constructed by connecting the circumcenters of adjacent triangles in the Delaunay triangulation.
For an interactive explanation of how this library works, see The Delaunay?s Dual.
To install, npm install d3-delaunay
or yarn add d3-delaunay
. You can also download the latest release or load directly from unpkg. AMD, CommonJS, ES5 and ES6+ environments are supported. In vanilla, a d3
global is exported.
rt {Delaunay} from "d3-delaunay";
t points = [[0, 0], [0, 1], [1, 0], [1, 1]];
t delaunay = Delaunay.from(points);
t voronoi = delaunay.voronoi([0, 0, 960, 500]);
Returns the Delaunay triangulation for the given flat array [x0, y0, x1, y1, ?] of points.
t delaunay = new Delaunay(Float64Array.of(0, 0, 0, 1, 1, 0, 1, 1));
# Delaunay.from(points[, fx[, fy[, that]]]) <>
Returns the Delaunay triangulation for the given array or iterable of points. If fx and fy are not specified, then points is assumed to be an array of two-element arrays of numbers: [[x0, y0], [x1, y1], ?]. Otherwise, fx and fy are functions that are invoked for each element in the points array in order, and must return the respective x- and y-coordinate for each point. If that is specified, the functions fx and fy are invoked with that as this. (See Array.from for reference.)
t delaunay = Delaunay.from([[0, 0], [0, 1], [1, 0], [1, 1]]);
# delaunay.points
The coordinates of the points as an array [x0, y0, x1, y1, ?]. Typically, this is a Float64Array, however you can use any array-like type in the constructor.
# delaunay.halfedges
The half-edge indexes as an Int32Array [j0, j1, ?]. For each index 0 ? i < halfedges.length, there is a half-edge from triangle vertex j = halfedges[i] to triangle vertex i. Equivalently, this means that triangle ?i / 3? is adjacent to triangle ?j / 3?. If j is negative, then triangle ?i / 3? is an exterior triangle on the convex hull. For example, to render the internal edges of the Delaunay triangulation:
t {points, halfedges, triangles} = delaunay;
(let i = 0, n = halfedges.length; i < n; ++i) {
nst j = halfedges[i];
(j < i) continue;
nst ti = triangles[i] * 2;
nst tj = triangles[j] * 2;
ntext.moveTo(points[ti], points[ti + 1]);
ntext.lineTo(points[tj], points[tj + 1]);
See also delaunay.render.
# delaunay.hull
The convex hull as an a Uint32Array [i0, i1, ?] of triangle vertex indexes. For example, to render the exterior edges of the Delaunay triangulation:
t {hull, triangles} = delaunay;
t n = hull.length;
i0, i1 = triangles[hull[n - 1]] * 2;
(let i = 0; i < n; ++i) {
= i1, i1 = triangles[hull[i]] * 2;
ntext.moveTo(points[i0], points[i0 + 1]);
ntext.lineTo(points[i1], points[i1 + 1]);
See also delaunay.renderHull.
# delaunay.triangles
The triangle vertex indexes as an Int32Array [i0, j0, k0, i1, j1, k1, ?]. Each contiguous triplet of indexes i, j, k forms a counterclockwise triangle. The coordinates of the triangle?s points can be found by going through delaunay.points. For example, to render triangle i:
t {points, triangles} = delaunay;
t t0 = triangles[i * 3 + 0] * 2;
t t1 = triangles[i * 3 + 1] * 2;
t t2 = triangles[i * 3 + 2] * 2;
ext.moveTo(points[t0], points[t0 + 1]);
ext.lineTo(points[t1], points[t1 + 1]);
ext.lineTo(points[t2], points[t2 + 1]);
ext.closePath();
See also delaunay.renderTriangle.
Renders the edges of the Delaunay triangulation to the specified context. The specified context must implement the context.moveTo and context.lineTo methods from the CanvasPathMethods API.
# delaunay.renderHull(context) <>
Renders the convex hull of the Delaunay triangulation to the specified context. The specified context must implement the context.moveTo and context.lineTo methods from the CanvasPathMethods API.
# delaunay.renderTriangle(context) <>
Renders triangle i of the Delaunay triangulation to the specified context. The specified context must implement the context.moveTo, context.lineTo and context.closePath methods from the CanvasPathMethods API.
# delaunay.renderPoints(context[, radius]) <>
Renders the input points of the Delaunay triangulation to the specified context as circles with the specified radius. If radius is not specified, it defaults to 2. The specified context must implement the context.moveTo and context.arc methods from the CanvasPathMethods API.
# delaunay.voronoi([bounds]) <>
Returns the Voronoi diagram for the associated points. When rendering, the diagram will be clipped to the specified bounds = [xmin, ymin, xmax, ymax]. If bounds is not specified, it defaults to [0, 0, 960, 500]. See To Infinity and Back Again for an interactive explanation of Voronoi cell clipping.
# voronoi.delaunay
The Voronoi diagram?s associated Delaunay triangulation.
# voronoi.circumcenters
The circumcenters of the Delaunay triangles as a Float64Array [cx0, cy0, cx1, cy1, ?]. Each contiguous pair of coordinates cx, cy is the circumcenter for the corresponding triangle. These circumcenters form the coordinates of the Voronoi cell polygons.
# voronoi.edges
? TODO The triangle indexes [i0, i1, ?] in counterclockwise order. Together with the start and end vectors cell.v0 and cell.vn if any, the circumcenters of these triangles form the exterior polygon of the cell. For coincident points, only the cell associated with the first input point is non-null.
# voronoi.index
? TODO The triangle indexes [i0, i1, ?] in counterclockwise order. Together with the start and end vectors cell.v0 and cell.vn if any, the circumcenters of these triangles form the exterior polygon of the cell. For coincident points, only the cell associated with the first input point is non-null.
# voronoi.vectors
? TODO The start vector [vx0, vy0], if the cell?s associated point is on the convex hull of the Delaunay triangulation. Together with the cell?s triangle circumcenters and end vector cell.vn if any, the start vector forms the exterior polygon of the cell. The end vector [vxn, vyn], if the cell?s associated point is on the convex hull of the Delaunay triangulation. Together with the cell?s triangle circumcenters and start vector cell.v0 if any, the end vector forms the exterior polygon of the cell.
# voronoi.xmin
# voronoi.ymin
# voronoi.xmax
# voronoi.ymax
The bounds of the viewport [xmin, ymin, xmax, ymax] for rendering the Voronoi diagram. These values only affect the rendering methods (voronoi.render, voronoi.renderBounds, cell.render).
Returns the index of the cell that contains the specified point ?x, y?. The search is started at the specified point i. If i is not specified, it defaults to zero. (This method is not affected by the associated Voronoi diagram?s viewport bounds.)
# voronoi.contains(i, x, y) <>
Returns true if the cell with the specified index i contains the specified point ?x, y?. (This method is not affected by the associated Voronoi diagram?s viewport bounds.)
Renders the mesh of Voronoi cells to the specified context. The specified context must implement the context.moveTo and context.lineTo methods from the CanvasPathMethods API.
# voronoi.renderBounds(context) <>
Renders the viewport extent to the specified context. The specified context must implement the context.rect method from the CanvasPathMethods API. Equivalent to context.rect(voronoi.xmin, voronoi.ymin, voronoi.xmax - voronoi.xmin, voronoi.ymax - voronoi.ymin).
# voronoi.renderCell(i, context) <>
Renders the cell with the specified index i to the specified context. The specified context must implement the context.moveTo , context.lineTo and context.closePath methods from the CanvasPathMethods API.