plotboilerplate 1.29.0 → 1.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +26 -13
  2. package/dist/index.esm.js +310 -57
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.esm.min.js +1 -1
  5. package/dist/index.esm.min.js.map +1 -1
  6. package/dist/plotboilerplate.browser.min.js +1 -1
  7. package/dist/plotboilerplate.browser.min.js.map +1 -1
  8. package/dist/plotboilerplate.js +266 -36
  9. package/dist/plotboilerplate.min.js +1 -1
  10. package/package.json +1 -1
  11. package/src/cjs/Circle.d.ts +17 -1
  12. package/src/cjs/Circle.js +21 -1
  13. package/src/cjs/Circle.js.map +1 -1
  14. package/src/cjs/CubicBezierCurve.d.ts +34 -3
  15. package/src/cjs/CubicBezierCurve.js +79 -12
  16. package/src/cjs/CubicBezierCurve.js.map +1 -1
  17. package/src/cjs/Line.d.ts +46 -1
  18. package/src/cjs/Line.js +80 -1
  19. package/src/cjs/Line.js.map +1 -1
  20. package/src/cjs/Polygon.d.ts +18 -4
  21. package/src/cjs/Polygon.js +56 -18
  22. package/src/cjs/Polygon.js.map +1 -1
  23. package/src/cjs/VertTuple.d.ts +24 -2
  24. package/src/cjs/VertTuple.js +28 -2
  25. package/src/cjs/VertTuple.js.map +1 -1
  26. package/src/cjs/Vertex.d.ts +2 -2
  27. package/src/cjs/Vertex.js +2 -2
  28. package/src/cjs/utils/algorithms/delaunay2voronoi.d.ts +2 -1
  29. package/src/cjs/utils/algorithms/delaunay2voronoi.js +8 -10
  30. package/src/cjs/utils/algorithms/delaunay2voronoi.js.map +1 -1
  31. package/src/cjs/utils/datastructures/PointSet.d.ts +63 -0
  32. package/src/cjs/utils/datastructures/PointSet.js +126 -0
  33. package/src/cjs/utils/datastructures/PointSet.js.map +1 -0
  34. package/src/cjs/utils/datastructures/VoronoiCell.d.ts +19 -2
  35. package/src/cjs/utils/datastructures/VoronoiCell.js +39 -12
  36. package/src/cjs/utils/datastructures/VoronoiCell.js.map +1 -1
  37. package/src/cjs/utils/renderers/VoronoiRenderer.d.ts +99 -0
  38. package/src/cjs/utils/renderers/VoronoiRenderer.js +147 -0
  39. package/src/cjs/utils/renderers/VoronoiRenderer.js.map +1 -0
  40. package/src/esm/Circle.d.ts +17 -1
  41. package/src/esm/Circle.js +21 -1
  42. package/src/esm/Circle.js.map +1 -1
  43. package/src/esm/CubicBezierCurve.d.ts +34 -3
  44. package/src/esm/CubicBezierCurve.js +79 -12
  45. package/src/esm/CubicBezierCurve.js.map +1 -1
  46. package/src/esm/Line.d.ts +46 -1
  47. package/src/esm/Line.js +80 -1
  48. package/src/esm/Line.js.map +1 -1
  49. package/src/esm/Polygon.d.ts +18 -4
  50. package/src/esm/Polygon.js +56 -18
  51. package/src/esm/Polygon.js.map +1 -1
  52. package/src/esm/VertTuple.d.ts +24 -2
  53. package/src/esm/VertTuple.js +28 -2
  54. package/src/esm/VertTuple.js.map +1 -1
  55. package/src/esm/Vertex.d.ts +2 -2
  56. package/src/esm/Vertex.js +2 -2
  57. package/src/esm/utils/algorithms/delaunay2voronoi.d.ts +2 -1
  58. package/src/esm/utils/algorithms/delaunay2voronoi.js +8 -10
  59. package/src/esm/utils/algorithms/delaunay2voronoi.js.map +1 -1
  60. package/src/esm/utils/datastructures/PointSet.d.ts +63 -0
  61. package/src/esm/utils/datastructures/PointSet.js +115 -0
  62. package/src/esm/utils/datastructures/PointSet.js.map +1 -0
  63. package/src/esm/utils/datastructures/VoronoiCell.d.ts +19 -2
  64. package/src/esm/utils/datastructures/VoronoiCell.js +39 -12
  65. package/src/esm/utils/datastructures/VoronoiCell.js.map +1 -1
  66. package/src/esm/utils/renderers/VoronoiRenderer.d.ts +99 -0
  67. package/src/esm/utils/renderers/VoronoiRenderer.js +142 -0
  68. package/src/esm/utils/renderers/VoronoiRenderer.js.map +1 -0
  69. package/src/index-gl.js +1 -2
  70. package/src/ts/Circle.ts +23 -1
  71. package/src/ts/CubicBezierCurve.ts +133 -48
  72. package/src/ts/Line.ts +84 -1
  73. package/src/ts/Polygon.ts +58 -18
  74. package/src/ts/VertTuple.ts +30 -2
  75. package/src/ts/Vertex.ts +2 -2
  76. package/src/ts/utils/_AppContext.ts_ +22 -0
  77. package/src/ts/utils/algorithms/delaunay2voronoi.ts +81 -88
  78. package/src/ts/utils/datastructures/PointSet.ts +135 -0
  79. package/src/ts/utils/datastructures/VoronoiCell.ts +198 -169
  80. package/src/ts/utils/renderers/VoronoiRenderer.ts +200 -0
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @classdesc A simple voronoi cell (part of a voronoi diagram), stored as an array of
2
+ * @classdesc A simple voronoi cell (part of a voronoi diagram), stored as an array of
3
3
  * adjacent triangles.
4
4
  *
5
5
  * @requires Line
@@ -10,13 +10,14 @@
10
10
  * @author Ikaros Kappler
11
11
  * @date 2018-04-11
12
12
  * @modified 2018-05-04 Added the 'sharedVertex' param to the constructor. Extended open cells into 'infinity'.
13
- * @modified 2019-10-25 Fixed a serious bug in the toPathArray function; cell with only one vertex (extreme cases) returned invalid arrays which broke the rendering.
13
+ * @modified 2019-10-25 Fixed a serious bug in the toPathArray function; cell with only one vertex (extreme cases) returned invalid arrays which broke the rendering.
14
14
  * @modified 2019-12-09 Removed an unnecesary if-condition from the _calculateOpenEdgePoint(...) helper function.
15
15
  * @modified 2020-05-18 Added function VoronoiCell.toPolygon().
16
16
  * @modified 2020-08-12 Ported this class from vanilla JS to TypeScript.
17
17
  * @modified 2020-08-17 Added some missing type declarations.
18
18
  * @modified 2021-01-20 Members `triangles` and `sharedVertex` are now public.
19
- * @version 1.1.3
19
+ * @modified 2026-09-15 Added the `VoronoiCell.sharedVertexIndex` attribute.
20
+ * @version 1.2.0
20
21
  *
21
22
  * @file VoronoiCell
22
23
  * @public
@@ -28,174 +29,202 @@ import { Triangle } from "../../Triangle";
28
29
  import { Vertex } from "../../Vertex";
29
30
 
30
31
  export class VoronoiCell {
32
+ /**
33
+ * @member {Array<Triangle>} triangles
34
+ * @memberof VoronoiCell
35
+ * @type {Array<Triangle>}
36
+ * @instance
37
+ */
38
+ triangles: Array<Triangle>;
31
39
 
32
- /**
33
- * @member {Array<Triangle>} triangles
34
- * @memberof VoronoiCell
35
- * @type {Array<Triangle>}
36
- * @instance
37
- */
38
- triangles: Array<Triangle>;
39
-
40
- /**
41
- * @member {Vertex} sharedVertex
42
- * @memberof VoronoiCell
43
- * @type {Vertex}
44
- * @instance
45
- */
46
- sharedVertex: Vertex;
47
-
48
- /**
49
- * The constructor.
50
- *
51
- * @constructor
52
- * @name VoronoiCell
53
- * @param {Triangle[]} triangles The passed triangle array must contain an ordered sequence of
54
- * adjacent triangles.
55
- * @param {Vertex} sharedVertex This is the 'center' of the voronoi cell; all triangles must share
56
- * that vertex.
57
- **/
58
- constructor( triangles:Array<Triangle>, sharedVertex:Vertex ) {
59
- if( typeof triangles === 'undefined' )
60
- triangles = [];
61
- if( typeof sharedVertex === 'undefined' )
62
- sharedVertex = new Vertex(0,0);
63
- this.triangles = triangles;
64
- this.sharedVertex = sharedVertex;
65
- };
66
-
67
-
68
- /**
69
- * Check if the first and the last triangle in the path are NOT connected.
70
- *
71
- * @method isOpen
72
- * @instance
73
- * @memberof VoronoiCell
74
- * @return {boolean}
75
- **/
76
- isOpen() : boolean {
77
- // There must be at least three triangles
78
- return this.triangles.length < 3 || !this.triangles[0].isAdjacent(this.triangles[this.triangles.length-1]);
79
- };
80
-
81
-
82
- /**
83
- * Convert this Voronoi cell to a path polygon, consisting of all Voronoi cell corner points.
84
- *
85
- * Note that open Voronoi cell cannot properly converted to Polygons as they are considered
86
- * infinite. 'Open' Voronoi edges will be cut off at their innermose vertices.
87
- *
88
- * @method toPolygon
89
- * @instance
90
- * @memberof VoronoiCell
91
- * @return {Polygon}
92
- **/
93
- toPolygon() : Polygon {
94
- return new Polygon( this.toPathArray(), this.isOpen() );
95
- };
96
-
97
- /**
98
- * Convert the voronoi cell path data to an SVG polygon data string.
99
- *
100
- * "x0,y0 x1,y1 x2,y2 ..."
101
- *
102
- * @method toPathSVGString
103
- * @instance
104
- * @memberof VoronoiCell
105
- * @return {string}
106
- **/
107
- toPathSVGString() : string {
108
- if( this.triangles.length == 0 )
109
- return "";
110
- const arr: Array<Vertex>= this.toPathArray();
111
- return arr.map( (vert:Vertex) => { return ''+vert.x+','+vert.y; } ).join(' ');
112
- };
113
-
114
-
115
- /**
116
- * Convert the voronoi cell path data to an array.
117
- *
118
- * [vertex0, vertex1, vertex2, ... ]
119
- *
120
- * @method toPathArray
121
- * @instance
122
- * @memberof VoronoiCell
123
- * @return {Vertex[]}
124
- **/
125
- toPathArray() : Array<Vertex> {
126
- if( this.triangles.length == 0 )
127
- return [];
128
- if( this.triangles.length == 1 )
129
- return [ this.triangles[0].getCircumcircle().center ];
130
- const arr : Array<Vertex> = [];
131
-
132
- // Working for path begin
133
- if( false && this.isOpen() )
134
- arr.push( VoronoiCell._calcOpenEdgePoint( this.triangles[0], this.triangles[1], this.sharedVertex ) );
135
-
136
- for( var t = 0; t < this.triangles.length; t++ ) {
137
- var cc = this.triangles[t].getCircumcircle();
138
- arr.push( cc.center );
139
- }
140
-
141
- // Urgh, this is not working right now.
142
- /* if( false && this.isOpen() )
40
+ /**
41
+ * @member {Vertex} sharedVertex
42
+ * @memberof VoronoiCell
43
+ * @type {Vertex}
44
+ * @instance
45
+ */
46
+ sharedVertex: Vertex;
47
+
48
+ /**
49
+ *
50
+ */
51
+ sharedVertexIndex: number;
52
+
53
+ /**
54
+ * The constructor.
55
+ *
56
+ * @constructor
57
+ * @name VoronoiCell
58
+ * @param {Triangle[]} triangles The passed triangle array must contain an ordered sequence of
59
+ * adjacent triangles.
60
+ * @param {Vertex} sharedVertex This is the 'center' of the voronoi cell; all triangles must share
61
+ * that vertex.
62
+ **/
63
+ constructor(triangles: Array<Triangle>, sharedVertex: Vertex, sharedVertexIndex: number) {
64
+ if (typeof triangles === "undefined") triangles = [];
65
+ if (typeof sharedVertex === "undefined") sharedVertex = new Vertex(0, 0);
66
+ this.triangles = triangles;
67
+ this.sharedVertex = sharedVertex;
68
+ this.sharedVertexIndex = sharedVertexIndex;
69
+ }
70
+
71
+ /**
72
+ * Check if the first and the last triangle in the path are NOT connected.
73
+ *
74
+ * @method isOpen
75
+ * @instance
76
+ * @memberof VoronoiCell
77
+ * @return {boolean}
78
+ **/
79
+ isOpen(): boolean {
80
+ // There must be at least three triangles
81
+ return this.triangles.length < 3 || !this.triangles[0].isAdjacent(this.triangles[this.triangles.length - 1]);
82
+ }
83
+
84
+ /**
85
+ * Convert this Voronoi cell to a path polygon, consisting of all Voronoi cell corner points.
86
+ *
87
+ * Note that open Voronoi cell cannot properly converted to Polygons as they are considered
88
+ * infinite. 'Open' Voronoi edges will be cut off at their innermose vertices.
89
+ *
90
+ * @method toPolygon
91
+ * @instance
92
+ * @memberof VoronoiCell
93
+ * @return {Polygon}
94
+ **/
95
+ toPolygon(): Polygon {
96
+ return new Polygon(this.toPathArray(), this.isOpen());
97
+ }
98
+
99
+ /**
100
+ * Get all 'umbrella' triangles for this Voronoi cell.
101
+ *
102
+ * The 'umbrella' is that sequence of triangles that covers the whole Voronoi cell and
103
+ * meet in the cell's centroid.
104
+ *
105
+ * @method getUmbrellaTriangles
106
+ * @instance
107
+ * @memberof VoronoiCell
108
+ * @return {Array<Triangle>}
109
+ **/
110
+ getUmbrellaTriangles(): Array<Triangle> {
111
+ var tris: Array<Triangle> = [];
112
+ // for (var i = 0; i < this.triangles.length; i++) {
113
+ // var delaunayTri = this.triangles[i];
114
+ // var tri = new Triangle(this.sharedVertex, delaunayTri.b, delaunayTri.c);
115
+ // tris.push(tri);
116
+ // }
117
+ var vertices = this.toPathArray();
118
+ var n = vertices.length;
119
+ for (var i = 0; i < n; i++) {
120
+ var tri = new Triangle(this.sharedVertex, vertices[i], vertices[(i + 1) % n]);
121
+ tris.push(tri);
122
+ }
123
+
124
+ return tris;
125
+ }
126
+
127
+ /**
128
+ * Convert the voronoi cell path data to an SVG polygon data string.
129
+ *
130
+ * "x0,y0 x1,y1 x2,y2 ..."
131
+ *
132
+ * @method toPathSVGString
133
+ * @instance
134
+ * @memberof VoronoiCell
135
+ * @return {string}
136
+ **/
137
+ toPathSVGString(): string {
138
+ if (this.triangles.length == 0) return "";
139
+ const arr: Array<Vertex> = this.toPathArray();
140
+ return arr
141
+ .map((vert: Vertex) => {
142
+ return "" + vert.x + "," + vert.y;
143
+ })
144
+ .join(" ");
145
+ }
146
+
147
+ /**
148
+ * Convert the voronoi cell path data to an array.
149
+ *
150
+ * [vertex0, vertex1, vertex2, ... ]
151
+ *
152
+ * @method toPathArray
153
+ * @instance
154
+ * @memberof VoronoiCell
155
+ * @return {Vertex[]}
156
+ **/
157
+ toPathArray(): Array<Vertex> {
158
+ if (this.triangles.length == 0) return [];
159
+ if (this.triangles.length == 1) return [this.triangles[0].getCircumcircle().center];
160
+ const arr: Array<Vertex> = [];
161
+
162
+ // Working for path begin
163
+ if (false && this.isOpen()) arr.push(VoronoiCell._calcOpenEdgePoint(this.triangles[0], this.triangles[1], this.sharedVertex));
164
+
165
+ for (var t = 0; t < this.triangles.length; t++) {
166
+ var cc = this.triangles[t].getCircumcircle();
167
+ arr.push(cc.center);
168
+ }
169
+
170
+ // Urgh, this is not working right now.
171
+ /* if( false && this.isOpen() )
143
172
  arr.push( VoronoiCell._calcOpenEdgePoint( this.triangles[ this.triangles.length-1 ], this.triangles[ this.triangles.length-2 ], this.sharedVertex ) );
144
173
  */
145
-
146
- return arr;
147
- }
148
174
 
149
-
150
-
151
- /**
152
- * A helper function.
153
- *
154
- * Calculate the 'infinite' open edge point based on the open path triangle
155
- * 'tri' and its neighbour 'neigh'.
156
- *
157
- * This function is used to determine outer hull points.
158
- *
159
- * @method _calcOpenEdhePoint
160
- * @private
161
- * @static
162
- * @instance
163
- * @memberof VoronoiCell
164
- * @return {Vertex}
165
- **/
166
- private static _calcOpenEdgePoint( tri:Triangle, neigh:Triangle, sharedVertex:Vertex ) : Vertex {
167
- const center : Vertex = tri.getCircumcircle().center;
168
- // Find non-adjacent edge (=outer edge)
169
- const edgePoint : Vertex = VoronoiCell._findOuterEdgePoint( tri, neigh, sharedVertex );
170
- // const perpendicular : Vertex = VoronoiCell._perpendicularLinePoint( sharedVertex, edgePoint, center );
171
- const perpendicular : Vertex = new Line( sharedVertex, edgePoint ).getClosestPoint( center );
172
- // It is not necesary to make a difference on the determinant here
173
- const openEdgePoint : Vertex = new Vertex( perpendicular.x + (center.x-perpendicular.x)*1000,
174
- perpendicular.y + (center.y-perpendicular.y)*1000 );
175
- return openEdgePoint;
176
- };
177
-
178
- /**
179
- * A helper function.
180
- *
181
- * Find the outer (not adjacent) vertex in triangle 'tri' which has triangle 'neighbour'.
182
- *
183
- * This function is used to determine outer hull points.
184
- *
185
- * @return {Vertex}
186
- **/
187
- private static _findOuterEdgePoint( tri: Triangle, neighbour: Triangle, sharedVertex: Vertex ) : Vertex {
188
- if( tri.a.equals(sharedVertex) ) {
189
- if( neighbour.a.equals(tri.b) || neighbour.b.equals(tri.b) || neighbour.c.equals(tri.b) ) return tri.c;
190
- else return tri.b;
191
- }
192
- if( tri.b.equals(sharedVertex) ) {
193
- if( neighbour.a.equals(tri.a) || neighbour.b.equals(tri.a) || neighbour.c.equals(tri.a) ) return tri.c;
194
- else return tri.a;
195
- }
196
- // Here:
197
- // tri.c.equals(sharedVertex)
198
- if( neighbour.a.equals(tri.a) || neighbour.b.equals(tri.a) || neighbour.c.equals(tri.a) ) return tri.b;
199
- else return tri.a;
200
- };
175
+ return arr;
176
+ }
177
+
178
+ /**
179
+ * A helper function.
180
+ *
181
+ * Calculate the 'infinite' open edge point based on the open path triangle
182
+ * 'tri' and its neighbour 'neigh'.
183
+ *
184
+ * This function is used to determine outer hull points.
185
+ *
186
+ * @method _calcOpenEdhePoint
187
+ * @private
188
+ * @static
189
+ * @instance
190
+ * @memberof VoronoiCell
191
+ * @return {Vertex}
192
+ **/
193
+ private static _calcOpenEdgePoint(tri: Triangle, neigh: Triangle, sharedVertex: Vertex): Vertex {
194
+ const center: Vertex = tri.getCircumcircle().center;
195
+ // Find non-adjacent edge (=outer edge)
196
+ const edgePoint: Vertex = VoronoiCell._findOuterEdgePoint(tri, neigh, sharedVertex);
197
+ // const perpendicular : Vertex = VoronoiCell._perpendicularLinePoint( sharedVertex, edgePoint, center );
198
+ const perpendicular: Vertex = new Line(sharedVertex, edgePoint).getClosestPoint(center);
199
+ // It is not necesary to make a difference on the determinant here
200
+ const openEdgePoint: Vertex = new Vertex(
201
+ perpendicular.x + (center.x - perpendicular.x) * 1000,
202
+ perpendicular.y + (center.y - perpendicular.y) * 1000
203
+ );
204
+ return openEdgePoint;
205
+ }
206
+
207
+ /**
208
+ * A helper function.
209
+ *
210
+ * Find the outer (not adjacent) vertex in triangle 'tri' which has triangle 'neighbour'.
211
+ *
212
+ * This function is used to determine outer hull points.
213
+ *
214
+ * @return {Vertex}
215
+ **/
216
+ private static _findOuterEdgePoint(tri: Triangle, neighbour: Triangle, sharedVertex: Vertex): Vertex {
217
+ if (tri.a.equals(sharedVertex)) {
218
+ if (neighbour.a.equals(tri.b) || neighbour.b.equals(tri.b) || neighbour.c.equals(tri.b)) return tri.c;
219
+ else return tri.b;
220
+ }
221
+ if (tri.b.equals(sharedVertex)) {
222
+ if (neighbour.a.equals(tri.a) || neighbour.b.equals(tri.a) || neighbour.c.equals(tri.a)) return tri.c;
223
+ else return tri.a;
224
+ }
225
+ // Here:
226
+ // tri.c.equals(sharedVertex)
227
+ if (neighbour.a.equals(tri.a) || neighbour.b.equals(tri.a) || neighbour.c.equals(tri.a)) return tri.b;
228
+ else return tri.a;
229
+ }
201
230
  }
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Refactored all Voronoi helper functions for drawing the diagram (demo 07) into this class.
3
+ *
4
+ * @author Ikaros Kappler
5
+ * @date 2026-09-13
6
+ * @modified 2026-09-15 Ported to Typescript.
7
+ * @version 1.0.1
8
+ */
9
+
10
+ import { Bounds } from "../../Bounds";
11
+ import { Polygon } from "../../Polygon";
12
+ import { Triangle } from "../../Triangle";
13
+ import { DrawLib } from "../../interfaces";
14
+ import { convexPolygonIncircle } from "../algorithms/convexPolygonIncircle";
15
+ import { sutherlandHodgman } from "../algorithms/sutherlandHodgman";
16
+ import { cloneVertexArray } from "../cloneVertexArray";
17
+ import { PointSet } from "../datastructures/PointSet";
18
+ import { VoronoiCell } from "../datastructures/VoronoiCell";
19
+
20
+ export interface IVoronoiConfig {
21
+ makeVoronoiDiagram: boolean;
22
+ drawPoints: boolean;
23
+ drawTriangles: boolean;
24
+ drawCircumCircles: boolean;
25
+ drawCubicCurves: boolean;
26
+ fillVoronoiCells: boolean;
27
+ voronoiOutlineColor: string;
28
+ voronoiCellColor: string;
29
+ voronoiCubicThreshold: number;
30
+ voronoiCellScale: number;
31
+ clipVoronoiCells: boolean;
32
+ drawClipBox: boolean;
33
+ drawUnclippedVoronoiCells: boolean;
34
+ drawVoronoiIncircles: boolean;
35
+ drawVoronoiOutlines: boolean;
36
+ pointCount: number;
37
+ horizontalSafeArea: number;
38
+ verticalSafeArea: number;
39
+ voronoiCellLineWidth: number;
40
+ }
41
+
42
+ export interface IVoronoiContext {
43
+ pointSet: PointSet;
44
+ triangles: Array<Triangle>;
45
+ voronoiCells: Array<VoronoiCell>;
46
+ // cells: Array<VoronoiCell>;
47
+ config: IVoronoiConfig;
48
+ }
49
+
50
+ export class VoronoiRenderer {
51
+ voronoiContext: IVoronoiContext;
52
+
53
+ constructor(voronoiContext: IVoronoiContext) {
54
+ this.voronoiContext = voronoiContext;
55
+ }
56
+
57
+ /**
58
+ * Draw the given triangle with the specified (CSS-) color.
59
+ *
60
+ * @name draw
61
+ * @instance
62
+ * @memberof VoronoiRenderer
63
+ * @param {DrawLib} draw
64
+ * @param {Triangle} t
65
+ * @param {string} color
66
+ * @return {void}
67
+ */
68
+ draw(draw: DrawLib<any>, fill: DrawLib<any>) {
69
+ // Draw circumcircles
70
+ if (this.voronoiContext.config.drawCircumCircles) {
71
+ VoronoiRenderer.drawCircumCircles(draw, this.voronoiContext.triangles);
72
+ }
73
+
74
+ // An array of VoronoiCells.
75
+ var clipBoxPolygon = Bounds.computeFromVertices(this.voronoiContext.pointSet.points).toPolygon();
76
+ if (this.voronoiContext.config.drawClipBox) {
77
+ draw.polygon(clipBoxPolygon, "rgba(192,192,192,0.25)");
78
+ }
79
+
80
+ for (var v in this.voronoiContext.voronoiCells) {
81
+ var cell = this.voronoiContext.voronoiCells[v];
82
+ this._drawCell(draw, fill, clipBoxPolygon, cell);
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Draw the given triangle with the specified (CSS-) color.
88
+ *
89
+ * @name _drawCell
90
+ * @instance
91
+ * @private
92
+ * @memberof VoronoiRenderer
93
+ * @param {DrawLib} draw
94
+ * @param {Polygon} clipBoxPolygon
95
+ * @param {VoronoiCell} cell
96
+ * @return {void}
97
+ */
98
+ private _drawCell(draw: DrawLib<any>, fill: DrawLib<any>, clipBoxPolygon: Polygon, cell: VoronoiCell) {
99
+ var cellPolygon = cell.toPolygon();
100
+ cellPolygon.scale(this.voronoiContext.config.voronoiCellScale, cell.sharedVertex);
101
+
102
+ // Draw large (unclipped) Voronoi cell
103
+ if (
104
+ this.voronoiContext.config.drawVoronoiOutlines &&
105
+ (!this.voronoiContext.config.clipVoronoiCells || this.voronoiContext.config.drawUnclippedVoronoiCells)
106
+ ) {
107
+ draw.polyline(
108
+ cellPolygon.vertices,
109
+ false,
110
+ this.voronoiContext.config.clipVoronoiCells ? "rgba(128,128,128,0.333)" : this.voronoiContext.config.voronoiOutlineColor,
111
+ this.voronoiContext.config.voronoiCellLineWidth ?? 2.0
112
+ );
113
+ }
114
+
115
+ // Apply clipping?
116
+ if (this.voronoiContext.config.clipVoronoiCells) {
117
+ // Clone the array here: convert Array<XYCoords> to Array<Vertex>
118
+ // polygon = new Polygon(cloneVertexArray(sutherlandHodgman(polygon.vertices, clipBoxPolygon.vertices)), false);
119
+ cellPolygon = VoronoiRenderer.clipVoronoiPolygon(cellPolygon, clipBoxPolygon);
120
+ }
121
+
122
+ if (this.voronoiContext.config.drawVoronoiOutlines && this.voronoiContext.config.clipVoronoiCells) {
123
+ draw.polygon(
124
+ cellPolygon,
125
+ this.voronoiContext.config.voronoiOutlineColor,
126
+ this.voronoiContext.config.voronoiCellLineWidth ?? 2.0
127
+ );
128
+ }
129
+
130
+ if ((!cell.isOpen() || this.voronoiContext.config.clipVoronoiCells) && cell.triangles.length >= 3) {
131
+ if (this.voronoiContext.config.drawCubicCurves) {
132
+ var cbezier = cellPolygon.toCubicBezierData(this.voronoiContext.config.voronoiCubicThreshold);
133
+ if (this.voronoiContext.config.fillVoronoiCells) {
134
+ fill.cubicBezierPath(cbezier, this.voronoiContext.config.voronoiCellColor);
135
+ } else {
136
+ draw.cubicBezierPath(cbezier, this.voronoiContext.config.voronoiCellColor);
137
+ }
138
+ }
139
+ if (this.voronoiContext.config.drawVoronoiIncircles) {
140
+ var result = convexPolygonIncircle(cellPolygon);
141
+ var circle = result.circle;
142
+ // var triangle = result.triangle;
143
+ // Here we should have found the best inlying circle (and the corresponding triangle)
144
+ // inside the Voronoi cell.
145
+ draw.circle(circle.center, circle.radius, "rgba(255,192,0,1.0)", 2);
146
+ }
147
+ } // END cell is not open
148
+ }
149
+
150
+ /**
151
+ * A function for drawing the triangles.
152
+ *
153
+ * @static
154
+ */
155
+ drawTriangles(draw: DrawLib<any>) {
156
+ for (var i in this.voronoiContext.triangles) {
157
+ var t = this.voronoiContext.triangles[i];
158
+ VoronoiRenderer.drawTriangle(draw, t, this.voronoiContext.config.makeVoronoiDiagram ? "rgba(0,128,224,0.33)" : "#0088d8");
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Draw the given triangle with the specified (CSS-) color.
164
+ *
165
+ * @static
166
+ * @name drawTriangle
167
+ * @memberof VoronoiRenderer
168
+ * @param {DrawLib} draw
169
+ * @param {Triangle} tri
170
+ * @param {string} color
171
+ * @return {void}
172
+ */
173
+ static drawTriangle(draw: DrawLib<any>, tri: Triangle, color: string) {
174
+ // draw.line(t.a, t.b, color);
175
+ // draw.line(t.b, t.c, color);
176
+ // draw.line(t.c, t.a, color);
177
+ draw.polyline([tri.a, tri.b, tri.c], false, color);
178
+ }
179
+
180
+ /**
181
+ * Draw the circumcircles of all triangles.
182
+ */
183
+ static drawCircumCircles(draw: DrawLib<any>, triangles: Array<Triangle>) {
184
+ for (var t in triangles) {
185
+ var cc = triangles[t].getCircumcircle();
186
+ draw.circle(cc.center, cc.radius, "#e86800");
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Clip the convex (!) cell polygon by the convex (!) clipping polygon.
192
+ *
193
+ * @param cellPolygon
194
+ * @param clipBoxPolygon
195
+ * @returns
196
+ */
197
+ static clipVoronoiPolygon(cellPolygon: Polygon, clipBoxPolygon: Polygon) {
198
+ return new Polygon(cloneVertexArray(sutherlandHodgman(cellPolygon.vertices, clipBoxPolygon.vertices)), false);
199
+ }
200
+ }