plotboilerplate 1.14.0 → 1.15.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 (38) hide show
  1. package/README.md +1 -0
  2. package/changelog.md +8 -0
  3. package/dist/index.esm.js +144 -42
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.esm.min.js +1 -1
  6. package/dist/index.esm.min.js.map +1 -1
  7. package/dist/plotboilerplate.browser.min.js +1 -1
  8. package/dist/plotboilerplate.browser.min.js.map +1 -1
  9. package/dist/plotboilerplate.js +144 -42
  10. package/dist/plotboilerplate.min.js +1 -1
  11. package/package.json +1 -1
  12. package/src/cjs/PlotBoilerplate.js +2 -1
  13. package/src/cjs/PlotBoilerplate.js.map +1 -1
  14. package/src/cjs/Polygon.d.ts +22 -1
  15. package/src/cjs/Polygon.js +102 -39
  16. package/src/cjs/Polygon.js.map +1 -1
  17. package/src/cjs/Vertex.d.ts +25 -1
  18. package/src/cjs/Vertex.js +39 -1
  19. package/src/cjs/Vertex.js.map +1 -1
  20. package/src/cjs/utils/findInVertexArray.d.ts +19 -0
  21. package/src/cjs/utils/findInVertexArray.js +29 -0
  22. package/src/cjs/utils/findInVertexArray.js.map +1 -0
  23. package/src/esm/PlotBoilerplate.js +2 -1
  24. package/src/esm/PlotBoilerplate.js.map +1 -1
  25. package/src/esm/Polygon.d.ts +22 -1
  26. package/src/esm/Polygon.js +102 -39
  27. package/src/esm/Polygon.js.map +1 -1
  28. package/src/esm/Vertex.d.ts +25 -1
  29. package/src/esm/Vertex.js +39 -1
  30. package/src/esm/Vertex.js.map +1 -1
  31. package/src/esm/utils/findInVertexArray.d.ts +19 -0
  32. package/src/esm/utils/findInVertexArray.js +25 -0
  33. package/src/esm/utils/findInVertexArray.js.map +1 -0
  34. package/src/ts/PlotBoilerplate.ts +3 -1
  35. package/src/ts/Polygon.ts +493 -451
  36. package/src/ts/Vertex.ts +41 -1
  37. package/src/ts/utils/findInVertexArray.ts +28 -0
  38. package/src/ts/utils/algorithms/sh-tmp.ts~ +0 -59
@@ -4826,8 +4826,9 @@ var PlotBoilerplate = /** @class */ (function () {
4826
4826
  **/
4827
4827
  PlotBoilerplate.prototype.getVertexNear = function (pixelPosition, pixelTolerance) {
4828
4828
  var p = this.locatePointNear(this.transformMousePosition(pixelPosition.x, pixelPosition.y), pixelTolerance / Math.min(this.config.cssScaleX, this.config.cssScaleY));
4829
- if (p && p.typeName == "vertex")
4829
+ if (p && p.typeName == "vertex") {
4830
4830
  return this.vertices[p.vindex];
4831
+ }
4831
4832
  return undefined;
4832
4833
  };
4833
4834
  /**
@@ -6127,7 +6128,9 @@ __webpack_unused_export__ = PlotBoilerplate;
6127
6128
  * @modified 2021-01-29 Added the `isClockwise` function.
6128
6129
  * @modified 2021-01-29 Added the `area` function.
6129
6130
  * @modified 2021-01-29 Changed the param type for `containsVert` from Vertex to XYCoords.
6130
- * @version 1.7.0
6131
+ * @modified 2021-12-14 Added the `perimeter()` function.
6132
+ * @modified 2021-12-16 Added the `getEvenDistributionPolygon()` function.
6133
+ * @version 1.8.0
6131
6134
  *
6132
6135
  * @file Polygon
6133
6136
  * @public
@@ -6164,12 +6167,11 @@ var Polygon = /** @class */ (function () {
6164
6167
  **/
6165
6168
  this.className = "Polygon";
6166
6169
  this.uid = UIDGenerator_1.UIDGenerator.next();
6167
- if (typeof vertices == 'undefined')
6170
+ if (typeof vertices == "undefined")
6168
6171
  vertices = [];
6169
6172
  this.vertices = vertices;
6170
6173
  this.isOpen = isOpen;
6171
6174
  }
6172
- ;
6173
6175
  /**
6174
6176
  * Add a vertex to the end of the `vertices` array.
6175
6177
  *
@@ -6181,7 +6183,6 @@ var Polygon = /** @class */ (function () {
6181
6183
  Polygon.prototype.addVertex = function (vert) {
6182
6184
  this.vertices.push(vert);
6183
6185
  };
6184
- ;
6185
6186
  /**
6186
6187
  * Get the polygon vertex at the given position (index).
6187
6188
  *
@@ -6204,7 +6205,6 @@ var Polygon = /** @class */ (function () {
6204
6205
  else
6205
6206
  return this.vertices[index % this.vertices.length];
6206
6207
  };
6207
- ;
6208
6208
  /**
6209
6209
  * Move the polygon's vertices by the given amount.
6210
6210
  *
@@ -6220,7 +6220,6 @@ var Polygon = /** @class */ (function () {
6220
6220
  }
6221
6221
  return this;
6222
6222
  };
6223
- ;
6224
6223
  /**
6225
6224
  * Check if the given vertex is inside this polygon.<br>
6226
6225
  * <br>
@@ -6240,14 +6239,12 @@ var Polygon = /** @class */ (function () {
6240
6239
  for (var i = 0, j = this.vertices.length - 1; i < this.vertices.length; j = i++) {
6241
6240
  var xi = this.vertices[i].x, yi = this.vertices[i].y;
6242
6241
  var xj = this.vertices[j].x, yj = this.vertices[j].y;
6243
- var intersect = ((yi > vert.y) != (yj > vert.y))
6244
- && (vert.x < (xj - xi) * (vert.y - yi) / (yj - yi) + xi);
6242
+ var intersect = yi > vert.y != yj > vert.y && vert.x < ((xj - xi) * (vert.y - yi)) / (yj - yi) + xi;
6245
6243
  if (intersect)
6246
6244
  inside = !inside;
6247
6245
  }
6248
6246
  return inside;
6249
6247
  };
6250
- ;
6251
6248
  /**
6252
6249
  * Calculate the area of the given polygon (unsigned).
6253
6250
  *
@@ -6261,7 +6258,6 @@ var Polygon = /** @class */ (function () {
6261
6258
  Polygon.prototype.area = function () {
6262
6259
  return Polygon.utils.area(this.vertices);
6263
6260
  };
6264
- ;
6265
6261
  /**
6266
6262
  * Calulate the signed polyon area by interpreting the polygon as a matrix
6267
6263
  * and calculating its determinant.
@@ -6274,7 +6270,6 @@ var Polygon = /** @class */ (function () {
6274
6270
  Polygon.prototype.signedArea = function () {
6275
6271
  return Polygon.utils.signedArea(this.vertices);
6276
6272
  };
6277
- ;
6278
6273
  /**
6279
6274
  * Get the winding order of this polgon: clockwise or counterclockwise.
6280
6275
  *
@@ -6286,7 +6281,28 @@ var Polygon = /** @class */ (function () {
6286
6281
  Polygon.prototype.isClockwise = function () {
6287
6282
  return Polygon.utils.signedArea(this.vertices) < 0;
6288
6283
  };
6289
- ;
6284
+ /**
6285
+ * Get the perimeter of this polygon.
6286
+ * The perimeter is the absolute length of the outline.
6287
+ *
6288
+ * If this polygon is open then the last segment (connecting the first and the
6289
+ * last vertex) will be skipped.
6290
+ *
6291
+ * @method perimeter
6292
+ * @instance
6293
+ * @memberof Polygon
6294
+ * @return {number}
6295
+ */
6296
+ Polygon.prototype.perimeter = function () {
6297
+ var length = 0;
6298
+ for (var i = 1; i < this.vertices.length; i++) {
6299
+ length += this.vertices[i - 1].distance(this.vertices[i]);
6300
+ }
6301
+ if (!this.isOpen && this.vertices.length > 1) {
6302
+ length += this.vertices[0].distance(this.vertices[this.vertices.length - 1]);
6303
+ }
6304
+ return length;
6305
+ };
6290
6306
  /**
6291
6307
  * Scale the polygon relative to the given center.
6292
6308
  *
@@ -6299,14 +6315,13 @@ var Polygon = /** @class */ (function () {
6299
6315
  **/
6300
6316
  Polygon.prototype.scale = function (factor, center) {
6301
6317
  for (var i in this.vertices) {
6302
- if (typeof this.vertices[i].scale == 'function')
6318
+ if (typeof this.vertices[i].scale == "function")
6303
6319
  this.vertices[i].scale(factor, center);
6304
6320
  else
6305
- console.log('There seems to be a null vertex!', this.vertices[i]);
6321
+ console.log("There seems to be a null vertex!", this.vertices[i]);
6306
6322
  }
6307
6323
  return this;
6308
6324
  };
6309
- ;
6310
6325
  /**
6311
6326
  * Rotate the polygon around the given center.
6312
6327
  *
@@ -6323,7 +6338,52 @@ var Polygon = /** @class */ (function () {
6323
6338
  }
6324
6339
  return this;
6325
6340
  };
6326
- ;
6341
+ /**
6342
+ * Convert this polygon into a new polygon with n evenly distributed vertices.
6343
+ *
6344
+ * @param {number} pointCount - Must not be negative.
6345
+ */
6346
+ Polygon.prototype.getEvenDistributionPolygon = function (pointCount) {
6347
+ if (pointCount <= 0) {
6348
+ throw new Error("pointCount must be larger than zero; is " + pointCount + ".");
6349
+ }
6350
+ var result = new Polygon([], this.isOpen);
6351
+ if (this.vertices.length === 0) {
6352
+ return result;
6353
+ }
6354
+ // Fetch and add the start point from the source polygon
6355
+ var polygonPoint = new Vertex_1.Vertex(this.vertices[0]);
6356
+ result.vertices.push(polygonPoint);
6357
+ if (this.vertices.length === 1) {
6358
+ return result;
6359
+ }
6360
+ var perimeter = this.perimeter();
6361
+ var stepSize = perimeter / pointCount;
6362
+ var n = this.vertices.length;
6363
+ var polygonIndex = 1;
6364
+ var nextPolygonPoint = new Vertex_1.Vertex(this.vertices[1]);
6365
+ var segmentLength = polygonPoint.distance(nextPolygonPoint);
6366
+ var loopMax = this.isOpen ? n : n + 1;
6367
+ var curSegmentU = stepSize;
6368
+ var i = 1;
6369
+ while (i < pointCount && polygonIndex < loopMax) {
6370
+ // Check if next eq point is inside this segment
6371
+ if (curSegmentU < segmentLength) {
6372
+ var newPoint = polygonPoint.clone().lerpAbs(nextPolygonPoint, curSegmentU);
6373
+ result.vertices.push(newPoint);
6374
+ curSegmentU += stepSize;
6375
+ i++;
6376
+ }
6377
+ else {
6378
+ polygonIndex++;
6379
+ polygonPoint = nextPolygonPoint;
6380
+ nextPolygonPoint = new Vertex_1.Vertex(this.vertices[polygonIndex % n]);
6381
+ curSegmentU = curSegmentU - segmentLength;
6382
+ segmentLength = polygonPoint.distance(nextPolygonPoint);
6383
+ }
6384
+ }
6385
+ return result;
6386
+ };
6327
6387
  /**
6328
6388
  * Get the bounding box (bounds) of this polygon.
6329
6389
  *
@@ -6335,7 +6395,6 @@ var Polygon = /** @class */ (function () {
6335
6395
  Polygon.prototype.getBounds = function () {
6336
6396
  return Bounds_1.Bounds.computeFromVertices(this.vertices);
6337
6397
  };
6338
- ;
6339
6398
  /**
6340
6399
  * Convert this polygon to a sequence of quadratic Bézier curves.<br>
6341
6400
  * <br>
@@ -6367,7 +6426,6 @@ var Polygon = /** @class */ (function () {
6367
6426
  }
6368
6427
  return qbezier;
6369
6428
  };
6370
- ;
6371
6429
  /**
6372
6430
  * Convert this polygon to a quadratic bezier curve, represented as an SVG data string.
6373
6431
  *
@@ -6380,13 +6438,12 @@ var Polygon = /** @class */ (function () {
6380
6438
  var qdata = this.toQuadraticBezierData();
6381
6439
  if (qdata.length == 0)
6382
6440
  return "";
6383
- var buffer = ['M ' + qdata[0].x + ' ' + qdata[0].y];
6441
+ var buffer = ["M " + qdata[0].x + " " + qdata[0].y];
6384
6442
  for (var i = 1; i < qdata.length; i += 2) {
6385
- buffer.push('Q ' + qdata[i].x + ' ' + qdata[i].y + ', ' + qdata[i + 1].x + ' ' + qdata[i + 1].y);
6443
+ buffer.push("Q " + qdata[i].x + " " + qdata[i].y + ", " + qdata[i + 1].x + " " + qdata[i + 1].y);
6386
6444
  }
6387
- return buffer.join(' ');
6445
+ return buffer.join(" ");
6388
6446
  };
6389
- ;
6390
6447
  /**
6391
6448
  * Convert this polygon to a sequence of cubic Bézier curves.<br>
6392
6449
  * <br>
@@ -6403,7 +6460,7 @@ var Polygon = /** @class */ (function () {
6403
6460
  * @memberof Polygon
6404
6461
  **/
6405
6462
  Polygon.prototype.toCubicBezierData = function (threshold) {
6406
- if (typeof threshold == 'undefined')
6463
+ if (typeof threshold == "undefined")
6407
6464
  threshold = 1.0;
6408
6465
  if (this.vertices.length < 3)
6409
6466
  return [];
@@ -6427,7 +6484,6 @@ var Polygon = /** @class */ (function () {
6427
6484
  }
6428
6485
  return cbezier;
6429
6486
  };
6430
- ;
6431
6487
  /**
6432
6488
  * Convert this polygon to a cubic bezier curve, represented as an SVG data string.
6433
6489
  *
@@ -6440,13 +6496,23 @@ var Polygon = /** @class */ (function () {
6440
6496
  var qdata = this.toCubicBezierData(threshold);
6441
6497
  if (qdata.length == 0)
6442
6498
  return "";
6443
- var buffer = ['M ' + qdata[0].x + ' ' + qdata[0].y];
6499
+ var buffer = ["M " + qdata[0].x + " " + qdata[0].y];
6444
6500
  for (var i = 1; i < qdata.length; i += 3) {
6445
- buffer.push('C ' + qdata[i].x + ' ' + qdata[i].y + ', ' + qdata[i + 1].x + ' ' + qdata[i + 1].y + ', ' + qdata[i + 2].x + ' ' + qdata[i + 2].y);
6446
- }
6447
- return buffer.join(' ');
6501
+ buffer.push("C " +
6502
+ qdata[i].x +
6503
+ " " +
6504
+ qdata[i].y +
6505
+ ", " +
6506
+ qdata[i + 1].x +
6507
+ " " +
6508
+ qdata[i + 1].y +
6509
+ ", " +
6510
+ qdata[i + 2].x +
6511
+ " " +
6512
+ qdata[i + 2].y);
6513
+ }
6514
+ return buffer.join(" ");
6448
6515
  };
6449
- ;
6450
6516
  /**
6451
6517
  * Convert this polygon to a cubic bezier path instance.
6452
6518
  *
@@ -6465,7 +6531,6 @@ var Polygon = /** @class */ (function () {
6465
6531
  }
6466
6532
  return BezierPath_1.BezierPath.fromArray(pathdata);
6467
6533
  };
6468
- ;
6469
6534
  /**
6470
6535
  * Create an SVG representation of this polygon.
6471
6536
  *
@@ -6479,29 +6544,28 @@ var Polygon = /** @class */ (function () {
6479
6544
  Polygon.prototype.toSVGString = function (options) {
6480
6545
  options = options || {};
6481
6546
  var buffer = [];
6482
- buffer.push('<path');
6547
+ buffer.push("<path");
6483
6548
  if (options.className)
6484
6549
  buffer.push(' class="' + options.className + '"');
6485
6550
  buffer.push(' d="');
6486
6551
  if (this.vertices.length > 0) {
6487
- buffer.push('M ');
6552
+ buffer.push("M ");
6488
6553
  buffer.push(this.vertices[0].x.toString());
6489
- buffer.push(' ');
6554
+ buffer.push(" ");
6490
6555
  buffer.push(this.vertices[0].y.toString());
6491
6556
  for (var i = 1; i < this.vertices.length; i++) {
6492
- buffer.push(' L ');
6557
+ buffer.push(" L ");
6493
6558
  buffer.push(this.vertices[i].x.toString());
6494
- buffer.push(' ');
6559
+ buffer.push(" ");
6495
6560
  buffer.push(this.vertices[i].y.toString());
6496
6561
  }
6497
6562
  if (!this.isOpen) {
6498
- buffer.push(' Z');
6563
+ buffer.push(" Z");
6499
6564
  }
6500
6565
  }
6501
6566
  buffer.push('" />');
6502
- return buffer.join('');
6567
+ return buffer.join("");
6503
6568
  };
6504
- ;
6505
6569
  Polygon.utils = {
6506
6570
  /**
6507
6571
  * Calculate the area of the given polygon (unsigned).
@@ -6520,8 +6584,8 @@ var Polygon = /** @class */ (function () {
6520
6584
  var addY = vertices[(i + 1) % l].y;
6521
6585
  var subX = vertices[(i + 1) % l].x;
6522
6586
  var subY = vertices[i].y;
6523
- total += (addX * addY * 0.5);
6524
- total -= (subX * subY * 0.5);
6587
+ total += addX * addY * 0.5;
6588
+ total -= subX * subY * 0.5;
6525
6589
  }
6526
6590
  return Math.abs(total);
6527
6591
  },
@@ -8289,7 +8353,8 @@ exports.VertTuple = VertTuple;
8289
8353
  * @modified 2021-03-01 Changed the second param `center` in the `rotate` function from Vertex to XYCoords.
8290
8354
  * @modified 2021-12-01 Changed the type of param of `scale` to XYCoords.
8291
8355
  * @modified 2021-12-01 Added function `scaleXY` for non uniform scaling.
8292
- * @version 2.5.0
8356
+ * @modified 2021-12-17 Added the functions `lerp` and `lerpAbs` for linear interpolations.
8357
+ * @version 2.6.0
8293
8358
  *
8294
8359
  * @file Vertex
8295
8360
  * @public
@@ -8647,6 +8712,43 @@ var Vertex = /** @class */ (function () {
8647
8712
  Vertex.prototype.scale = function (factor, center) {
8648
8713
  return this.scaleXY({ x: factor, y: factor }, center);
8649
8714
  };
8715
+ /**
8716
+ * Perform a linear interpolation towards the given target vertex.
8717
+ * The amount value `t` is relative, `t=0.0` means no change, `t=1.0`
8718
+ * means this point will be moved to the exact target position.
8719
+ *
8720
+ * `t=0.5` will move this point to the middle of the connecting
8721
+ * linear segment.
8722
+ *
8723
+ * @param {XYCoords} target - The target position to lerp this vertex to.
8724
+ * @param {number} t - The relative amount, usually in [0..1], but other values will work, too.
8725
+ * @returns
8726
+ */
8727
+ Vertex.prototype.lerp = function (target, t) {
8728
+ var diff = this.difference(target);
8729
+ // return new Vertex(this.x + diff.x * t, this.y + diff.y * t);
8730
+ this.x += diff.x * t;
8731
+ this.y += diff.y * t;
8732
+ return this;
8733
+ };
8734
+ /**
8735
+ * Perform a linear interpolation towards the given target vertex (absolute variant).
8736
+ * The amount value `t` is absolute, which means the lerp amount is a direct distance
8737
+ * value. This point will have move the amount of the passed distance `u`.
8738
+ *
8739
+ * @param {XYCoords} target - The target position to lerp this vertex to.
8740
+ * @param {number} t - The absolute move amount to use to lerping.
8741
+ * @returns
8742
+ */
8743
+ Vertex.prototype.lerpAbs = function (target, u) {
8744
+ var dist = this.distance(target);
8745
+ var diff = this.difference(target);
8746
+ var step = { x: diff.x / dist, y: diff.y / dist };
8747
+ // return new Vertex(this.x + step.x * u, this.y + step.y * u);
8748
+ this.x += step.x * u;
8749
+ this.y += step.y * u;
8750
+ return this;
8751
+ };
8650
8752
  /**
8651
8753
  * This is a vector-like behavior and 'scales' this vertex
8652
8754
  * towards/from a given center by two independent x- and y- scale factors.