Skip to content

Voronoi 图

¥Voronoi diagrams

给定一组点,Voronoi 图将平面划分为多个单元,这些单元表示平面中距离相应点最近的区域。Voronoi 图是 Delaunay 三角剖分 的对偶。

¥Given a set of points, the Voronoi diagram partitions the plane into cells representing the region of the plane that is closest to the corresponding point. The Voronoi diagram is the dual of the Delaunay triangulation.

delaunay.voronoi(bounds) {#delaunay_voronoi}

源代码 · 返回给定 Delaunay 三角剖分 的 Voronoi 图。渲染时,图表将被裁剪到指定的边界 = [xmin, ymin, xmax, ymax]。

¥Source · Returns the Voronoi diagram for the given Delaunay triangulation. When rendering, the diagram will be clipped to the specified bounds = [xmin, ymin, xmax, ymax].

js
const delaunay = d3.Delaunay.from([[0, 0], [0, 100], [100, 0], [100, 100]]);
const voronoi = delaunay.voronoi([0, 0, 640, 480]);

如果未指定 bounds,则默认为 [0, 0, 960, 500]。即使在不存在三角剖分的退化情况下(即 0、1 或 2 个点以及共线点),也会返回 Voronoi 图。

¥If bounds is not specified, it defaults to [0, 0, 960, 500]. The Voronoi diagram is returned even in degenerate cases where no triangulation exists — namely 0, 1 or 2 points, and collinear points.

voronoi.delaunay {#voronoi_delaunay}

Voronoi 图关联的 Delaunay 三角剖分

¥The Voronoi diagram’s associated Delaunay triangulation.

voronoi.circumcenters {#voronoi_circumcenters}

Delaunay 三角形的 circumcenters 是一个 Float64Array [cx0, cy0, cx1, cy1, …]。每对相邻的坐标 cx, cy 都是相应三角形的外心。这些外接圆心构成了 Voronoi 单元多边形的坐标。

¥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.vectors {#voronoi_vectors}

一个 Float64Array [vx0, vy0, wx0, wy0, …],其中每个非零四元组描述外壳上的一个开放(无限)单元,给出两个开放半线的方向。

¥A Float64Array [vx0, vy0, wx0, wy0, …] where each non-zero quadruple describes an open (infinite) cell on the outer hull, giving the directions of two open half-lines.

voronoi.xmin
voronoi.ymin
voronoi.xmax
voronoi.ymax {#voronoi_bounds}

用于渲染 Voronoi 图的视口边界 [xmin, ymin, xmax, ymax]。这些值仅影响渲染方法(voronoi.rendervoronoi.renderBoundsvoronoi.renderCell)。

¥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, voronoi.renderCell).

voronoi.contains(i, x, y) {#voronoi_contains}

源代码 · 如果指定索引 i 的单元格包含指定点 ⟨x, y⟩,则返回 true;即,点 i 是否是图中距离指定点最近的点。(此方法不受关联 Voronoi 图视口 bounds 的影响。)

¥Source · Returns true if the cell with the specified index i contains the specified point ⟨x, y⟩; i.e., whether the point i is the closest point in the diagram to the specified point. (This method is not affected by the associated Voronoi diagram’s viewport bounds.)

voronoi.neighbors(i) {#voronoi_neighbors}

js
voronoi.neighbors(-1) // []

源代码 · 返回与指定单元格 i 共享公共边的单元格索引的可迭代对象。Voronoi 邻居在 Delaunay 图上始终是邻居,但当公共边被 Voronoi 图的视口裁剪掉时,反之亦然。

¥Source · Returns an iterable over the indexes of the cells that share a common edge with the specified cell i. Voronoi neighbors are always neighbors on the Delaunay graph, but the converse is false when the common edge has been clipped out by the Voronoi diagram’s viewport.

voronoi.render(context) {#voronoi_render}

源代码 · 将 Voronoi 单元网格渲染到指定的上下文。指定的上下文必须实现 CanvasPathMethods API 中的 context.moveTo 和 context.lineTo 方法。如果未指定上下文,则返回 SVG 路径字符串。

¥Source · 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. If a context is not specified, an SVG path string is returned instead.

voronoi.renderBounds(context) {#voronoi_renderBounds}

源代码 · 将视口范围渲染到指定的上下文。指定的上下文必须实现 CanvasPathMethods API 中的 context.rect 方法。相当于 context.rect(voronoi.xmin, voronoi.ymin, voronoi.xmax - voronoi.xmin, voronoi.ymax - voronoi.ymin)。如果未指定上下文,则返回 SVG 路径字符串。

¥Source · 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). If a context is not specified, an SVG path string is returned instead.

voronoi.renderCell(i, context) {#voronoi_renderCell}

源代码 · 将指定索引 i 的单元格渲染到指定的上下文。指定的上下文必须实现 CanvasPathMethods API 中的 context.moveTo 、 context.lineTo 和 context.closePath 方法。如果未指定上下文,则返回 SVG 路径字符串。

¥Source · 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. If a context is not specified, an SVG path string is returned instead.

voronoi.cellPolygons() {#voronoi_cellPolygons}

源代码 · 返回一个非空 每个细胞的多边形 上的可迭代对象,其属性为单元格索引。另请参阅 voronoi.renderCell

¥Source · Returns an iterable over the non-empty polygons for each cell, with the cell index as property. See also voronoi.renderCell.

voronoi.cellPolygon(i) {#voronoi_cellPolygon}

源代码 · 返回表示指定点 i 的单元格的凸闭合多边形 [[x0, y0], [x1, y1], …, [x0, y0]]。另请参阅 voronoi.renderCell

¥Source · Returns the convex, closed polygon [[x0, y0], [x1, y1], …, [x0, y0]] representing the cell for the specified point i. See also voronoi.renderCell.

voronoi.update() {#voronoi_update}

源代码 · 在点被修改后更新 Voronoi 图和底层三角剖分 - 这对 Lloyd 松弛很有用。在底层 Delaunay 三角剖分上调用 delaunay.update

¥Source · Updates the Voronoi diagram and underlying triangulation after the points have been modified in-place — useful for Lloyd’s relaxation. Calls delaunay.update on the underlying Delaunay triangulation.