@immugio/three-math-extensions 0.0.3 → 0.0.5

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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,34 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
9
+
10
+ ## 0.0.4 - 2022-11-21
11
+
12
+ ### Commits
13
+
14
+ - Automatic publish with change log [`a70c99d`](https://github.com/Immugio/three-math-extensions/commit/a70c99d0d42e60411ce01bcdb8dd4f3f489bb949)
15
+ - Switch to npm [`ed47a17`](https://github.com/Immugio/three-math-extensions/commit/ed47a1787522db3bde5a2112bfd02480fe2edc54)
16
+ - Initial commit [`3d1bf9e`](https://github.com/Immugio/three-math-extensions/commit/3d1bf9ef015570830007c9be99140c8c8d760d7f)
17
+ - Add Line2D [`9a1dd0f`](https://github.com/Immugio/three-math-extensions/commit/9a1dd0f58352b7b25828693c688aa4770e95c174)
18
+ - Readme, npm info [`dda4a28`](https://github.com/Immugio/three-math-extensions/commit/dda4a282a71a00308dcae858ffe53d67d4185be8)
19
+ - Run tests in CI on commit [`e3f77ca`](https://github.com/Immugio/three-math-extensions/commit/e3f77ca76e25f4d99eef0130e5779b5e7c5aec6b)
20
+ - Remove babel.config.js [`c9a1e16`](https://github.com/Immugio/three-math-extensions/commit/c9a1e1607cffec8a3d74adcca644320a79ca4c43)
21
+ - Initial changelog [`6c98aa6`](https://github.com/Immugio/three-math-extensions/commit/6c98aa6ad631cf5e73d32dd76276f99b3ba5089f)
22
+ - Release 0.0.4 [`bdac840`](https://github.com/Immugio/three-math-extensions/commit/bdac840f33261a3f39ef33bc05c80772169cfe87)
23
+ - Add test badge [`37c62e8`](https://github.com/Immugio/three-math-extensions/commit/37c62e809bdcdfc63f7fe135469fd131f190f950)
24
+ - Edit publish script condition [`dc77d89`](https://github.com/Immugio/three-math-extensions/commit/dc77d8978d87daab8732d6429ad1b01ba0ca6f97)
25
+ - CI to use node version 16 [`dc42650`](https://github.com/Immugio/three-math-extensions/commit/dc426508939de922cc0316b7652bbd09aeed4610)
26
+ - Action rename [`26a1f01`](https://github.com/Immugio/three-math-extensions/commit/26a1f014827faab2c02d30ca8ed18dac8f9ff8af)
27
+ - Release 0.0.4 - tag pattern 4 [`ab9ad4e`](https://github.com/Immugio/three-math-extensions/commit/ab9ad4e49477a991482ed6d4d3a3ca4687a70b44)
28
+ - Release 0.0.4 - tag pattern 3 [`d7d4c8f`](https://github.com/Immugio/three-math-extensions/commit/d7d4c8f546e6550868ddd06a5213fdec6c68bcd2)
29
+ - Release 0.0.4 - tag pattern 2 [`0dcf801`](https://github.com/Immugio/three-math-extensions/commit/0dcf80190bd67a3ca93d45131a403201ab39e00f)
30
+ - Release 0.0.4 - tag pattern [`e7f2f76`](https://github.com/Immugio/three-math-extensions/commit/e7f2f76aa247e1c51f0522c300a7aaf07253039c)
31
+ - Release 0.0.5 [`c80d6bd`](https://github.com/Immugio/three-math-extensions/commit/c80d6bd12c25ddad0f67610cdf5c804d5759f084)
32
+ - Status badge update [`370843e`](https://github.com/Immugio/three-math-extensions/commit/370843ed0a28b024761555572b7d51ccbdb4b77d)
33
+ - Run tests in CI on commit [`aee8c55`](https://github.com/Immugio/three-math-extensions/commit/aee8c556ae1cab9025dbe0984dd17278fe6275a1)
34
+ - Add Line2D to index [`a5fb6bd`](https://github.com/Immugio/three-math-extensions/commit/a5fb6bdeee5d9f07f325bfffc31ef96f0ce167d1)
package/README.md CHANGED
@@ -1 +1,3 @@
1
- Set of utilities for 2d and 3d line math built on top of three.js
1
+ # Set of utilities for 2d and 3d line math built on top of three.js
2
+
3
+ [![Build](https://github.com/Immugio/three-math-extensions/actions/workflows/build.yml/badge.svg)](https://github.com/Immugio/three-math-extensions/actions/workflows/build.yml)
package/cjs/Line2D.js CHANGED
@@ -18,14 +18,23 @@ class Line2D {
18
18
  static fromPoints(p1, p2, index = 0) {
19
19
  return new Line2D(new Vec2_1.Vec2(p1.x, p1.y), new Vec2_1.Vec2(p2.x, p2.y), index);
20
20
  }
21
- static fromPolygon(polygon) {
22
- if (polygon[0].x === polygon[polygon.length - 1].x && polygon[0].y === polygon[polygon.length - 1].y) {
23
- polygon = polygon.slice(0, polygon.length - 1);
21
+ /**
22
+ * Creates a polygon formed by an array of lines from points provided.
23
+ * The polygon will only be closed if either
24
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
25
+ */
26
+ static fromPolygon(polygon, forceClosedPolygon = false) {
27
+ if (!polygon || polygon.length < 2) {
28
+ return [];
24
29
  }
25
- return polygon.map((p, i) => {
26
- const next = polygon[(i + 1) % polygon.length];
27
- return Line2D.fromPoints(p, next, i);
28
- });
30
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y)) {
31
+ polygon = [...polygon, polygon[0]];
32
+ }
33
+ const lines = [];
34
+ for (let i = 0; i < polygon.length - 1; i++) {
35
+ lines.push(Line2D.fromPoints(polygon[i], polygon[i + 1], i));
36
+ }
37
+ return lines;
29
38
  }
30
39
  static fromLength(length) {
31
40
  return Line2D.fromCoordinates(-length / 2, 0, length / 2, 0);
@@ -101,6 +110,14 @@ class Line2D {
101
110
  get length() {
102
111
  return this.start.distanceTo(this.end);
103
112
  }
113
+ /**
114
+ * Set the length of this line. Center and direction remain unchanged.
115
+ * @param length
116
+ */
117
+ setLength(length) {
118
+ this.length = length;
119
+ return this;
120
+ }
104
121
  /**
105
122
  * Returns the start and end points of the line as an array.
106
123
  * Endpoints are not cloned.
package/cjs/Line3D.js CHANGED
@@ -13,6 +13,24 @@ class Line3D extends three_1.Line3 {
13
13
  static fromPoints(start, end) {
14
14
  return new Line3D(new Vec3_1.Vec3(start.x, start.y, start.z), new Vec3_1.Vec3(end.x, end.y, end.z));
15
15
  }
16
+ /**
17
+ * Creates a polygon formed by an array of lines from points provided.
18
+ * The polygon will only be closed if either
19
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
20
+ */
21
+ static fromPolygon(polygon, forceClosedPolygon = false) {
22
+ if (!polygon || polygon.length < 2) {
23
+ return [];
24
+ }
25
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y || polygon[0].z !== polygon.at(-1).z)) {
26
+ polygon = [...polygon, polygon[0]];
27
+ }
28
+ const lines = [];
29
+ for (let i = 0; i < polygon.length - 1; i++) {
30
+ lines.push(Line3D.fromPoints(polygon[i], polygon[i + 1]));
31
+ }
32
+ return lines;
33
+ }
16
34
  /**
17
35
  * Returns lines that are the result of clipping this line by the @other line.
18
36
  * Clips must be parallel to this line.
package/cjs/Vec2.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Vec2 = void 0;
4
4
  const three_1 = require("three");
5
+ const Vec3_1 = require("./Vec3");
5
6
  class Vec2 extends three_1.Vector2 {
6
7
  static fromPoint(point) {
7
8
  return new Vec2(point.x, point.y);
@@ -15,5 +16,8 @@ class Vec2 extends three_1.Vector2 {
15
16
  }
16
17
  return this;
17
18
  }
19
+ in3DSpace(z = 0) {
20
+ return new Vec3_1.Vec3(this.x, z, this.y);
21
+ }
18
22
  }
19
23
  exports.Vec2 = Vec2;
package/esm/Line2D.js ADDED
@@ -0,0 +1,540 @@
1
+ import { Vector2 } from "three";
2
+ import { Vec2 } from "./Vec2";
3
+ export class Line2D {
4
+ start;
5
+ end;
6
+ index;
7
+ constructor(start, end, index = 0) {
8
+ this.start = start;
9
+ this.end = end;
10
+ this.index = index;
11
+ }
12
+ static fromCoordinates(x1, y1, x2, y2, index = 0) {
13
+ return new Line2D(new Vec2(x1, y1), new Vec2(x2, y2), index);
14
+ }
15
+ static fromPoints(p1, p2, index = 0) {
16
+ return new Line2D(new Vec2(p1.x, p1.y), new Vec2(p2.x, p2.y), index);
17
+ }
18
+ /**
19
+ * Creates a polygon formed by an array of lines from points provided.
20
+ * The polygon will only be closed if either
21
+ * 1) the first and last points are the same or 2) `forceClosedPolygon` is true.
22
+ */
23
+ static fromPolygon(polygon, forceClosedPolygon = false) {
24
+ if (!polygon || polygon.length < 2) {
25
+ return [];
26
+ }
27
+ if (forceClosedPolygon && (polygon[0].x !== polygon.at(-1).x || polygon[0].y !== polygon.at(-1).y)) {
28
+ polygon = [...polygon, polygon[0]];
29
+ }
30
+ const lines = [];
31
+ for (let i = 0; i < polygon.length - 1; i++) {
32
+ lines.push(Line2D.fromPoints(polygon[i], polygon[i + 1], i));
33
+ }
34
+ return lines;
35
+ }
36
+ static fromLength(length) {
37
+ return Line2D.fromCoordinates(-length / 2, 0, length / 2, 0);
38
+ }
39
+ get center() {
40
+ return new Vec2((this.start.x + this.end.x) / 2, (this.start.y + this.end.y) / 2);
41
+ }
42
+ /**
43
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
44
+ * Modifies this line.
45
+ * @param value
46
+ */
47
+ set center(value) {
48
+ const current = this.center;
49
+ const diffX = current.x - value.x;
50
+ const diffY = current.y - value.y;
51
+ this.start.x -= diffX;
52
+ this.start.y -= diffY;
53
+ this.end.x -= diffX;
54
+ this.end.y -= diffY;
55
+ }
56
+ /**
57
+ * Set the center of the line to the provided point. Length and direction remain unchanged.
58
+ * Modifies this line.
59
+ * @param value
60
+ */
61
+ setCenter(value) {
62
+ this.center = value;
63
+ return this;
64
+ }
65
+ /*
66
+ * Extends or reduces the line by the given length while keeping the center of the line constant.
67
+ * Modifies this line.
68
+ */
69
+ resize(amount) {
70
+ this.moveStartPoint(amount / 2);
71
+ this.moveEndPoint(amount / 2);
72
+ return this;
73
+ }
74
+ /*
75
+ * Moves start point on the line by the given amount. Plus values move the point further away from the center.
76
+ * Modifies this line.
77
+ */
78
+ moveStartPoint(amount) {
79
+ const p1 = this.movePointOnThisLine(this.start, amount);
80
+ this.start.copy(p1);
81
+ return this;
82
+ }
83
+ /**
84
+ * Moves end point on the line by the given amount. Plus values move the point further away from the center.
85
+ * Modifies this line.
86
+ */
87
+ moveEndPoint(amount) {
88
+ const p2 = this.movePointOnThisLine(this.end, amount);
89
+ this.end.copy(p2);
90
+ return this;
91
+ }
92
+ movePointOnThisLine(point, amount) {
93
+ const vec = new Vector2(this.center.x - point.x, this.center.y - point.y);
94
+ const length = vec.length();
95
+ vec.normalize().multiplyScalar(length + amount);
96
+ return new Vec2(this.center.x - vec.x, this.center.y - vec.y);
97
+ }
98
+ /**
99
+ * Set the length of this line. Center and direction remain unchanged.
100
+ * Modifies this line.
101
+ * @param l
102
+ */
103
+ set length(l) {
104
+ const length = this.length;
105
+ this.resize(l - length);
106
+ }
107
+ get length() {
108
+ return this.start.distanceTo(this.end);
109
+ }
110
+ /**
111
+ * Set the length of this line. Center and direction remain unchanged.
112
+ * @param length
113
+ */
114
+ setLength(length) {
115
+ this.length = length;
116
+ return this;
117
+ }
118
+ /**
119
+ * Returns the start and end points of the line as an array.
120
+ * Endpoints are not cloned.
121
+ */
122
+ get endpoints() {
123
+ return [this.start, this.end];
124
+ }
125
+ /**
126
+ * Returns the direction of this line.
127
+ */
128
+ get direction() {
129
+ return this.end.clone().sub(this.start).normalize();
130
+ }
131
+ /**
132
+ * Inverts the direction of the line.
133
+ * Modifies this line.
134
+ */
135
+ flip() {
136
+ const temp = this.start.clone();
137
+ this.start.copy(this.end);
138
+ this.end.copy(temp);
139
+ return this;
140
+ }
141
+ /**
142
+ * Rotates the line around the center by the given angle in radians.
143
+ * Modifies this line.
144
+ * @param radians Positive values rotate counter-clockwise.
145
+ * @param center
146
+ */
147
+ rotate(radians, center = this.center) {
148
+ this.start.rotateAround(center, radians);
149
+ this.end.rotateAround(center, radians);
150
+ return this;
151
+ }
152
+ /**
153
+ * Move the line by the given vector.
154
+ * Modifies this line.
155
+ */
156
+ translate(value) {
157
+ this.start.x += value.x;
158
+ this.start.y += value.y;
159
+ this.end.x += value.x;
160
+ this.end.y += value.y;
161
+ return this;
162
+ }
163
+ /**
164
+ * Move the line to its left by the given amount.
165
+ * Modifies this line.
166
+ */
167
+ translateLeft(amount) {
168
+ const translation = this.direction.rotateAround(new Vec2(), -Math.PI / 2).multiplyScalar(amount);
169
+ return this.translate(translation);
170
+ }
171
+ /**
172
+ * Move the line to its right by the given amount.
173
+ * Modifies this line.
174
+ */
175
+ translateRight(amount) {
176
+ const translation = this.direction.rotateAround(new Vec2(), Math.PI / 2).multiplyScalar(amount);
177
+ return this.translate(translation);
178
+ }
179
+ /**
180
+ * Returns true when the point is actually inside the (finite) line segment.
181
+ * https://jsfiddle.net/c06zdxtL/2/
182
+ * https://stackoverflow.com/questions/6865832/detecting-if-a-point-is-of-a-line-segment/6877674
183
+ * @param point: Point2
184
+ */
185
+ isPointOnLineSection(point) {
186
+ if (!this.isPointOnInfiniteLine(point)) {
187
+ return false;
188
+ }
189
+ return this.isPointBesideLineSection(point);
190
+ }
191
+ /**
192
+ * Returns true when the point is beside the line **segment** and within the maxDistance.
193
+ * @param point
194
+ * @param maxDistance
195
+ */
196
+ isPointCloseToAndBesideLineSection(point, maxDistance) {
197
+ const distance = this.distanceToPointOnInfiniteLine(point);
198
+ return distance <= maxDistance && this.isPointBesideLineSection(point);
199
+ }
200
+ /**
201
+ * Returns true when the point is beside the line **segment**
202
+ * @param point
203
+ */
204
+ isPointBesideLineSection(point) {
205
+ const l2 = (((this.end.x - this.start.x) * (this.end.x - this.start.x)) + ((this.end.y - this.start.y) * (this.end.y - this.start.y)));
206
+ if (l2 == 0)
207
+ return false;
208
+ const r = (((point.x - this.start.x) * (this.end.x - this.start.x)) + ((point.y - this.start.y) * (this.end.y - this.start.y))) / l2;
209
+ return (0 <= r) && (r <= 1);
210
+ }
211
+ /**
212
+ * Returns true when the point is on the **infinite** line.
213
+ * @param point
214
+ */
215
+ isPointOnInfiniteLine(point) {
216
+ return (point.y - this.start.y) * (this.end.x - this.start.x) === (this.end.y - this.start.y) * (point.x - this.start.x);
217
+ }
218
+ /**
219
+ * Returns true if other line is collinear and overlaps or at least touching this line.
220
+ * @param other
221
+ */
222
+ isCollinearWithTouchOrOverlap(other) {
223
+ if (!this.isPointOnInfiniteLine(other.start) || !this.isPointOnInfiniteLine(other.end)) {
224
+ return false;
225
+ }
226
+ return this.isPointOnLineSection(other.start) || this.isPointOnLineSection(other.end) ||
227
+ other.isPointOnLineSection(this.start) || other.isPointOnLineSection(this.end);
228
+ }
229
+ /**
230
+ * Returns true if there is any overlap between this line and the @other line section.
231
+ */
232
+ overlaps(other) {
233
+ if (!this.isCollinearWithTouchOrOverlap(other)) {
234
+ return false;
235
+ }
236
+ if (this.start.equals(other.start) && this.end.equals(other.end)) {
237
+ return true;
238
+ }
239
+ return !this.start.equals(other.end) && !this.end.equals(other.start);
240
+ }
241
+ /**
242
+ * Logical AND of this and the other line section.
243
+ * @param other
244
+ */
245
+ getOverlap(other) {
246
+ if (!this.overlaps(other)) {
247
+ return null;
248
+ }
249
+ if (this.equals(other)) {
250
+ return this.clone();
251
+ }
252
+ const points = [
253
+ [this.start, this.end].filter(thisPoint => other.isPointOnLineSection(thisPoint)),
254
+ [other.start, other.end].filter(otherPoint => this.isPointOnLineSection(otherPoint))
255
+ ].flat();
256
+ if (points.length !== 2) {
257
+ return null;
258
+ }
259
+ const overlap = Line2D.fromPoints(points[0], points[1]);
260
+ if (overlap.direction.manhattanDistanceTo(this.direction) > Number.EPSILON) {
261
+ overlap.flip();
262
+ }
263
+ return overlap;
264
+ }
265
+ /**
266
+ * Joins a copy of @line with the @other line.
267
+ * Other must be parallel to this line.
268
+ * Returns null if there is no overlap
269
+ * Clones the line, does not modify.
270
+ * @param line
271
+ * @param other
272
+ */
273
+ static joinLine(line, other) {
274
+ if (!line.isCollinearWithTouchOrOverlap(other)) {
275
+ return null;
276
+ }
277
+ const p1 = !line.isPointOnLineSection(other.start) ? other.start : line.start;
278
+ const p2 = !line.isPointOnLineSection(other.end) ? other.end : line.end;
279
+ return new Line2D(p1.clone(), p2.clone(), line.index);
280
+ }
281
+ /**
282
+ * Joins provided lines into several joined lines.
283
+ * Lines must be parallel for joining.
284
+ * Clone the lines, does not modify.
285
+ * @param lines
286
+ */
287
+ static joinLines(lines) {
288
+ if (lines.length < 2) {
289
+ return lines.map(x => x.clone());
290
+ }
291
+ const toProcess = lines.slice();
292
+ const result = [];
293
+ while (toProcess.length > 0) {
294
+ const current = toProcess.pop();
295
+ let joined = false;
296
+ for (let i = 0; i < result.length; i++) {
297
+ const other = result[i];
298
+ const joinedLine = Line2D.joinLine(current, other);
299
+ if (joinedLine) {
300
+ result[i] = joinedLine;
301
+ joined = true;
302
+ break;
303
+ }
304
+ }
305
+ if (!joined) {
306
+ result.push(current.clone());
307
+ }
308
+ }
309
+ return result;
310
+ }
311
+ /**
312
+ * Divides the Line3D into a number of segments of the given length.
313
+ * Clone the line, does not modify.
314
+ * @param maxSegmentLength number
315
+ */
316
+ chunk(maxSegmentLength) {
317
+ const source = this.clone();
318
+ const result = [];
319
+ while (source.length > maxSegmentLength) {
320
+ const chunk = source.clone();
321
+ chunk.moveEndPoint(-(chunk.length - maxSegmentLength));
322
+ result.push(chunk);
323
+ source.start = chunk.end.clone();
324
+ }
325
+ result.push(source);
326
+ return result;
327
+ }
328
+ /**
329
+ * Returns the closest point parameter on the **infinite** line to the given point.
330
+ * @param point
331
+ */
332
+ closestPointToPointParameterOnInfiniteLine(point) {
333
+ const startP = new Vec2().subVectors(point, this.start);
334
+ const startEnd = new Vec2().subVectors(this.end, this.start);
335
+ const startEnd2 = startEnd.dot(startEnd);
336
+ const startEnd_startP = startEnd.dot(startP);
337
+ return startEnd_startP / startEnd2;
338
+ }
339
+ /**
340
+ * Returns the closest point on the **infinite** line to the given point.
341
+ * @param point
342
+ */
343
+ closestPointOnInfiniteLine(point) {
344
+ const t = this.closestPointToPointParameterOnInfiniteLine(point);
345
+ return new Vec2().subVectors(this.end, this.start).multiplyScalar(t).add(this.start);
346
+ }
347
+ /**
348
+ * Returns the closest point on the line **section** to the given point.
349
+ * @param point
350
+ */
351
+ closestPointOnLine(point) {
352
+ const closestPoint = this.closestPointOnInfiniteLine(point);
353
+ if (this.isPointOnLineSection(closestPoint)) {
354
+ return closestPoint;
355
+ }
356
+ return closestPoint.distanceTo(this.start) < closestPoint.distanceTo(this.end) ? this.start : this.end;
357
+ }
358
+ /**
359
+ * Returns the distance between the **infinite** line and the point.
360
+ * @param point
361
+ */
362
+ distanceToPointOnInfiniteLine(point) {
363
+ const l2 = (((this.end.x - this.start.x) * (this.end.x - this.start.x)) + ((this.end.y - this.start.y) * (this.end.y - this.start.y)));
364
+ if (l2 == 0)
365
+ return Infinity;
366
+ const s = (((this.start.y - point.y) * (this.end.x - this.start.x)) - ((this.start.x - point.x) * (this.end.y - this.start.y))) / l2;
367
+ return Math.abs(s) * Math.sqrt(l2);
368
+ }
369
+ /**
370
+ * Returns lines that are the result of clipping @source line by the @clips.
371
+ * Clips must be parallel to this line.
372
+ * Clones the line, does not modify this.
373
+ * @param source
374
+ * @param clips
375
+ */
376
+ static clipLines(source, clips) {
377
+ if (!clips || clips.length === 0)
378
+ return [source];
379
+ clips = clips.map(c => {
380
+ const copy = c.clone();
381
+ if (copy.direction.manhattanDistanceTo(source.direction) > Number.EPSILON) {
382
+ copy.flip();
383
+ }
384
+ return copy;
385
+ });
386
+ const free = [];
387
+ const sources = [source];
388
+ while (sources.length > 0) {
389
+ let isFree = true;
390
+ const tested = sources.pop();
391
+ for (const cover of clips) {
392
+ if (tested.overlaps(cover)) {
393
+ isFree = false;
394
+ const subtracted = this.subtractSingle(tested, cover);
395
+ sources.push(...subtracted);
396
+ break;
397
+ }
398
+ }
399
+ if (isFree)
400
+ free.push(tested);
401
+ }
402
+ return this.order(source, free);
403
+ }
404
+ /**
405
+ * Returns the original line section split into two parts, if the line **sections** overlap, otherwise null
406
+ */
407
+ splitAtIntersection(other, tolerance = 0) {
408
+ const intersection = this.intersect(other);
409
+ if (intersection) {
410
+ if (this.isPointCloseToAndBesideLineSection(intersection, tolerance) && other.isPointCloseToAndBesideLineSection(intersection, tolerance)) {
411
+ return [
412
+ Line2D.fromPoints(this.start, intersection),
413
+ Line2D.fromPoints(intersection, this.end)
414
+ ];
415
+ }
416
+ }
417
+ return null;
418
+ }
419
+ /**
420
+ * If lines **sections** overlap, returns the original line section split into two parts, sorted by length
421
+ * Else, if the **infinite** lines intersect, returns a new line extended to the intersection point
422
+ * Otherwise, null if the lines are parallel and do not intersect
423
+ */
424
+ splitAtOrExtendToIntersection(other) {
425
+ const intersection = this.intersect(other);
426
+ if (intersection) {
427
+ return [
428
+ Line2D.fromPoints(this.start, intersection),
429
+ Line2D.fromPoints(intersection, this.end)
430
+ ].filter(x => x.length > Number.EPSILON).sort((a, b) => a.length - b.length);
431
+ }
432
+ return null;
433
+ }
434
+ static order(source, lines) {
435
+ if (source.start.x < source.end.x) {
436
+ lines.sort((a, b) => a.start.x - b.start.x);
437
+ }
438
+ else if (source.start.x > source.end.x) {
439
+ lines.sort((a, b) => b.start.x - a.start.x);
440
+ }
441
+ if (source.start.y < source.end.y) {
442
+ lines.sort((a, b) => a.start.y - b.start.y);
443
+ }
444
+ else if (source.start.y > source.end.y) {
445
+ lines.sort((a, b) => b.start.y - a.start.y);
446
+ }
447
+ return lines;
448
+ }
449
+ static subtractSingle(source, cover) {
450
+ const left = source.clone();
451
+ left.end.copy(cover.start);
452
+ const right = source.clone();
453
+ right.start.copy(cover.end);
454
+ return [left, right].filter(x => x.length > 1 && x.direction.manhattanDistanceTo(source.direction) < Number.EPSILON);
455
+ }
456
+ /**
457
+ * If other line is not contained within this line, the excess is trimmed.
458
+ * Does not create a copy. Provided line is modified.
459
+ * @param lineToTrim
460
+ */
461
+ trimExcess(lineToTrim) {
462
+ if (!this.isCollinearWithTouchOrOverlap(lineToTrim)) {
463
+ return;
464
+ }
465
+ if (!this.isPointOnLineSection(lineToTrim.start)) {
466
+ const closest = this.closestPointOnLine(lineToTrim.start);
467
+ lineToTrim.start.copy(closest);
468
+ }
469
+ if (!this.isPointOnLineSection(lineToTrim.end)) {
470
+ const closest = this.closestPointOnLine(lineToTrim.end);
471
+ lineToTrim.end.copy(closest);
472
+ }
473
+ }
474
+ /**
475
+ * If other line is shorter than this, endpoints are moved to extend other
476
+ * Does not create a copy. Provided line is modified.
477
+ * @param lineToExtend
478
+ * @param tolerance
479
+ */
480
+ extendToEnds(lineToExtend, tolerance) {
481
+ if (!this.isCollinearWithTouchOrOverlap(lineToExtend)) {
482
+ console.log("Can't clip, lines that are not collinear with touch or overlap");
483
+ return;
484
+ }
485
+ if (this.start.distanceTo(lineToExtend.start) <= tolerance) {
486
+ lineToExtend.start.copy(this.start);
487
+ }
488
+ if (this.end.distanceTo(lineToExtend.end) <= tolerance) {
489
+ lineToExtend.end.copy(this.end);
490
+ }
491
+ }
492
+ /**
493
+ * Returns the intersection point of two lines. The lines are assumed to be infinite.
494
+ */
495
+ intersect(other) {
496
+ // Check if none of the lines are of length 0
497
+ if ((this.start.x === this.end.x && this.start.y === this.end.y) || (other.start.x === other.end.x && other.start.y === other.end.y)) {
498
+ return null;
499
+ }
500
+ const denominator = ((other.end.y - other.start.y) * (this.end.x - this.start.x) - (other.end.x - other.start.x) * (this.end.y - this.start.y));
501
+ // Lines are parallel
502
+ if (denominator === 0) {
503
+ return null;
504
+ }
505
+ const ua = ((other.end.x - other.start.x) * (this.start.y - other.start.y) - (other.end.y - other.start.y) * (this.start.x - other.start.x)) / denominator;
506
+ // Return an object with the x and y coordinates of the intersection
507
+ const x = this.start.x + ua * (this.end.x - this.start.x);
508
+ const y = this.start.y + ua * (this.end.y - this.start.y);
509
+ return new Vec2(x, y);
510
+ }
511
+ /**
512
+ * Check that the infinite lines intersect and that they are in the specified angle to each other
513
+ * @param other Line
514
+ * @param expectedAngleInRads number
515
+ */
516
+ hasIntersectionWithAngle(other, expectedAngleInRads) {
517
+ const angle = this.direction.angle();
518
+ const otherAngle = other.direction.angle();
519
+ const actualAngle = Math.abs(angle - otherAngle);
520
+ if (Math.abs(actualAngle - expectedAngleInRads) < Number.EPSILON) {
521
+ const intersection = this.intersect(other);
522
+ if (intersection && this.isPointOnLineSection(intersection) && other.isPointOnLineSection(intersection)) {
523
+ return intersection;
524
+ }
525
+ }
526
+ return null;
527
+ }
528
+ /**
529
+ * Deep clone of this line
530
+ */
531
+ clone() {
532
+ return new Line2D(this.start.clone(), this.end.clone(), this.index);
533
+ }
534
+ toString() {
535
+ return `Line(${this.start.x}, ${this.start.y}, ${this.end.x}, ${this.end.y})`;
536
+ }
537
+ equals(other) {
538
+ return !!other && this.start.equals(other.start) && this.end.equals(other.end);
539
+ }
540
+ }