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
@@ -32,8 +32,9 @@
32
32
  * @modified 2025-04-18 Added evaluation method for cubic Bézier curves `CubicBezierCurve.utils.evaluateT`.
33
33
  * @modified 2025-04-18 Refactored method `CubicBezierCurve.getPointAt` to use `evaluateT`.
34
34
  * @modified 2025-04-18 Fixed the `CubicBezierCurve.getBounds` method: now returning the real bounding box. Before it was an approximated one.
35
- * @modified 2025-ß4-18 Added helper methods for bounding box calculation `CubucBezierCurve.util.cubicPolyMinMax` and `cubicPoly`.
36
- * @version 2.9.0
35
+ * @modified 2025-04-18 Added helper methods for bounding box calculation `CubucBezierCurve.util.cubicPolyMinMax` and `cubicPoly`.
36
+ * @modified 2026-09-09 Adding methods `CubicBezierCurve.trimStartEnd` and `CubicBezierCurve.trimStartEndAt`.
37
+ * @version 2.10.0
37
38
  *
38
39
  * @file CubicBezierCurve
39
40
  * @public
@@ -401,30 +402,30 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
401
402
  // Thanks to Richard "RM" for the Bézier bounds calculatin
402
403
  // https://jsfiddle.net/SalixAlba/QQnvm/4/
403
404
 
404
- const xMinMax = CubicBezierCurve.utils.cubicPolyMinMax(
405
- this.startPoint.x,
406
- this.startControlPoint.x,
407
- this.endControlPoint.x,
408
- this.endPoint.x
409
- );
410
- const xl = xMinMax.min;
411
- const xh = xMinMax.max;
412
-
413
- const yMinMax = CubicBezierCurve.utils.cubicPolyMinMax(
414
- this.startPoint.y,
415
- this.startControlPoint.y,
416
- this.endControlPoint.y,
417
- this.endPoint.y
418
- );
419
- const yl = yMinMax.min;
420
- const yh = yMinMax.max;
421
-
422
- return Bounds.computeFromVertices([
423
- { x: xl, y: yl },
424
- { x: xl, y: yh },
425
- { x: xh, y: yh },
426
- { x: xh, y: yl }
427
- ]);
405
+ const xMinMax = CubicBezierCurve.utils.cubicPolyMinMax(
406
+ this.startPoint.x,
407
+ this.startControlPoint.x,
408
+ this.endControlPoint.x,
409
+ this.endPoint.x
410
+ );
411
+ const xl = xMinMax.min;
412
+ const xh = xMinMax.max;
413
+
414
+ const yMinMax = CubicBezierCurve.utils.cubicPolyMinMax(
415
+ this.startPoint.y,
416
+ this.startControlPoint.y,
417
+ this.endControlPoint.y,
418
+ this.endPoint.y
419
+ );
420
+ const yl = yMinMax.min;
421
+ const yh = yMinMax.max;
422
+
423
+ return Bounds.computeFromVertices([
424
+ { x: xl, y: yl },
425
+ { x: xl, y: yh },
426
+ { x: xh, y: yh },
427
+ { x: xh, y: yl }
428
+ ]);
428
429
  }
429
430
  //--- END --- Implement interface `IBounded`
430
431
 
@@ -526,8 +527,20 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
526
527
  // this.startControlPoint.y * 3 * t * Math.pow(1.0 - t, 2) +
527
528
  // this.endControlPoint.y * 3 * Math.pow(t, 2) * (1.0 - t) +
528
529
  // this.endPoint.y * Math.pow(t, 3);
529
- const x: number = CubicBezierCurve.utils.evaluateT(this.startPoint.x, this.startControlPoint.x, this.endControlPoint.x, this.endPoint.x, t);
530
- const y: number = CubicBezierCurve.utils.evaluateT(this.startPoint.y, this.startControlPoint.y, this.endControlPoint.y, this.endPoint.y, t);
530
+ const x: number = CubicBezierCurve.utils.evaluateT(
531
+ this.startPoint.x,
532
+ this.startControlPoint.x,
533
+ this.endControlPoint.x,
534
+ this.endPoint.x,
535
+ t
536
+ );
537
+ const y: number = CubicBezierCurve.utils.evaluateT(
538
+ this.startPoint.y,
539
+ this.startControlPoint.y,
540
+ this.endControlPoint.y,
541
+ this.endPoint.y,
542
+ t
543
+ );
531
544
  return new Vertex(x, y);
532
545
  }
533
546
 
@@ -611,6 +624,7 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
611
624
  this.endControlPoint.set(subCurbePoints[3]);
612
625
  this.updateArcLengths();
613
626
  return this;
627
+ // return this.trimStartEndAt(t, null);
614
628
  }
615
629
 
616
630
  /**
@@ -648,8 +662,88 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
648
662
  this.endControlPoint.set(subCurbePoints[3]);
649
663
  this.updateArcLengths();
650
664
  return this;
665
+ // return this.trimStartEndAt(null, t);
651
666
  }
652
667
 
668
+ /**
669
+ * Trim off a start and end section of this curve. The position parameters `uStart` and `uEnd` are the absolute positions in [0..arcLength].
670
+ * The remaining curve will be the one in the bounds `[uStart,uEnd]` (so `[0.0,uStart]` and `[uEnd,1.0]` are cut off).
671
+ *
672
+ * Parameters out of bounds (< 0.0 or > arcLength) are ignored.
673
+ * If `uEnd` is smaller than `uStart` then a curve with length zero (0) at `uStart` is returned.
674
+ *
675
+ * @method trimStartEndAt
676
+ * @instance
677
+ * @memberof CubicBezierCurve
678
+ * @param {number} tStart - The relative position parameter where to cut off the head curve.
679
+ * @param {number} tEnd - The relative position parameter where to cut off the tail curve.
680
+ * @returns {CubicBezierCurve} `this` for chanining.
681
+ */
682
+
683
+ trimStartEnd(uStart: number, uEnd: number): CubicBezierCurve {
684
+ return this.trimStartEndAt(this.convertU2T(uStart), this.convertU2T(uEnd));
685
+ }
686
+
687
+ /**
688
+ * Trim off a start and end section of this curve. The position parameters `tStart` and `tEnd` are the relative positions in [0..1].
689
+ * The remaining curve will be the one in the bounds `[tStart,tEnd]` (so `[0.0,tStart]` and `[tEnd,1.0]` are cut off).
690
+ *
691
+ * Parameters out of bounds (< 0.0 or > 1.0) are ignored.
692
+ * If `tEnd` is smaller than `tStart` then a curve with length zero (0) at `tStart` is returned.
693
+ *
694
+ * @method trimStartEndAt
695
+ * @instance
696
+ * @memberof CubicBezierCurve
697
+ * @param {number} tStart - The relative position parameter where to cut off the head curve.
698
+ * @param {number} tEnd - The relative position parameter where to cut off the tail curve.
699
+ * @returns {CubicBezierCurve} `this` for chanining.
700
+ */
701
+
702
+ trimStartEndAt(tStart: number, tEnd: number): CubicBezierCurve {
703
+ const cleanTrimStart = Math.min(Math.max(0.0, tStart), 1.0);
704
+ const cleanTrimEnd = Math.min(Math.max(cleanTrimStart, tEnd), 1.0);
705
+ this.trimStartAt(cleanTrimStart);
706
+ const relativeTrimEnd = (cleanTrimEnd - cleanTrimStart) / (1.0 - cleanTrimStart);
707
+ this.trimEndAt(relativeTrimEnd);
708
+ return this;
709
+ }
710
+
711
+ // __trimStartEndAt(tStart: number, tEnd: number): CubicBezierCurve {
712
+ // var finalCurvePoints = [
713
+ // this.startPoint.clone(),
714
+ // this.endPoint.clone(),
715
+ // this.startControlPoint.clone(),
716
+ // this.endControlPoint.clone()
717
+ // ];
718
+ // if (typeof tStart === "number" && !Number.isNaN(tStart)) {
719
+ // const subCurvePointsStart = CubicBezierCurve.utils.getSubCurvePointsAt(this, tStart, 1.0);
720
+ // finalCurvePoints[0].set(subCurvePointsStart[0]);
721
+ // finalCurvePoints[2].set(subCurvePointsStart[2]);
722
+ // // this.startPoint.set(subCurvePointsStart[0]);
723
+ // // this.startControlPoint.set(subCurvePointsStart[2]);
724
+ // // this.endPoint.set(subCurvePointsStart[1]);
725
+ // // this.endControlPoint.set(subCurvePointsStart[3]);
726
+ // }
727
+
728
+ // if (typeof tEnd === "number" && !Number.isNaN(tEnd)) {
729
+ // const subCurvePointsEnd = CubicBezierCurve.utils.getSubCurvePointsAt(this, 0.0, tEnd);
730
+ // // this.startPoint.set(subCurvePointsEnd[0]);
731
+ // // this.startControlPoint.set(subCurvePointsEnd[2]);
732
+ // // this.endPoint.set(subCurvePointsEnd[1]);
733
+ // // this.endControlPoint.set(subCurvePointsEnd[3]);
734
+ // finalCurvePoints[1].set(subCurvePointsEnd[1]);
735
+ // finalCurvePoints[3].set(subCurvePointsEnd[3]);
736
+ // }
737
+
738
+ // this.startPoint.set(finalCurvePoints[0]);
739
+ // this.endPoint.set(finalCurvePoints[1]);
740
+ // this.startControlPoint.set(finalCurvePoints[2]);
741
+ // this.endControlPoint.set(finalCurvePoints[3]);
742
+
743
+ // this.updateArcLengths();
744
+ // return this;
745
+ // }
746
+
653
747
  /**
654
748
  * Get a sub curve at the given start end end positions (values on the curve's length, between 0 and curve.arcLength).
655
749
  *
@@ -1027,15 +1121,13 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
1027
1121
  * Helper utils.
1028
1122
  */
1029
1123
  public static utils = {
1030
-
1031
- evaluateT: (p0:number, p1:number, p2:number, p3:number, t:number) : number => {
1032
- return p0 * Math.pow(1.0 - t, 3) +
1033
- p1 * 3 * t * Math.pow(1.0 - t, 2) +
1034
- p2 * 3 * Math.pow(t, 2) * (1.0 - t) +
1035
- p3 * Math.pow(t, 3);
1124
+ evaluateT: (p0: number, p1: number, p2: number, p3: number, t: number): number => {
1125
+ return (
1126
+ p0 * Math.pow(1.0 - t, 3) + p1 * 3 * t * Math.pow(1.0 - t, 2) + p2 * 3 * Math.pow(t, 2) * (1.0 - t) + p3 * Math.pow(t, 3)
1127
+ );
1036
1128
  },
1037
1129
 
1038
- cubicPolyMinMax: (p0:number, p1:number, p2:number, p3:number) : { min: number, max: number }=> {
1130
+ cubicPolyMinMax: (p0: number, p1: number, p2: number, p3: number): { min: number; max: number } => {
1039
1131
  // var polyX = CubicBezierCurve.utils.cubicPoly2(
1040
1132
  // p0, // P[0].X, // bezierCurve.startPoint.x,
1041
1133
  // p1, // P[1].X, // bezierCurve.startControlPoint.x,
@@ -1048,7 +1140,6 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
1048
1140
  // var c = polyX.c;
1049
1141
  // var disc = polyX.b * polyX.b - 4 * polyX.a * polyX.c;
1050
1142
 
1051
-
1052
1143
  var polyX = CubicBezierCurve.utils.cubicPoly(
1053
1144
  p0, // P[0].X, // bezierCurve.startPoint.x,
1054
1145
  p1, // P[1].X, // bezierCurve.startControlPoint.x,
@@ -1062,7 +1153,6 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
1062
1153
  // var disc = b * b - 4 * a * c;
1063
1154
  var disc = polyX[1] * polyX[1] - 4 * polyX[0] * polyX[2];
1064
1155
 
1065
-
1066
1156
  // var polyX = CubicBezierCurve.utils.bezierCoeffs(p3,p2,p1,p0);
1067
1157
  // var a = polyX[0]; //polyX.a;
1068
1158
  // var b = polyX[1]; // .b;
@@ -1111,7 +1201,7 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
1111
1201
  * @param {number} tEnd – The end offset if the desired cub curve (must be in [0..1]).
1112
1202
  * @instance
1113
1203
  * @memberof CubicBezierCurve
1114
- * @return {CubicBezierCurve} The sub curve as a new curve.
1204
+ * @return {[Vertex, Vertex, Vertex, Vertex]} The sub curve as curve vertices.
1115
1205
  **/
1116
1206
  getSubCurvePointsAt: (curve: CubicBezierCurve, tStart: number, tEnd: number): [Vertex, Vertex, Vertex, Vertex] => {
1117
1207
  const startVec: Vector = new Vector(curve.getPointAt(tStart), curve.getTangentAt(tStart));
@@ -1196,8 +1286,8 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
1196
1286
  * @param {number} p3 - The end point coordinate.
1197
1287
  * @returns {[number,number,number,number]}
1198
1288
  */
1199
- bezierCoeffs: (p0: number, p1: number, p2: number, p3: number): [number,number,number,number] => {
1200
- const coeffs : [number,number,number,number] = [NaN,NaN,NaN,NaN]; //Array(4);
1289
+ bezierCoeffs: (p0: number, p1: number, p2: number, p3: number): [number, number, number, number] => {
1290
+ const coeffs: [number, number, number, number] = [NaN, NaN, NaN, NaN]; //Array(4);
1201
1291
  coeffs[0] = -p0 + 3 * p1 + -3 * p2 + p3;
1202
1292
  coeffs[1] = 3 * p0 - 6 * p1 + 3 * p2;
1203
1293
  coeffs[2] = -3 * p0 + 3 * p1;
@@ -1207,22 +1297,17 @@ export class CubicBezierCurve implements IBounded, Intersectable, PathSegment {
1207
1297
 
1208
1298
  /**
1209
1299
  * Calculate the cubic polynomial coefficients used to find the bounding box.
1210
- *
1300
+ *
1211
1301
  * @param {number} p0 - The start point coordinate.
1212
1302
  * @param {number} p1 - The start control point coordinate.
1213
1303
  * @param {number} p2 - The end control point coordinate.
1214
1304
  * @param {number} p3 - The end point coordinate.
1215
1305
  * @returns {[number,number,number]}
1216
1306
  */
1217
- cubicPoly: (p0:number, p1:number, p2:number, p3:number) : [number,number,number] => {
1218
- return [
1219
- 3 * p3 - 9 * p2 + 9 * p1 - 3 * p0,
1220
- 6 * p0 - 12 * p1 + 6 * p2,
1221
- 3 * p1 - 3 * p0
1222
- ]
1307
+ cubicPoly: (p0: number, p1: number, p2: number, p3: number): [number, number, number] => {
1308
+ return [3 * p3 - 9 * p2 + 9 * p1 - 3 * p0, 6 * p0 - 12 * p1 + 6 * p2, 3 * p1 - 3 * p0];
1223
1309
  },
1224
1310
 
1225
-
1226
1311
  /**
1227
1312
  * sign of number, but is division safe: no zero returned :)
1228
1313
  */
package/src/ts/Line.ts CHANGED
@@ -18,7 +18,8 @@
18
18
  * @modified 2023-09-25 Changed param type of `intersection()` from Line to VertTuple.
19
19
  * @modified 2025-04-15 Class `Line` now implements interface `Intersectable`.
20
20
  * @modified 2025-04-16 Class `Line` now implements interface `IBounded`.
21
- * @version 2.4.0
21
+ * @modified 2026-08-16 Adding methods `Line.trimStart` and `Line.trimEnd`. Adding methods `Line.trimStartAt` and `Line.trimEndAt`.
22
+ * @version 2.5.0
22
23
  *
23
24
  * @file Line
24
25
  * @public
@@ -165,6 +166,88 @@ export class Line extends VertTuple<Line> implements IBounded, Intersectable, Pa
165
166
  }
166
167
  //--- END Implement PathSegment ---
167
168
 
169
+ /**
170
+ * Trim this line segment from the start point by the given amount.
171
+ * The amount must be positive and should be withing the segment's length. If the amount exceeds the segment's length
172
+ * then the length of the resulting line will be zero (0.0).
173
+ *
174
+ * @method trimStart
175
+ * @memberof Line
176
+ * @param {number} amount - The positive amount to trim the line from the start point `a`.
177
+ * @returns {Line} This for chaining, with updated point `a`.
178
+ */
179
+ trimStart(amount: number) {
180
+ // Calculate the relative position `t` on this line.
181
+ var t: number = amount / this.length();
182
+ // `t` should be inside 0..1 – otherwise the amount was too large or negative.
183
+ if (t < 0.0) {
184
+ return this;
185
+ }
186
+ if (t > 1.0) {
187
+ // Set the line to length zero (endpoint only)
188
+ this.a = this.b.clone();
189
+ return this;
190
+ }
191
+ this.a = this.vertAt(t);
192
+ return this;
193
+ }
194
+
195
+ /**
196
+ * Trim this line segment from the start point by the given relative amount.
197
+ * The amount must be positive and should be within 0.0 and 1.0. If the amount exceeds the segment's length
198
+ * then the length of the resulting line will be zero (0.0).
199
+ *
200
+ * @method trimStartAt
201
+ * @memberof Line
202
+ * @param {number} amount - The positive amount to trim the line from the start point `a`.
203
+ * @returns {Line} This for chaining, with updated point `a`.
204
+ */
205
+ trimStartAt(relativeAmount: number) {
206
+ // Calculate the relative position `t` on this line.
207
+ return this.trimStart(relativeAmount * this.length());
208
+ }
209
+
210
+ /**
211
+ * Trim this line segment from the end point by the given amount.
212
+ * The amount must be positive and should be withing the segment's length. If the amount exceeds the segment's length
213
+ * then the length of the resulting line will be zero (0.0).
214
+ *
215
+ * @method trimEnd
216
+ * @memberof Line
217
+ * @param {number} amount - The positive amount to trim the line from the end point `b`.
218
+ * @returns {Line} This for chaining, with updated point `b`.
219
+ */
220
+ trimEnd(amount: number) {
221
+ // Calculate the relative position `t` on this line.
222
+ var t: number = 1.0 - amount / this.length();
223
+ // `t` should be inside 0..1 – otherwise the amount was too large or negative.
224
+ if (t < 0.0) {
225
+ return this;
226
+ }
227
+ if (t > 1.0) {
228
+ // Set the line to length zero (endpoint only)
229
+ this.b = this.a.clone();
230
+ return this;
231
+ }
232
+ this.b = this.vertAt(t);
233
+ return this;
234
+ }
235
+
236
+ /**
237
+ * Trim this line segment from the end point by the given relative amount.
238
+ * The amount must be positive and should be within 0.0 and 1.0. If the amount exceeds the segment's length
239
+ * then the length of the resulting line will be zero (0.0).
240
+ *
241
+ * @method trimEndAt
242
+ * @memberof Line
243
+ * @param {number} amount - The positive amount to trim the line from the start point `a`.
244
+ * @returns {Line} This for chaining, with updated point `a`.
245
+ */
246
+ trimEndAt(relativeAmount: number) {
247
+ // Calculate the relative position `t` on this line.
248
+ return this.trimEnd(relativeAmount * this.length());
249
+ }
250
+
168
251
  //--- BEGIN --- Implement interface `Intersectable`
169
252
  /**
170
253
  * Get all line intersections with this polygon.
package/src/ts/Polygon.ts CHANGED
@@ -38,7 +38,9 @@
38
38
  * @modified 2025-05-16 Class `Polygon` now implements `IBounded`.
39
39
  * @modified 2025-05-20 Tweaking `Polygon.getInnerAngleAt` and `Polygo.isAngleAcute` to handle indices out of array bounds as well.
40
40
  * @modified 2025-06-07 Adding `Polygon.closestLineIntersectionIndex` to determine line intersections plus detected edge index.
41
- * @version 1.16.0
41
+ * @modified 2026-09-16 Adding a `forceClockwise` parameter to the `Polygon.getCentroid()` method.
42
+ * @modified 2026-09-21 Adding `Polygon.revert` method to change the winding order.
43
+ * @version 1.17.0
42
44
  *
43
45
  * @file Polygon
44
46
  * @public
@@ -400,6 +402,21 @@ export class Polygon implements IBounded, Intersectable, SVGSerializable {
400
402
  return Polygon.utils.isClockwise(this.vertices);
401
403
  }
402
404
 
405
+ /**
406
+ * Revert the order of this polygon's vertices to change the winding order.
407
+ * This operation is in-place.
408
+ *
409
+ * @method revert
410
+ * @instance
411
+ * @memberof Polygon
412
+ * @return {Polygon} This for chaining.
413
+ */
414
+ revert(): Polygon {
415
+ // this.vertices.slice().reverse(); // Copy?
416
+ this.vertices.reverse();
417
+ return this;
418
+ }
419
+
403
420
  /**
404
421
  * Get the perimeter of this polygon.
405
422
  * The perimeter is the absolute length of the outline.
@@ -483,35 +500,37 @@ export class Polygon implements IBounded, Intersectable, SVGSerializable {
483
500
 
484
501
  /**
485
502
  * Get centroid.
486
- * Centroids define the barycenter of any non self-intersecting convex polygon.
503
+ * Centroids define the barycenter of any non self-intersecting convex clockwise polygon.
487
504
  *
488
- * If the polygon is self intersecting or non konvex then the barycenter is not well defined.
505
+ * If the polygon is self intersecting or non convex or not clockwise then the barycenter is not well defined.
489
506
  *
490
507
  * https://mathworld.wolfram.com/PolygonCentroid.html
491
508
  *
492
509
  * @method getCentroid
493
510
  * @instance
511
+ * @param {boolean} forceClockwise - [optiona] If set to true then the centroid will be calculated for the clockwise polygon.
494
512
  * @memberof Polygon
495
513
  * @returns {Vertex|null}
496
514
  */
497
- getCentroid(): Vertex | null {
515
+ getCentroid(forceClockwise?: boolean): Vertex | null {
498
516
  if (this.vertices.length === 0) {
499
517
  return null;
500
518
  }
501
- const center: Vertex = new Vertex(0.0, 0.0);
502
- const n = this.vertices.length;
503
- for (var i = 0; i < n; i++) {
504
- // center.add(this.vertices[i]);
505
- const cur: Vertex = this.vertices[i];
506
- const next: Vertex = this.vertices[(i + 1) % n];
507
- var factor: number = cur.x * next.y - next.x * cur.y;
508
- center.x += (cur.x + next.x) * factor;
509
- center.y += (cur.y + next.y) * factor;
510
- }
511
- const area = this.area();
512
- center.x *= 1 / (6 * area);
513
- center.y *= 1 / (6 * area);
514
- return center;
519
+ // const centroid: Vertex = new Vertex(0.0, 0.0);
520
+ // const n = this.vertices.length;
521
+ // for (var i = 0; i < n; i++) {
522
+ // // center.add(this.vertices[i]);
523
+ // const cur: Vertex = this.vertices[i];
524
+ // const next: Vertex = this.vertices[(i + 1) % n];
525
+ // var factor: number = cur.x * next.y - next.x * cur.y;
526
+ // centroid.x += (cur.x + next.x) * factor;
527
+ // centroid.y += (cur.y + next.y) * factor;
528
+ // }
529
+ // const area = this.area();
530
+ // centroid.x *= 1 / (6 * area);
531
+ // centroid.y *= 1 / (6 * area);
532
+ // return centroid;
533
+ return Polygon.utils.calculateCentroid(this.vertices, forceClockwise);
515
534
  }
516
535
 
517
536
  //--- BEGIN --- Implement interface `Intersectable`
@@ -972,6 +991,27 @@ export class Polygon implements IBounded, Intersectable, SVGSerializable {
972
991
  return Polygon.utils.signedArea(vertices) < 0;
973
992
  },
974
993
 
994
+ calculateCentroid(vertices: XYCoords[], forceClockwise?: boolean): Vertex {
995
+ const centroid: Vertex = new Vertex(0.0, 0.0);
996
+ const n = vertices.length;
997
+ if (forceClockwise && !Polygon.utils.isClockwise(vertices)) {
998
+ return Polygon.utils.calculateCentroid(vertices.slice().reverse());
999
+ }
1000
+ for (var i = 0; i < n; i++) {
1001
+ // center.add(this.vertices[i]);
1002
+ const cur: XYCoords = vertices[i];
1003
+ const next: XYCoords = vertices[(i + 1) % n];
1004
+ var factor: number = cur.x * next.y - next.x * cur.y;
1005
+ centroid.x += (cur.x + next.x) * factor;
1006
+ centroid.y += (cur.y + next.y) * factor;
1007
+ }
1008
+ // const area = this.area();
1009
+ const area = Polygon.utils.area(vertices);
1010
+ centroid.x *= 1 / (6 * area);
1011
+ centroid.y *= 1 / (6 * area);
1012
+ return centroid;
1013
+ },
1014
+
975
1015
  /**
976
1016
  * Calulate the signed polyon area by interpreting the polygon as a matrix
977
1017
  * and calculating its determinant.
@@ -17,13 +17,17 @@
17
17
  * @modified 2025-04-15 Changed param of `VertTuple.moveTo` method from `Vertex` to `XYCoords`.
18
18
  * @modified 2025-04-15 Added method `VertTuple.move` method.
19
19
  * @modified 2026-06-10 Adding helper function `VertTuple.utils.calcCircumcircle`.
20
- * @version 1.5.0
20
+ * @modified 2026-06-17 Adding method `VertTuple.asLine` for converting TertTuples to Line instances.
21
+ * @modified 2026-09-21 Adding method `VertTuple.asVector` for converting TertTuples to Vector instances.
22
+ * @version 1.7.0
21
23
  */
22
24
 
23
25
  import { Vertex } from "./Vertex";
24
26
  import { UIDGenerator } from "./UIDGenerator";
25
27
  import { XYCoords, UID } from "./interfaces";
26
28
  import { ICircle } from "./Circle";
29
+ import { Line } from "./Line";
30
+ import { Vector } from "./Vector";
27
31
 
28
32
  /**
29
33
  * @classdesc An abstract base classes for vertex tuple constructs, like Lines or Vectors.
@@ -360,7 +364,7 @@ export class VertTuple<T extends VertTuple<T>> {
360
364
  /**
361
365
  * Create a deep clone of this instance.
362
366
  *
363
- * @method cloneLine
367
+ * @method clone
364
368
  * @return {T} A type safe clone if this instance.
365
369
  * @instance
366
370
  * @memberof VertTuple
@@ -369,6 +373,30 @@ export class VertTuple<T extends VertTuple<T>> {
369
373
  return this.factory(this.a.clone(), this.b.clone());
370
374
  }
371
375
 
376
+ /**
377
+ * Converts this `VertTuple` to a `Line` (segment).
378
+ *
379
+ * @method asLine
380
+ * @return {Line} This tuple as `Line` instance.
381
+ * @instance
382
+ * @memberof VertTuple
383
+ **/
384
+ asLine(): Line {
385
+ return new Line(this.a, this.b);
386
+ }
387
+
388
+ /**
389
+ * Converts this `VertTuple` to a `Vector` (arrow).
390
+ *
391
+ * @method asVector
392
+ * @return {Line} This tuple as `Vector` instance.
393
+ * @instance
394
+ * @memberof VertTuple
395
+ **/
396
+ asVector(): Vector {
397
+ return new Vector(this.a, this.b);
398
+ }
399
+
372
400
  /**
373
401
  * Create a string representation of this line.
374
402
  *
package/src/ts/Vertex.ts CHANGED
@@ -37,7 +37,7 @@
37
37
  * @modified 2025-04-13 Adding the `Vertex.move(amount: XYCoords)` method (does the same as `add`, added by naming convention).
38
38
  * @modified 2025-05-07 Class `Vertex` is now implementing interface `IBounded` (to meet convention).
39
39
  * @modified 2026-06-10 Adding methods `Vertex.findClosestPoint` and `Vertex.findFarestPoint`.
40
- * @version 2.12.0
40
+ * @version 2.12.1
41
41
  *
42
42
  * @file Vertex
43
43
  * @public
@@ -569,7 +569,7 @@ export class Vertex implements IBounded, XYCoords, SVGSerializable {
569
569
  * around given center.
570
570
  *
571
571
  * @method rotate
572
- * @param {number} angle - The angle to 'rotate' this vertex; 0.0 means no change.
572
+ * @param {number} angle - The angle in radians to 'rotate' this vertex; 0.0 means no change.
573
573
  * @param {XYCoords=} center - The center of rotation; default is (0,0).
574
574
  * @return {Vertex} this
575
575
  * @instance
@@ -0,0 +1,22 @@
1
+ /**
2
+ * An experimental top class to wrap demo environments.
3
+ *
4
+ * @date 2026-04-13
5
+ * @modified 2026-09-15 Ported to Typescript.
6
+ * @author Ikaros Kappler
7
+ * @version 1.0.0
8
+ */
9
+
10
+ import PlotBoilerplate from "../PlotBoilerplate";
11
+
12
+ export class AppContext<GlobalVars = Record<string, any>> {
13
+ pb: PlotBoilerplate;
14
+ config: any;
15
+
16
+ globals: Record<keyof GlobalVars, GlobalVars[keyof GlobalVars]>;
17
+
18
+ constructor(pb, config) {
19
+ this.pb = pb;
20
+ this.config = config;
21
+ }
22
+ }