Found delaunator docs, very helpful in writing function documentation.

This commit is contained in:
Gabriel 2021-02-02 18:28:51 -05:00
parent a0119cc8bd
commit 63e13bcadb

View file

@ -1,7 +1,8 @@
class Voronoi { class Voronoi {
/** /**
* Creates a Voronoi diagram from the given Delaunator, a list of points, and the number of points. The Voronoi diagram is constructed using (I think) the {@link https://en.wikipedia.org/wiki/Bowyer%E2%80%93Watson_algorithm |Bowyer-Watson Algorithm} * Creates a Voronoi diagram from the given Delaunator, a list of points, and the number of points. The Voronoi diagram is constructed using (I think) the {@link https://en.wikipedia.org/wiki/Bowyer%E2%80%93Watson_algorithm |Bowyer-Watson Algorithm}
* @param {{triangles: Uint32Array, halfedges: Int32Array}} delaunay A {@link https://github.com/mapbox/delaunator/blob/master/index.js |Delaunator} instance * The {@link https://github.com/mapbox/delaunator/ |Delaunator} library uses {@link https://en.wikipedia.org/wiki/Doubly_connected_edge_list |half-edges} to represent the relationship between points and triangles.
* @param {{triangles: Uint32Array, halfedges: Int32Array}} delaunay A {@link https://github.com/mapbox/delaunator/blob/master/index.js |Delaunator} instance.
* @param {[number, number][]} points A list of coordinates. * @param {[number, number][]} points A list of coordinates.
* @param {number} pointsN The number of points. * @param {number} pointsN The number of points.
*/ */
@ -12,6 +13,10 @@ class Voronoi {
this.cells = { v: [], c: [], b: [] }; // voronoi cells: v = cell vertices, c = adjacent cells, b = near-border cell this.cells = { v: [], c: [], b: [] }; // voronoi cells: v = cell vertices, c = adjacent cells, b = near-border cell
this.vertices = { p: [], v: [], c: [] }; // cells vertices: p = vertex coordinates, v = neighboring vertices, c = adjacent cells this.vertices = { p: [], v: [], c: [] }; // cells vertices: p = vertex coordinates, v = neighboring vertices, c = adjacent cells
// Half-edges are the indices into the delaunator outputs:
// delaunay.triangles[e] gives the point ID where the half-edge starts
// delaunay.triangles[e] returns either the opposite half-edge in the adjacent triangle, or -1 if there's not an adjacent triangle.
for (let e = 0; e < this.delaunay.triangles.length; e++) { for (let e = 0; e < this.delaunay.triangles.length; e++) {
const p = this.delaunay.triangles[this.nextHalfedge(e)]; const p = this.delaunay.triangles[this.nextHalfedge(e)];
@ -32,18 +37,18 @@ class Voronoi {
} }
/** /**
* * Gets the IDs of the points comprising the given triangle. Taken from {@link https://mapbox.github.io/delaunator/#triangle-to-points| the Delaunator docs.}
* @param {number} t The index of the triangle * @param {number} t The index of the triangle
* @returns {[number, number, number]} * @returns {[number, number, number]} The IDs of the points comprising the given triangle.
*/ */
pointsOfTriangle(t) { pointsOfTriangle(t) {
return this.edgesOfTriangle(t).map(edge => this.delaunay.triangles[edge]); return this.edgesOfTriangle(t).map(edge => this.delaunay.triangles[edge]);
} }
/** /**
* Identifies what triangles are adjacent to the given triangle * Identifies what triangles are adjacent to the given triangle. Taken from {@link https://mapbox.github.io/delaunator/#triangle-to-triangles| the Delaunator docs.}
* @param {number} t The index of the triangle * @param {number} t The index of the triangle
* @returns {number[]} * @returns {number[]} The indices of the triangles that share half-edges with this triangle.
*/ */
trianglesAdjacentToTriangle(t) { trianglesAdjacentToTriangle(t) {
let triangles = []; let triangles = [];
@ -55,9 +60,9 @@ class Voronoi {
} }
/** /**
* * Gets the indices of all the incoming and outgoing half-edges that touch the given point. Taken from {@link https://mapbox.github.io/delaunator/#point-to-edges| the Delaunator docs.}
* @param {number} start * @param {number} start The index of an incoming half-edge that leads to the desired point
* @returns {number[]} * @returns {number[]} The indices of all half-edges (incoming or outgoing) that touch the point.
*/ */
edgesAroundPoint(start) { edgesAroundPoint(start) {
const result = []; const result = [];
@ -73,7 +78,7 @@ class Voronoi {
/** /**
* Returns the center of the triangle located at the given index. * Returns the center of the triangle located at the given index.
* @param {number} t The index of the triangle * @param {number} t The index of the triangle
* @returns {number} * @returns {[number, number]}
*/ */
triangleCenter(t) { triangleCenter(t) {
let vertices = this.pointsOfTriangle(t).map(p => this.points[p]); let vertices = this.pointsOfTriangle(t).map(p => this.points[p]);
@ -81,46 +86,51 @@ class Voronoi {
} }
/** /**
* Gets all of the edges of a triangle starting at index t * Retrieves all of the half-edges for a specific triangle `t`. Taken from {@link https://mapbox.github.io/delaunator/#edge-and-triangle| the Delaunator docs.}
* @param {number} t The index of the triangle * @param {number} t The index of the triangle
* @returns {[number, number, number]} The edges of the triangle * @returns {[number, number, number]} The edges of the triangle.
*/ */
edgesOfTriangle(t) { return [3 * t, 3 * t + 1, 3 * t + 2]; } edgesOfTriangle(t) { return [3 * t, 3 * t + 1, 3 * t + 2]; }
/** /**
* Identifies the triangle that corresponds to the given edge index * Enables lookup of a triangle, given one of the half-edges of that triangle. Taken from {@link https://mapbox.github.io/delaunator/#edge-and-triangle| the Delaunator docs.}
* @param {number} e The index of the edge * @param {number} e The index of the edge
* @returns {number} The index of the triangle * @returns {number} The index of the triangle
*/ */
triangleOfEdge(e) { return Math.floor(e / 3); } triangleOfEdge(e) { return Math.floor(e / 3); }
/** /**
* Determines the index of the next half edge of the current triangle in the this.delaunay.triangles array * Moves to the next half-edge of a triangle, given the current half-edge's index. Taken from {@link https://mapbox.github.io/delaunator/#edge-to-edges| the Delaunator docs.}
* @param {number} e The index of the current half edge * @param {number} e The index of the current half edge
* @returns {number} The index of the next half edge * @returns {number} The index of the next half edge
*/ */
nextHalfedge(e) { return (e % 3 === 2) ? e - 2 : e + 1; } nextHalfedge(e) { return (e % 3 === 2) ? e - 2 : e + 1; }
/** /**
* Determines the index of the previous half edge of the current triangle in the this.delaunay.triangles array * Moves to the previous half-edge of a triangle, given the current half-edge's index. Taken from {@link https://mapbox.github.io/delaunator/#edge-to-edges| the Delaunator docs.}
* @param {number} e The index of the current half edge * @param {number} e The index of the current half edge
* @returns {number} The index of the previous half edge * @returns {number} The index of the previous half edge
*/ */
prevHalfedge(e) { return (e % 3 === 0) ? e + 2 : e - 1; } prevHalfedge(e) { return (e % 3 === 0) ? e + 2 : e - 1; }
/** /**
* Finds the circumcenter of the triangle identified by points a, b, and c. * Finds the circumcenter of the triangle identified by points a, b, and c. Taken from {@link https://en.wikipedia.org/wiki/Circumscribed_circle#Circumcenter_coordinates| Wikipedia}
* @param {[number, number]} a * @param {[number, number]} a The coordinates of the first point of the triangle
* @param {[number, number]} b * @param {[number, number]} b The coordinates of the second point of the triangle
* @param {[number, number]} c * @param {[number, number]} c The coordinates of the third point of the triangle
* @return {[number, number]} The coordinates of the circumcenter of the triangle. * @return {[number, number]} The coordinates of the circumcenter of the triangle.
*/ */
circumcenter(a, b, c) { circumcenter(a, b, c) {
let ad = a[0] * a[0] + a[1] * a[1]; const [ax, ay] = a;
let bd = b[0] * b[0] + b[1] * b[1]; const [bx, by] = b;
let cd = c[0] * c[0] + c[1] * c[1]; const [cx, cy] = c;
let D = 2 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1])); const ad = ax * ax + ay * ay;
const bd = bx * bx + by * by;
const cd = cx * cx + cy * cy;
const D = 2 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
return [ return [
Math.floor(1 / D * (ad * (b[1] - c[1]) + bd * (c[1] - a[1]) + cd * (a[1] - b[1]))), Math.floor(1 / D * (ad * (by - cy) + bd * (cy - ay) + cd * (ay - by))),
Math.floor(1 / D * (ad * (c[0] - b[0]) + bd * (a[0] - c[0]) + cd * (b[0] - a[0]))) Math.floor(1 / D * (ad * (cx - bx) + bd * (ax - cx) + cd * (bx - ax)))
]; ];
} }
} }