UNPKG

11.4 kBTypeScriptView Raw
1// Type definitions for D3JS d3-voronoi module 1.1
2// Project: https://github.com/d3/d3-voronoi/, https://d3js.org/d3-voronoi
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// denisname <https://github.com/denisname>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8// TypeScript Version: 2.3
9
10// Last module patch version validated against: 1.1.4
11
12// --------------------------------------------------------------------------
13// Shared Type Definitions and Interfaces
14// --------------------------------------------------------------------------
15
16/**
17 * The VoronoiPoint interface is defined as a cue that the array is strictly of type [number, number] with two elements
18 * for x and y coordinates. However, it is used as a base for interface definitions, and [number, number]
19 * cannot be extended.
20 */
21export interface VoronoiPoint extends Array<number> {
22 0: number;
23 1: number;
24}
25
26/**
27 * The VoronoiPointPair interface is defined as a cue that the array is strictly of type [[number, number], [number, number]] with two elements, one
28 * for each point containing the respective x and y coordinates. However, it is used as a base for interface definitions, and
29 * [[number, number], [number, number]] cannot be extended.
30 */
31export interface VoronoiPointPair extends Array<[number, number]> {
32 0: [number, number];
33 1: [number, number];
34}
35
36/**
37 * A Voronoi Polygon is represented as an array of points [x, y] where x and y are the point coordinates, and a data field that refers to the corresponding element in data.
38 * Polygons are open: they do not contain a closing point that duplicates the first point; a triangle, for example, is an array of three points.
39 * Polygons are also counterclockwise, assuming the origin ⟨0,0⟩ is in the top-left corner.
40 *
41 * The generic refers to the type of the data for the corresponding element.
42 */
43export interface VoronoiPolygon<T> extends Array<[number, number]> {
44 /**
45 * The input data corresponding to this Voronoi polygon.
46 */
47 data: T;
48}
49
50/**
51 * Voronoi Triangle is a three-element array of elements from data.
52 *
53 * The generic refers to the type of the data for the corresponding element.
54 */
55export type VoronoiTriangle<T> = [T, T, T];
56
57/**
58 * A Voronoi Site in the diagram is an array [x, y] with two additional properties:
59 * index and data.
60 *
61 * The generic refers to the type of the data for the corresponding element.
62 */
63export interface VoronoiSite<T> extends VoronoiPoint {
64 /**
65 * The Voronoi Site’s index, corresponding to the associated input point.
66 */
67 index: number;
68
69 /**
70 * The input data corresponding to this site.
71 */
72 data: T;
73}
74
75/**
76 * A Voronoi Cell in the diagram is an object with the following properties:
77 * site and halfedges
78 *
79 * The generic refers to the type of the data for the corresponding element.
80 */
81export interface VoronoiCell<T> {
82 /**
83 * The Voronoi Site of the cell’s associated input point.
84 */
85 site: VoronoiSite<T>;
86
87 /**
88 * An array of indexes into diagram.edges representing the cell’s polygon.
89 */
90 halfedges: number[];
91}
92
93/**
94 * Voronoi Edge in the diagram is an array [[x0, y0], [x1, y1]] with two additional properties:
95 * left and right.
96 *
97 * The generic refers to the type of the data for the corresponding element.
98 */
99export interface VoronoiEdge<T> extends VoronoiPointPair {
100 /**
101 * The Voronoi site on the left side of the edge.
102 */
103 left: VoronoiSite<T>;
104
105 /**
106 * The Voronoi site on the right side of the edge; `null` for a clipped border edge.
107 */
108 right: VoronoiSite<T> | null;
109}
110
111/**
112 * Voronoi Link for an edge in the mesh created by the Delaunay triangulation of the specified data array.
113 * Each link has the following attributes: source and target.
114 *
115 * The generic refers to the type of the data for the corresponding element.
116 */
117export interface VoronoiLink<T> {
118 /**
119 * The source node, an element in data.
120 */
121 source: T;
122
123 /**
124 * The target node, an element in data.
125 */
126 target: T;
127}
128
129/**
130 * A Voronoi Layout.
131 *
132 * The generic refers to the type of the data for the corresponding element.
133 */
134export interface VoronoiLayout<T> {
135 /**
136 * Computes the Voronoi diagram for the specified data points.
137 * @param data Array of data elements
138 */
139 (data: T[]): VoronoiDiagram<T>;
140
141 /**
142 * Return the current x-coordinate accessor,
143 * which defaults to accessing the first element of an array (i.e. at index 0).
144 */
145 x(): (d: T) => number;
146 /**
147 * Set the x-coordinate accessor and return the layout.
148 *
149 * @param x An accessor function which takes a data element as input and return a
150 * numeric value for the x-coordinate.
151 */
152 x(x: (d: T) => number): this;
153
154 /**
155 * Return the current y-coordinate accessor,
156 * which defaults to accessing the second element of an array (i.e. at index 1).
157 */
158 y(): (d: T) => number;
159 /**
160 * Set the y-coordinate accessor and return the layout.
161 *
162 * @param y An accessor function which takes a data element as input and return a
163 * numeric value for the y-coordinate.
164 */
165 y(y: (d: T) => number): this;
166
167 /**
168 * Returns the current clip extent which defaults to null.
169 *
170 * The extent bounds are specified as an array [[x0, y0], [x1, y1]],
171 * where x0 is the left side of the extent, y0 is the top,
172 * x1 is the right and y1 is the bottom.
173 *
174 * A clip extent is required when using voronoi.polygons.
175 *
176 */
177 extent(): [[number, number], [number, number]] | null;
178 /**
179 * Set the clip extent of the Voronoi layout to the specified bounds and return the layout.
180 *
181 * A clip extent is required when using voronoi.polygons.
182 *
183 * @param extent The extent bounds are specified as an array [[x0, y0], [x1, y1]],
184 * where x0 is the left side of the extent, y0 is the top, x1 is the right and y1 is the bottom.
185 */
186 extent(extent: [[number, number], [number, number]]): this;
187
188 /**
189 * Get the clip size of the Voronoi layout. Size is an alias for voronoi.extent
190 * where the minimum x and y of the extent are ⟨0,0⟩.
191 */
192 size(): [number, number] | null;
193 /**
194 * Set the clip size and return the layout.
195 *
196 * Size is an alias for voronoi.extent where the minimum x and y of the extent are ⟨0,0⟩.
197 *
198 * @param size An array representing the x- and y-size of the clip extent,
199 * where the minimum x and y of the extent are ⟨0,0⟩.
200 */
201 size(size: [number, number]): this;
202
203 /**
204 * Return an array of polygons clipped to the extent, one for each input point in the specified data points,
205 * corresponding to the cells in the computed Voronoi diagram.
206 *
207 * Each polygon is represented as an array of points [x, y] where x and y are the point coordinates,
208 * and a data field that refers to the corresponding element in data.
209 * Polygons are open: they do not contain a closing point that duplicates the first point;
210 * a triangle, for example, is an array of three points. Polygons are also counterclockwise,
211 * assuming the origin ⟨0,0⟩ is in the top-left corner.
212 *
213 * If the cell’s site is coincident with an earlier site, the associated polygon is null.
214 *
215 * Important: Using polygon requires the extent to be set for the layout.
216 *
217 * @param data Array of data points.
218 */
219 polygons(data: T[]): Array<VoronoiPolygon<T>>;
220
221 /**
222 * Return the Delaunay triangulation of the specified data array as an array of triangles.
223 * Each triangle is a three-element array of elements from data.
224 *
225 * @param data Array of data points.
226 */
227 triangles(data: T[]): Array<VoronoiTriangle<T>>;
228
229 /**
230 * Return the Delaunay triangulation of the specified data array as an array of links.
231 * Each link has source and target attributes referring to elements in data.
232 *
233 * @param data Array of data points.
234 */
235 links(data: T[]): Array<VoronoiLink<T>>;
236}
237
238/**
239 * Computed Voronoi diagram
240 *
241 * The generic refers to the type of the data for the corresponding element.
242 */
243export interface VoronoiDiagram<T> {
244 /**
245 * Array of Voronoi Edges
246 */
247 edges: Array<VoronoiEdge<T>>;
248
249 /**
250 * Array of Voronoi Cells, one per input point; a cell may be null for a coincident point.
251 */
252 cells: Array<VoronoiCell<T> | null>;
253
254 /**
255 * Return an array of polygons clipped to the extent, one for each cell in the diagram.
256 * Each polygon is represented as an array of points [x, y] where x and y are the point coordinates,
257 * and a data field that refers to the corresponding element in data.
258 * Polygons are open: they do not contain a closing point that duplicates the first point;
259 * a triangle, for example, is an array of three points. Polygons are also counterclockwise,
260 * assuming the origin ⟨0,0⟩ is in the top-left corner.
261 *
262 * If the cell’s site is coincident with an earlier site, the associated polygon is null.
263 */
264 polygons(): Array<VoronoiPolygon<T>>;
265
266 /**
267 * Returns the Delaunay triangulation of the specified data array as an array of triangles.
268 * Each triangle is a three-element array of elements from data.
269 * Since the triangulation is computed as the dual of the Voronoi diagram, and the Voronoi diagram is clipped by the extent,
270 * a subset of the Delaunay triangulation is returned.
271 */
272 triangles(): Array<VoronoiTriangle<T>>;
273
274 /**
275 * Returns the Delaunay triangulation of the specified data array as an array of links, one for each edge in the mesh.
276 * Each link has the following attributes:
277 * - source (the source node, an element in data)
278 * - target (the target node, an element in data)
279 *
280 * Since the triangulation is computed as the dual of the Voronoi diagram, and the Voronoi diagram is clipped by the extent, a subset of the Delaunay links is returned.
281 */
282 links(): Array<VoronoiLink<T>>;
283
284 /**
285 * Return the nearest Voronoi Site to point [x, y]. If radius is specified, only sites within radius distance are considered.
286 * If no Voronoi Site can be found (within the specified radius), null is returned.
287 *
288 * @param x x-coordinate
289 * @param y y-coordinate
290 * @param radius Optional parameter for search radius around [x, y]
291 */
292 find(x: number, y: number, radius?: number): VoronoiSite<T> | null;
293}
294
295// --------------------------------------------------------------------------
296// voronoi Export
297// --------------------------------------------------------------------------
298
299/**
300 * Creates a new Voronoi layout with default x- and y- accessors and a null extent.
301 * x- and y-accessors may have to be set to correspond to the data type provided by the
302 * generic.
303 *
304 * The generic refers to the type of the data for the corresponding element.
305 * Without specifying a generic the layout is assumed to be based on data represented
306 * by a two-dimensional coordinate `[number, number]` for x- and y-coordinate, respectively.
307 */
308export function voronoi<T = [number, number]>(): VoronoiLayout<T>;