dgeoutils 2.2.22 → 2.2.23

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.
@@ -2,6 +2,7 @@
2
2
  import { DCoord, DPoint, LatLng } from './DPoint';
3
3
  import { DLine } from './DLine';
4
4
  import { DPolygonLoop } from './DPolygonLoop';
5
+ import { True } from './utils';
5
6
  export declare const MIN_POINTS_IN_VALID_POLYGON = 3;
6
7
  export declare class DPolygon {
7
8
  private pPoints;
@@ -102,8 +103,11 @@ export declare class DPolygon {
102
103
  getTrianglesPointIndexes(): number[];
103
104
  get closed(): boolean;
104
105
  buffer(v: number, quadrantSegments?: number, type?: number): DPolygon;
106
+ sideBuffers(v: number, quadrantSegments?: number): [DPolygon, DPolygon];
105
107
  bezier(step?: number): DPolygon;
106
108
  setGrowingHeight(from: number, to: number): DPolygon;
109
+ loopPointsGenerator(): () => Generator<[DPoint, DPoint, undefined, number]>;
110
+ loopPointsGenerator(withLine: True): () => Generator<[DPoint, DPoint, DLine, number]>;
107
111
  private getBezierPoint;
108
112
  private simpleIncludeX;
109
113
  private simpleIncludeY;
package/dist/DPolygon.js CHANGED
@@ -18,21 +18,16 @@ const containCalculator = (poly, p) => {
18
18
  if (hasSamePoint) {
19
19
  return true;
20
20
  }
21
- for (let i = 0; i < poly.length - 1; i++) {
22
- const p0 = poly.at(i);
23
- const p1 = poly.at(i + 1);
24
- const polygonLine = p0.findLine(p1);
21
+ for (const [, , polygonLine] of poly.loopPointsGenerator(true)()) {
25
22
  const onBorder = polygonLine.x(p).equal(p) && polygonLine.inRange(p);
26
23
  if (onBorder) {
27
24
  return true;
28
25
  }
29
26
  }
30
27
  let totalFi = 0;
31
- for (let i = 0; i < poly.length - 1; i++) {
32
- const p1 = poly.at(i);
33
- const p2 = poly.at(i + 1);
34
- const line1 = new DLine_1.DLine(p1.x - p.x, p1.y - p.y, 0);
35
- const line2 = new DLine_1.DLine(p2.x - p.x, p2.y - p.y, 0);
28
+ for (const [{ x, y }, { x: a, y: b }] of poly.loopPointsGenerator()()) {
29
+ const line1 = new DLine_1.DLine(x - p.x, y - p.y, 0);
30
+ const line2 = new DLine_1.DLine(a - p.x, b - p.y, 0);
36
31
  const fiDif = line1.findFi(line2);
37
32
  if (line1.vectorProduct(line2).c > 0) {
38
33
  totalFi += fiDif;
@@ -198,18 +193,15 @@ class DPolygon {
198
193
  }
199
194
  get perimeter() {
200
195
  let p = 0;
201
- for (let i = 1; i < this.pPoints.length; i++) {
202
- p += this.pPoints[i - 1].distance(this.pPoints[i]);
196
+ for (const [p1, p2] of this.loopPointsGenerator()()) {
197
+ p += p1.distance(p2);
203
198
  }
204
199
  return p;
205
200
  }
206
201
  get area() {
207
- const closed = this.deintersection;
208
202
  let sum = 0;
209
- for (let i = 1; i < closed.length; i++) {
210
- const cur = closed.at(i);
211
- const prev = closed.at(i - 1);
212
- sum += prev.x * cur.y - prev.y * cur.x;
203
+ for (const [{ x, y }, { x: a, y: b }] of this.deintersection.loopPointsGenerator()()) {
204
+ sum += x * b - y * a;
213
205
  }
214
206
  return Math.abs(sum / 2) - this.holes.reduce((a, hole) => a + hole.area, 0);
215
207
  }
@@ -269,19 +261,18 @@ class DPolygon {
269
261
  const p = this.convex;
270
262
  let resultPolygon = new DPolygon();
271
263
  let resultArea = Infinity;
272
- for (let k = 0; k < p.length - 1; k++) {
273
- const l = p.at(k).findLine(p.at(k + 1));
264
+ for (const [, , l] of p.loopPointsGenerator(true)()) {
274
265
  let maxWidth = 0;
275
266
  let maxWidthPoint1 = null;
276
267
  let maxWidthPoint2 = null;
277
268
  let maxHeight = 0;
278
269
  let maxHeightPoint = null;
279
- for (let i = 0; i < p.length - 1; i++) {
280
- const p1 = l.findPoint(l.findPerpendicular(p.at(i)));
281
- const h = p1.distance(p.at(i));
270
+ for (const [z, , , i] of p.loopPointsGenerator()()) {
271
+ const p1 = l.findPoint(l.findPerpendicular(z));
272
+ const h = p1.distance(z);
282
273
  if (h >= maxHeight) {
283
274
  maxHeight = h;
284
- maxHeightPoint = p.at(i);
275
+ maxHeightPoint = z;
285
276
  }
286
277
  for (let j = i; j < p.length - 1; j++) {
287
278
  const p2 = l.findPoint(l.findPerpendicular(p.at(j)));
@@ -354,11 +345,9 @@ class DPolygon {
354
345
  }
355
346
  get isClockwise() {
356
347
  let sum = 0;
357
- const p = this.clone().close();
358
- for (let i = 1; i < p.length; i++) {
359
- const p1 = p.at(i - 1);
360
- const p2 = p.at(i);
361
- sum += (p2.x - p1.x) * (p2.y + p1.y);
348
+ for (const [{ x, y }, { x: a, y: b }] of this.clone().close()
349
+ .loopPointsGenerator()()) {
350
+ sum += (a - x) * (b + y);
362
351
  }
363
352
  return sum < 0;
364
353
  }
@@ -375,10 +364,7 @@ class DPolygon {
375
364
  }
376
365
  intersection(l, includeOnly = false) {
377
366
  const res = [];
378
- for (let i = 0; i < this.pPoints.length - 1; i++) {
379
- const p1 = this.pPoints[i];
380
- const p2 = this.pPoints[i + 1];
381
- const line = p1.findLine(p2);
367
+ for (const [, , line] of this.loopPointsGenerator(true)()) {
382
368
  const intersect = line.intersection(l, 0, includeOnly);
383
369
  if (intersect) {
384
370
  res.push(intersect);
@@ -572,11 +558,9 @@ class DPolygon {
572
558
  }
573
559
  const poly = this.deintersection;
574
560
  let totalFi = 0;
575
- for (let i = 0; i < poly.length - 1; i++) {
576
- const p1 = poly.at(i);
577
- const p2 = poly.at(i + 1);
578
- const line1 = new DLine_1.DLine(p1.x - p.x, p1.y - p.y, 0);
579
- const line2 = new DLine_1.DLine(p2.x - p.x, p2.y - p.y, 0);
561
+ for (const [{ x, y }, { x: a, y: b }] of poly.loopPointsGenerator()()) {
562
+ const line1 = new DLine_1.DLine(x - p.x, y - p.y, 0);
563
+ const line2 = new DLine_1.DLine(a - p.x, b - p.y, 0);
580
564
  const fiDif = line1.findFi(line2);
581
565
  if (line1.vectorProduct(line2).c > 0) {
582
566
  totalFi += fiDif;
@@ -603,10 +587,7 @@ class DPolygon {
603
587
  if (hasSamePoint) {
604
588
  return true;
605
589
  }
606
- for (let i = 0; i < poly.length - 1; i++) {
607
- const p0 = poly.at(i);
608
- const p1 = poly.at(i + 1);
609
- const polygonLine = p0.findLine(p1);
590
+ for (const [, , polygonLine] of poly.loopPointsGenerator(true)()) {
610
591
  const onBorder = polygonLine.x(p).equal(p) && polygonLine.inRange(p);
611
592
  if (onBorder) {
612
593
  return true;
@@ -642,9 +623,7 @@ class DPolygon {
642
623
  const { fullLength } = this;
643
624
  const pieceLength = fullLength / piecesCount;
644
625
  let currentPieceLength = pieceLength;
645
- for (let i = 1; i < this.length; i++) {
646
- const p1 = this.at(i - 1);
647
- const p2 = this.at(i);
626
+ for (const [p1, p2, , i] of this.loopPointsGenerator()()) {
648
627
  const d = p1.distance(p2);
649
628
  if (d === currentPieceLength) {
650
629
  p2.properties.pieceBorder = true;
@@ -656,7 +635,7 @@ class DPolygon {
656
635
  const intersectionPoint = line.intersectionWithCircle(circle)
657
636
  .filter((p) => line.inRange(p, CLOSE_TO_INTERSECTION_DISTANCE))[0];
658
637
  intersectionPoint.properties.pieceBorder = true;
659
- this.insertAfter(i - 1, intersectionPoint);
638
+ this.insertAfter(i, intersectionPoint);
660
639
  currentPieceLength = pieceLength;
661
640
  }
662
641
  else {
@@ -871,6 +850,17 @@ class DPolygon {
871
850
  .getCoordinates();
872
851
  return new DPolygon(points.map(({ x, y }) => new DPoint_1.DPoint(x, y)));
873
852
  }
853
+ sideBuffers(v, quadrantSegments = 64) {
854
+ const { first, last } = this;
855
+ const buffer = this.buffer(v, quadrantSegments, DPolygon.CAP_FLAT).open();
856
+ const [start0, start1] = first.sortByDistance(buffer).points.map((r) => r.properties.index);
857
+ const [end0, end1] = last.sortByDistance(buffer).points.map((r) => r.properties.index);
858
+ const fromPoint = Math.min(Math.max(start0, start1), Math.max(end0, end1));
859
+ const toPoint = Math.max(Math.min(start0, start1), Math.min(end0, end1));
860
+ const linePart = new DPolygon(buffer.removePart(fromPoint - 1, toPoint - fromPoint + 1));
861
+ buffer.unshift(buffer.pop());
862
+ return [linePart, buffer];
863
+ }
874
864
  bezier(step = 0.1) {
875
865
  const res = new DPolygon();
876
866
  for (let i = 0; i < 1; i += step) {
@@ -892,13 +882,21 @@ class DPolygon {
892
882
  .run();
893
883
  return this;
894
884
  }
885
+ loopPointsGenerator(withLine = false) {
886
+ const that = this;
887
+ return function* () {
888
+ for (let i = 0; i < that.length - 1; i++) {
889
+ const p1 = that.at(i);
890
+ const p2 = that.at(i + 1);
891
+ yield [p1, p2, withLine ? p1.findLine(p2) : undefined, i];
892
+ }
893
+ };
894
+ }
895
895
  getBezierPoint(v) {
896
896
  if (this.length === 1) {
897
897
  return this.first;
898
898
  }
899
- for (let i = 0; i < this.length - 1; i++) {
900
- const p1 = this.at(i);
901
- const p2 = this.at(i + 1);
899
+ for (const [p1, p2] of this.loopPointsGenerator()()) {
902
900
  p1.move(p2.clone().move(p1.clone().minus())
903
901
  .scale(v));
904
902
  }
package/dist/utils.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare const gaussianElimination: {
22
22
  (matrix: number[][]): number[];
23
23
  MIN: number;
24
24
  };
25
- declare type True = true;
25
+ export declare type True = true;
26
26
  export declare const createCanvas: {
27
27
  (size: number): [HTMLCanvasElement, CanvasRenderingContext2D];
28
28
  (size: number, offscreen: True): [OffscreenCanvas, OffscreenCanvasRenderingContext2D];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dgeoutils",
3
- "version": "2.2.22",
3
+ "version": "2.2.23",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "node_modules/.bin/tsc",