@immugio/three-math-extensions 0.2.5 → 0.2.7
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 +20 -16
- package/README.md +82 -2
- package/cjs/Line2D.js +33 -31
- package/cjs/MathConstants.js +4 -0
- package/cjs/Vec2.js +21 -0
- package/cjs/index.js +3 -1
- package/cjs/normalizeAngleDegrees.js +4 -0
- package/cjs/normalizeAngleRadians.js +7 -3
- package/docs/classes/BoundingBox.md +7 -7
- package/docs/classes/Line2D.md +80 -81
- package/docs/classes/Line3D.md +35 -35
- package/docs/classes/Polygon.md +38 -16
- package/docs/classes/Rectangle.md +18 -18
- package/docs/classes/Size2.md +3 -3
- package/docs/classes/Vec2.md +23 -6
- package/docs/classes/Vec3.md +12 -12
- package/docs/interfaces/Point2.md +30 -0
- package/docs/interfaces/Point3.md +41 -0
- package/docs/modules.md +68 -0
- package/esm/Line2D.js +33 -31
- package/esm/MathConstants.js +1 -0
- package/esm/Vec2.js +21 -0
- package/esm/index.js +1 -0
- package/esm/normalizeAngleDegrees.js +4 -0
- package/esm/normalizeAngleRadians.js +7 -3
- package/package.json +1 -1
- package/src/Line2D.ts +39 -34
- package/src/MathConstants.ts +1 -0
- package/src/Vec2.ts +26 -0
- package/src/index.ts +4 -1
- package/src/normalizeAngleDegrees.ts +4 -0
- package/src/normalizeAngleRadians.ts +11 -5
- package/types/Line2D.d.ts +10 -14
- package/types/MathConstants.d.ts +1 -0
- package/types/Vec2.d.ts +11 -0
- package/types/index.d.ts +3 -0
- package/types/normalizeAngleDegrees.d.ts +4 -0
- package/types/normalizeAngleRadians.d.ts +4 -0
- package/HowToRelease.txt +0 -13
package/docs/classes/Vec2.md
CHANGED
|
@@ -22,6 +22,7 @@ Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
|
|
|
22
22
|
- [isNear](Vec2.md#isnear)
|
|
23
23
|
- [moveTowards](Vec2.md#movetowards)
|
|
24
24
|
- [roundIfCloseToInteger](Vec2.md#roundifclosetointeger)
|
|
25
|
+
- [signedAngle](Vec2.md#signedangle)
|
|
25
26
|
- [fromPoint](Vec2.md#frompoint)
|
|
26
27
|
|
|
27
28
|
## Constructors
|
|
@@ -67,7 +68,7 @@ A new Vec3 instance.
|
|
|
67
68
|
|
|
68
69
|
#### Defined in
|
|
69
70
|
|
|
70
|
-
[src/Vec2.ts:
|
|
71
|
+
[src/Vec2.ts:52](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec2.ts#L52)
|
|
71
72
|
|
|
72
73
|
___
|
|
73
74
|
|
|
@@ -91,7 +92,7 @@ maxDistance is the maximum distance between the two vectors within which they ar
|
|
|
91
92
|
|
|
92
93
|
#### Defined in
|
|
93
94
|
|
|
94
|
-
[src/Vec2.ts:
|
|
95
|
+
[src/Vec2.ts:60](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec2.ts#L60)
|
|
95
96
|
|
|
96
97
|
___
|
|
97
98
|
|
|
@@ -116,7 +117,7 @@ This Vec2 instance.
|
|
|
116
117
|
|
|
117
118
|
#### Defined in
|
|
118
119
|
|
|
119
|
-
[src/Vec2.ts:
|
|
120
|
+
[src/Vec2.ts:26](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec2.ts#L26)
|
|
120
121
|
|
|
121
122
|
___
|
|
122
123
|
|
|
@@ -140,7 +141,23 @@ This Vec2 instance.
|
|
|
140
141
|
|
|
141
142
|
#### Defined in
|
|
142
143
|
|
|
143
|
-
[src/Vec2.ts:
|
|
144
|
+
[src/Vec2.ts:37](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec2.ts#L37)
|
|
145
|
+
|
|
146
|
+
___
|
|
147
|
+
|
|
148
|
+
### signedAngle
|
|
149
|
+
|
|
150
|
+
▸ **signedAngle**(): `number`
|
|
151
|
+
|
|
152
|
+
Returns the angle between this vector and positive x-axis, the return value is between 0 and 2PI
|
|
153
|
+
|
|
154
|
+
#### Returns
|
|
155
|
+
|
|
156
|
+
`number`
|
|
157
|
+
|
|
158
|
+
#### Defined in
|
|
159
|
+
|
|
160
|
+
[src/Vec2.ts:71](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec2.ts#L71)
|
|
144
161
|
|
|
145
162
|
___
|
|
146
163
|
|
|
@@ -154,7 +171,7 @@ Creates a new Vec2 instance from an {x, y} object.
|
|
|
154
171
|
|
|
155
172
|
| Name | Type | Description |
|
|
156
173
|
| :------ | :------ | :------ |
|
|
157
|
-
| `point` | `Point2` | The {x, y} instance. |
|
|
174
|
+
| `point` | [`Point2`](../interfaces/Point2.md) | The {x, y} instance. |
|
|
158
175
|
|
|
159
176
|
#### Returns
|
|
160
177
|
|
|
@@ -164,4 +181,4 @@ A new Vec2 instance.
|
|
|
164
181
|
|
|
165
182
|
#### Defined in
|
|
166
183
|
|
|
167
|
-
[src/Vec2.ts:
|
|
184
|
+
[src/Vec2.ts:16](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec2.ts#L16)
|
package/docs/classes/Vec3.md
CHANGED
|
@@ -72,7 +72,7 @@ Adds x amount to this Vec3 instance and return this
|
|
|
72
72
|
|
|
73
73
|
#### Defined in
|
|
74
74
|
|
|
75
|
-
[src/Vec3.ts:65](https://github.com/Immugio/three-math-extensions/blob/
|
|
75
|
+
[src/Vec3.ts:65](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L65)
|
|
76
76
|
|
|
77
77
|
___
|
|
78
78
|
|
|
@@ -94,7 +94,7 @@ Adds y amount to this Vec3 instance and return this
|
|
|
94
94
|
|
|
95
95
|
#### Defined in
|
|
96
96
|
|
|
97
|
-
[src/Vec3.ts:56](https://github.com/Immugio/three-math-extensions/blob/
|
|
97
|
+
[src/Vec3.ts:56](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L56)
|
|
98
98
|
|
|
99
99
|
___
|
|
100
100
|
|
|
@@ -112,7 +112,7 @@ Vector3.clone
|
|
|
112
112
|
|
|
113
113
|
#### Defined in
|
|
114
114
|
|
|
115
|
-
[src/Vec3.ts:114](https://github.com/Immugio/three-math-extensions/blob/
|
|
115
|
+
[src/Vec3.ts:114](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L114)
|
|
116
116
|
|
|
117
117
|
___
|
|
118
118
|
|
|
@@ -134,7 +134,7 @@ Returns a clone of the point closest to this from the given points.
|
|
|
134
134
|
|
|
135
135
|
#### Defined in
|
|
136
136
|
|
|
137
|
-
[src/Vec3.ts:74](https://github.com/Immugio/three-math-extensions/blob/
|
|
137
|
+
[src/Vec3.ts:74](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L74)
|
|
138
138
|
|
|
139
139
|
___
|
|
140
140
|
|
|
@@ -156,7 +156,7 @@ Get distance to another vector while ignoring the y-axis.
|
|
|
156
156
|
|
|
157
157
|
#### Defined in
|
|
158
158
|
|
|
159
|
-
[src/Vec3.ts:98](https://github.com/Immugio/three-math-extensions/blob/
|
|
159
|
+
[src/Vec3.ts:98](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L98)
|
|
160
160
|
|
|
161
161
|
___
|
|
162
162
|
|
|
@@ -180,7 +180,7 @@ maxDistance is the maximum distance between the two vectors within which they ar
|
|
|
180
180
|
|
|
181
181
|
#### Defined in
|
|
182
182
|
|
|
183
|
-
[src/Vec3.ts:106](https://github.com/Immugio/three-math-extensions/blob/
|
|
183
|
+
[src/Vec3.ts:106](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L106)
|
|
184
184
|
|
|
185
185
|
___
|
|
186
186
|
|
|
@@ -204,7 +204,7 @@ This Vec3 instance.
|
|
|
204
204
|
|
|
205
205
|
#### Defined in
|
|
206
206
|
|
|
207
|
-
[src/Vec3.ts:43](https://github.com/Immugio/three-math-extensions/blob/
|
|
207
|
+
[src/Vec3.ts:43](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L43)
|
|
208
208
|
|
|
209
209
|
___
|
|
210
210
|
|
|
@@ -229,7 +229,7 @@ This Vec3 instance.
|
|
|
229
229
|
|
|
230
230
|
#### Defined in
|
|
231
231
|
|
|
232
|
-
[src/Vec3.ts:27](https://github.com/Immugio/three-math-extensions/blob/
|
|
232
|
+
[src/Vec3.ts:27](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L27)
|
|
233
233
|
|
|
234
234
|
___
|
|
235
235
|
|
|
@@ -245,7 +245,7 @@ Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ig
|
|
|
245
245
|
|
|
246
246
|
#### Defined in
|
|
247
247
|
|
|
248
|
-
[src/Vec3.ts:90](https://github.com/Immugio/three-math-extensions/blob/
|
|
248
|
+
[src/Vec3.ts:90](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L90)
|
|
249
249
|
|
|
250
250
|
___
|
|
251
251
|
|
|
@@ -261,7 +261,7 @@ Returns a clone of this Vec3 instance with y and z swapped.
|
|
|
261
261
|
|
|
262
262
|
#### Defined in
|
|
263
263
|
|
|
264
|
-
[src/Vec3.ts:83](https://github.com/Immugio/three-math-extensions/blob/
|
|
264
|
+
[src/Vec3.ts:83](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L83)
|
|
265
265
|
|
|
266
266
|
___
|
|
267
267
|
|
|
@@ -275,7 +275,7 @@ Creates a new Vec3 instance from an {x, y, z} object.
|
|
|
275
275
|
|
|
276
276
|
| Name | Type | Description |
|
|
277
277
|
| :------ | :------ | :------ |
|
|
278
|
-
| `point` | `Point3` | The {x, y, z} instance. |
|
|
278
|
+
| `point` | [`Point3`](../interfaces/Point3.md) | The {x, y, z} instance. |
|
|
279
279
|
|
|
280
280
|
#### Returns
|
|
281
281
|
|
|
@@ -285,4 +285,4 @@ A new Vec3 instance.
|
|
|
285
285
|
|
|
286
286
|
#### Defined in
|
|
287
287
|
|
|
288
|
-
[src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/
|
|
288
|
+
[src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Vec3.ts#L17)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Point2
|
|
2
|
+
|
|
3
|
+
# Interface: Point2
|
|
4
|
+
|
|
5
|
+
## Table of contents
|
|
6
|
+
|
|
7
|
+
### Properties
|
|
8
|
+
|
|
9
|
+
- [x](Point2.md#x)
|
|
10
|
+
- [y](Point2.md#y)
|
|
11
|
+
|
|
12
|
+
## Properties
|
|
13
|
+
|
|
14
|
+
### x
|
|
15
|
+
|
|
16
|
+
• **x**: `number`
|
|
17
|
+
|
|
18
|
+
#### Defined in
|
|
19
|
+
|
|
20
|
+
[src/Point2.ts:2](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point2.ts#L2)
|
|
21
|
+
|
|
22
|
+
___
|
|
23
|
+
|
|
24
|
+
### y
|
|
25
|
+
|
|
26
|
+
• **y**: `number`
|
|
27
|
+
|
|
28
|
+
#### Defined in
|
|
29
|
+
|
|
30
|
+
[src/Point2.ts:3](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point2.ts#L3)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Point3
|
|
2
|
+
|
|
3
|
+
# Interface: Point3
|
|
4
|
+
|
|
5
|
+
## Table of contents
|
|
6
|
+
|
|
7
|
+
### Properties
|
|
8
|
+
|
|
9
|
+
- [x](Point3.md#x)
|
|
10
|
+
- [y](Point3.md#y)
|
|
11
|
+
- [z](Point3.md#z)
|
|
12
|
+
|
|
13
|
+
## Properties
|
|
14
|
+
|
|
15
|
+
### x
|
|
16
|
+
|
|
17
|
+
• **x**: `number`
|
|
18
|
+
|
|
19
|
+
#### Defined in
|
|
20
|
+
|
|
21
|
+
[src/Point3.ts:2](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point3.ts#L2)
|
|
22
|
+
|
|
23
|
+
___
|
|
24
|
+
|
|
25
|
+
### y
|
|
26
|
+
|
|
27
|
+
• **y**: `number`
|
|
28
|
+
|
|
29
|
+
#### Defined in
|
|
30
|
+
|
|
31
|
+
[src/Point3.ts:3](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point3.ts#L3)
|
|
32
|
+
|
|
33
|
+
___
|
|
34
|
+
|
|
35
|
+
### z
|
|
36
|
+
|
|
37
|
+
• **z**: `number`
|
|
38
|
+
|
|
39
|
+
#### Defined in
|
|
40
|
+
|
|
41
|
+
[src/Point3.ts:4](https://github.com/Immugio/three-math-extensions/blob/151f214/src/Point3.ts#L4)
|
package/docs/modules.md
CHANGED
|
@@ -14,3 +14,71 @@
|
|
|
14
14
|
- [Size2](classes/Size2.md)
|
|
15
15
|
- [Vec2](classes/Vec2.md)
|
|
16
16
|
- [Vec3](classes/Vec3.md)
|
|
17
|
+
|
|
18
|
+
### Interfaces
|
|
19
|
+
|
|
20
|
+
- [Point2](interfaces/Point2.md)
|
|
21
|
+
- [Point3](interfaces/Point3.md)
|
|
22
|
+
|
|
23
|
+
### Variables
|
|
24
|
+
|
|
25
|
+
- [TwoPI](modules.md#twopi)
|
|
26
|
+
|
|
27
|
+
### Functions
|
|
28
|
+
|
|
29
|
+
- [normalizeAngleDegrees](modules.md#normalizeangledegrees)
|
|
30
|
+
- [normalizeAngleRadians](modules.md#normalizeangleradians)
|
|
31
|
+
|
|
32
|
+
## Variables
|
|
33
|
+
|
|
34
|
+
### TwoPI
|
|
35
|
+
|
|
36
|
+
• `Const` **TwoPI**: `number`
|
|
37
|
+
|
|
38
|
+
#### Defined in
|
|
39
|
+
|
|
40
|
+
[src/MathConstants.ts:1](https://github.com/Immugio/three-math-extensions/blob/151f214/src/MathConstants.ts#L1)
|
|
41
|
+
|
|
42
|
+
## Functions
|
|
43
|
+
|
|
44
|
+
### normalizeAngleDegrees
|
|
45
|
+
|
|
46
|
+
▸ **normalizeAngleDegrees**(`angle`): `number`
|
|
47
|
+
|
|
48
|
+
Normalizes an angle in degrees to the range [0, 360].
|
|
49
|
+
|
|
50
|
+
#### Parameters
|
|
51
|
+
|
|
52
|
+
| Name | Type | Description |
|
|
53
|
+
| :------ | :------ | :------ |
|
|
54
|
+
| `angle` | `number` | in degrees |
|
|
55
|
+
|
|
56
|
+
#### Returns
|
|
57
|
+
|
|
58
|
+
`number`
|
|
59
|
+
|
|
60
|
+
#### Defined in
|
|
61
|
+
|
|
62
|
+
[src/normalizeAngleDegrees.ts:5](https://github.com/Immugio/three-math-extensions/blob/151f214/src/normalizeAngleDegrees.ts#L5)
|
|
63
|
+
|
|
64
|
+
___
|
|
65
|
+
|
|
66
|
+
### normalizeAngleRadians
|
|
67
|
+
|
|
68
|
+
▸ **normalizeAngleRadians**(`angle`): `number`
|
|
69
|
+
|
|
70
|
+
Normalize an angle in radians to the range of 0 to 2π.
|
|
71
|
+
|
|
72
|
+
#### Parameters
|
|
73
|
+
|
|
74
|
+
| Name | Type | Description |
|
|
75
|
+
| :------ | :------ | :------ |
|
|
76
|
+
| `angle` | `number` | in radians |
|
|
77
|
+
|
|
78
|
+
#### Returns
|
|
79
|
+
|
|
80
|
+
`number`
|
|
81
|
+
|
|
82
|
+
#### Defined in
|
|
83
|
+
|
|
84
|
+
[src/normalizeAngleRadians.ts:7](https://github.com/Immugio/three-math-extensions/blob/151f214/src/normalizeAngleRadians.ts#L7)
|
package/esm/Line2D.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Vector2 } from "three";
|
|
2
2
|
import { Vec2 } from "./Vec2";
|
|
3
|
+
import { MathUtils } from "three";
|
|
4
|
+
import { TwoPI } from "./MathConstants";
|
|
5
|
+
const _startP = /*@__PURE__*/ new Vec2();
|
|
6
|
+
const _startEnd = /*@__PURE__*/ new Vec2();
|
|
3
7
|
export class Line2D {
|
|
4
8
|
start;
|
|
5
9
|
end;
|
|
@@ -326,34 +330,28 @@ export class Line2D {
|
|
|
326
330
|
return result;
|
|
327
331
|
}
|
|
328
332
|
/**
|
|
329
|
-
* Returns the closest point
|
|
333
|
+
* Returns the closest point on the line to the given point.
|
|
330
334
|
* @param point
|
|
335
|
+
* @param clampToLine boolean (optional)
|
|
336
|
+
* @param target Vec2 (optional)
|
|
331
337
|
*/
|
|
332
|
-
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
const startEnd2 = startEnd.dot(startEnd);
|
|
336
|
-
const startEnd_startP = startEnd.dot(startP);
|
|
337
|
-
return startEnd_startP / startEnd2;
|
|
338
|
+
closestPointToPoint(point, clampToLine, target) {
|
|
339
|
+
const t = this.closestPointToPointParameter(point, clampToLine);
|
|
340
|
+
return this.delta(target || new Vec2()).multiplyScalar(t).add(this.start);
|
|
338
341
|
}
|
|
339
|
-
|
|
340
|
-
|
|
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);
|
|
342
|
+
delta(target) {
|
|
343
|
+
return target.subVectors(this.end, this.start);
|
|
346
344
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (
|
|
354
|
-
|
|
345
|
+
closestPointToPointParameter(point, clampToLine) {
|
|
346
|
+
_startP.subVectors(point, this.start);
|
|
347
|
+
_startEnd.subVectors(this.end, this.start);
|
|
348
|
+
const startEnd2 = _startEnd.dot(_startEnd);
|
|
349
|
+
const startEnd_startP = _startEnd.dot(_startP);
|
|
350
|
+
let t = startEnd_startP / startEnd2;
|
|
351
|
+
if (clampToLine) {
|
|
352
|
+
t = MathUtils.clamp(t, 0, 1);
|
|
355
353
|
}
|
|
356
|
-
return
|
|
354
|
+
return t;
|
|
357
355
|
}
|
|
358
356
|
/**
|
|
359
357
|
* Returns the distance between the **infinite** line and the point.
|
|
@@ -463,11 +461,11 @@ export class Line2D {
|
|
|
463
461
|
return;
|
|
464
462
|
}
|
|
465
463
|
if (!this.isPointOnLineSection(lineToTrim.start)) {
|
|
466
|
-
const closest = this.
|
|
464
|
+
const closest = this.closestPointToPoint(lineToTrim.start, true);
|
|
467
465
|
lineToTrim.start.copy(closest);
|
|
468
466
|
}
|
|
469
467
|
if (!this.isPointOnLineSection(lineToTrim.end)) {
|
|
470
|
-
const closest = this.
|
|
468
|
+
const closest = this.closestPointToPoint(lineToTrim.end, true);
|
|
471
469
|
lineToTrim.end.copy(closest);
|
|
472
470
|
}
|
|
473
471
|
}
|
|
@@ -531,17 +529,21 @@ export class Line2D {
|
|
|
531
529
|
return new Vec2(x, y);
|
|
532
530
|
}
|
|
533
531
|
/**
|
|
534
|
-
* Check that the
|
|
532
|
+
* Check that the line section intersect and that they are in the specified angle to each other
|
|
535
533
|
* @param other Line
|
|
536
534
|
* @param expectedAngleInRads number
|
|
535
|
+
* @param angleTolerance number
|
|
536
|
+
* @param distanceTolerance number
|
|
537
537
|
*/
|
|
538
|
-
hasIntersectionWithAngle(other, expectedAngleInRads) {
|
|
539
|
-
const angle = this.direction.angle();
|
|
540
|
-
const otherAngle = other.direction.angle();
|
|
538
|
+
hasIntersectionWithAngle(other, expectedAngleInRads, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON) {
|
|
539
|
+
const angle = this.direction.angle() % TwoPI;
|
|
540
|
+
const otherAngle = other.direction.angle() % TwoPI;
|
|
541
541
|
const actualAngle = Math.abs(angle - otherAngle);
|
|
542
|
-
if (Math.abs(actualAngle - expectedAngleInRads)
|
|
542
|
+
if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
|
|
543
543
|
const intersection = this.intersect(other);
|
|
544
|
-
if (intersection &&
|
|
544
|
+
if (intersection &&
|
|
545
|
+
this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
|
|
546
|
+
other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance) {
|
|
545
547
|
return intersection;
|
|
546
548
|
}
|
|
547
549
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TwoPI = 2 * Math.PI;
|
package/esm/Vec2.js
CHANGED
|
@@ -13,6 +13,13 @@ export class Vec2 extends Vector2 {
|
|
|
13
13
|
static fromPoint(point) {
|
|
14
14
|
return new Vec2(point?.x, point?.y);
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new Vec2[] array from arguments of {x, y} objects.
|
|
18
|
+
* @param points - The ...{x, y} instances.
|
|
19
|
+
*/
|
|
20
|
+
static fromPoints(...points) {
|
|
21
|
+
return points?.map(p => Vec2.fromPoint(p)) ?? [];
|
|
22
|
+
}
|
|
16
23
|
/**
|
|
17
24
|
* Moves this Vec2 instance towards the target Vec2 by the given amount.
|
|
18
25
|
* @param target - The target Vec2.
|
|
@@ -63,4 +70,18 @@ export class Vec2 extends Vector2 {
|
|
|
63
70
|
const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
|
|
64
71
|
return normalizeAngleRadians(signed_angle);
|
|
65
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* check if the angle between the two vectors is close enough to 0 or 180 degrees (same or opposite direction) within the given tolerance
|
|
75
|
+
* @param other Vector2
|
|
76
|
+
* @param toleranceRadians number angle tolerance in radians
|
|
77
|
+
*/
|
|
78
|
+
parallelTo(other, toleranceRadians = 0) {
|
|
79
|
+
const v1 = this.clone().normalize(); // Normalize both vectors to remove magnitude influence
|
|
80
|
+
const v2 = other.clone().normalize();
|
|
81
|
+
const dotProduct = v1.dot(v2); // Calculate the dot product to find the cosine of the angle between the vectors
|
|
82
|
+
// Calculate the angle in radians
|
|
83
|
+
const angle = Math.acos(dotProduct);
|
|
84
|
+
// Check if the angle is within the tolerance of 0 or 180 degrees (Math.PI)
|
|
85
|
+
return Math.abs(angle) <= toleranceRadians || Math.abs(angle - Math.PI) <= toleranceRadians;
|
|
86
|
+
}
|
|
66
87
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import { TwoPI } from "./MathConstants";
|
|
2
|
+
/**
|
|
3
|
+
* Normalize an angle in radians to the range of 0 to 2π.
|
|
4
|
+
* @param angle in radians
|
|
5
|
+
*/
|
|
1
6
|
export function normalizeAngleRadians(angle) {
|
|
2
|
-
|
|
3
|
-
angle = angle % twoPi; // Use modulus to get the angle within the range of 0 to 2π
|
|
7
|
+
angle = angle % TwoPI; // Use modulus to get the angle within the range of 0 to 2π
|
|
4
8
|
if (angle < 0) { // Add 2π if the angle is negative
|
|
5
|
-
angle = angle +
|
|
9
|
+
angle = angle + TwoPI;
|
|
6
10
|
}
|
|
7
11
|
return angle;
|
|
8
12
|
}
|
package/package.json
CHANGED
package/src/Line2D.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Point2 } from "./Point2";
|
|
2
2
|
import { Vector2 } from "three";
|
|
3
3
|
import { Vec2 } from "./Vec2";
|
|
4
|
+
import { MathUtils } from "three";
|
|
5
|
+
import { TwoPI } from "./MathConstants";
|
|
6
|
+
|
|
7
|
+
const _startP = /*@__PURE__*/ new Vec2();
|
|
8
|
+
const _startEnd = /*@__PURE__*/ new Vec2();
|
|
4
9
|
|
|
5
10
|
export class Line2D {
|
|
6
11
|
|
|
@@ -381,39 +386,34 @@ export class Line2D {
|
|
|
381
386
|
}
|
|
382
387
|
|
|
383
388
|
/**
|
|
384
|
-
* Returns the closest point
|
|
389
|
+
* Returns the closest point on the line to the given point.
|
|
385
390
|
* @param point
|
|
391
|
+
* @param clampToLine boolean (optional)
|
|
392
|
+
* @param target Vec2 (optional)
|
|
386
393
|
*/
|
|
387
|
-
public
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const startEnd2 = startEnd.dot(startEnd);
|
|
392
|
-
const startEnd_startP = startEnd.dot(startP);
|
|
393
|
-
|
|
394
|
-
return startEnd_startP / startEnd2;
|
|
394
|
+
public closestPointToPoint(point: Vector2, clampToLine?: boolean, target?: Vec2): Vec2 {
|
|
395
|
+
const t = this.closestPointToPointParameter(point, clampToLine);
|
|
396
|
+
return this.delta(target || new Vec2()).multiplyScalar(t).add(this.start);
|
|
395
397
|
}
|
|
396
398
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
* @param point
|
|
400
|
-
*/
|
|
401
|
-
public closestPointOnInfiniteLine(point: Vector2): Vec2 {
|
|
402
|
-
const t = this.closestPointToPointParameterOnInfiniteLine(point);
|
|
403
|
-
return new Vec2().subVectors(this.end, this.start).multiplyScalar(t).add(this.start);
|
|
399
|
+
public delta(target: Vec2): Vec2 {
|
|
400
|
+
return target.subVectors(this.end, this.start);
|
|
404
401
|
}
|
|
405
402
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
403
|
+
public closestPointToPointParameter(point, clampToLine): number {
|
|
404
|
+
_startP.subVectors(point, this.start);
|
|
405
|
+
_startEnd.subVectors(this.end, this.start);
|
|
406
|
+
|
|
407
|
+
const startEnd2 = _startEnd.dot(_startEnd);
|
|
408
|
+
const startEnd_startP = _startEnd.dot(_startP);
|
|
409
|
+
|
|
410
|
+
let t = startEnd_startP / startEnd2;
|
|
411
|
+
|
|
412
|
+
if (clampToLine) {
|
|
413
|
+
t = MathUtils.clamp(t, 0, 1);
|
|
414
414
|
}
|
|
415
415
|
|
|
416
|
-
return
|
|
416
|
+
return t;
|
|
417
417
|
}
|
|
418
418
|
|
|
419
419
|
/**
|
|
@@ -541,12 +541,12 @@ export class Line2D {
|
|
|
541
541
|
}
|
|
542
542
|
|
|
543
543
|
if (!this.isPointOnLineSection(lineToTrim.start)) {
|
|
544
|
-
const closest = this.
|
|
544
|
+
const closest = this.closestPointToPoint(lineToTrim.start, true);
|
|
545
545
|
lineToTrim.start.copy(closest);
|
|
546
546
|
}
|
|
547
547
|
|
|
548
548
|
if (!this.isPointOnLineSection(lineToTrim.end)) {
|
|
549
|
-
const closest = this.
|
|
549
|
+
const closest = this.closestPointToPoint(lineToTrim.end, true);
|
|
550
550
|
lineToTrim.end.copy(closest);
|
|
551
551
|
}
|
|
552
552
|
}
|
|
@@ -624,19 +624,24 @@ export class Line2D {
|
|
|
624
624
|
}
|
|
625
625
|
|
|
626
626
|
/**
|
|
627
|
-
* Check that the
|
|
627
|
+
* Check that the line section intersect and that they are in the specified angle to each other
|
|
628
628
|
* @param other Line
|
|
629
629
|
* @param expectedAngleInRads number
|
|
630
|
+
* @param angleTolerance number
|
|
631
|
+
* @param distanceTolerance number
|
|
630
632
|
*/
|
|
631
|
-
public hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number): Vec2 {
|
|
632
|
-
const angle = this.direction.angle();
|
|
633
|
-
const otherAngle = other.direction.angle();
|
|
633
|
+
public hasIntersectionWithAngle(other: Line2D, expectedAngleInRads: number, angleTolerance = Number.EPSILON, distanceTolerance = Number.EPSILON): Vec2 {
|
|
634
|
+
const angle = this.direction.angle() % TwoPI;
|
|
635
|
+
const otherAngle = other.direction.angle() % TwoPI;
|
|
634
636
|
const actualAngle = Math.abs(angle - otherAngle);
|
|
635
637
|
|
|
636
|
-
if (Math.abs(actualAngle - expectedAngleInRads)
|
|
638
|
+
if (Math.abs(actualAngle - expectedAngleInRads) <= angleTolerance) {
|
|
637
639
|
const intersection = this.intersect(other);
|
|
638
|
-
if (
|
|
639
|
-
|
|
640
|
+
if (
|
|
641
|
+
intersection &&
|
|
642
|
+
this.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance &&
|
|
643
|
+
other.closestPointToPoint(intersection, true).distanceTo(intersection) <= distanceTolerance
|
|
644
|
+
) {
|
|
640
645
|
return intersection;
|
|
641
646
|
}
|
|
642
647
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TwoPI = 2 * Math.PI;
|
package/src/Vec2.ts
CHANGED
|
@@ -17,6 +17,14 @@ export class Vec2 extends Vector2 {
|
|
|
17
17
|
return new Vec2(point?.x, point?.y);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Vec2[] array from arguments of {x, y} objects.
|
|
22
|
+
* @param points - The ...{x, y} instances.
|
|
23
|
+
*/
|
|
24
|
+
public static fromPoints(...points: Point2[]): Vec2[] {
|
|
25
|
+
return points?.map(p => Vec2.fromPoint(p)) ?? [];
|
|
26
|
+
}
|
|
27
|
+
|
|
20
28
|
/**
|
|
21
29
|
* Moves this Vec2 instance towards the target Vec2 by the given amount.
|
|
22
30
|
* @param target - The target Vec2.
|
|
@@ -72,4 +80,22 @@ export class Vec2 extends Vector2 {
|
|
|
72
80
|
const signed_angle = Math.atan2(this.y, this.x) - Math.atan2(0, 1);
|
|
73
81
|
return normalizeAngleRadians(signed_angle);
|
|
74
82
|
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* check if the angle between the two vectors is close enough to 0 or 180 degrees (same or opposite direction) within the given tolerance
|
|
86
|
+
* @param other Vector2
|
|
87
|
+
* @param toleranceRadians number angle tolerance in radians
|
|
88
|
+
*/
|
|
89
|
+
public parallelTo(other: Vector2, toleranceRadians: number = 0): boolean {
|
|
90
|
+
const v1 = this.clone().normalize(); // Normalize both vectors to remove magnitude influence
|
|
91
|
+
const v2 = other.clone().normalize();
|
|
92
|
+
|
|
93
|
+
const dotProduct = v1.dot(v2); // Calculate the dot product to find the cosine of the angle between the vectors
|
|
94
|
+
|
|
95
|
+
// Calculate the angle in radians
|
|
96
|
+
const angle = Math.acos(dotProduct);
|
|
97
|
+
|
|
98
|
+
// Check if the angle is within the tolerance of 0 or 180 degrees (Math.PI)
|
|
99
|
+
return Math.abs(angle) <= toleranceRadians || Math.abs(angle - Math.PI) <= toleranceRadians;
|
|
100
|
+
}
|
|
75
101
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,4 +7,7 @@ export { Polygon } from "./Polygon";
|
|
|
7
7
|
export { BoundingBox } from "./BoundingBox";
|
|
8
8
|
export { Rectangle } from "./Rectangle";
|
|
9
9
|
export { normalizeAngleDegrees } from "./normalizeAngleDegrees";
|
|
10
|
-
export { normalizeAngleRadians } from "./normalizeAngleRadians";
|
|
10
|
+
export { normalizeAngleRadians } from "./normalizeAngleRadians";
|
|
11
|
+
export { TwoPI } from "./MathConstants";
|
|
12
|
+
export { Point2 } from "./Point2";
|
|
13
|
+
export { Point3 } from "./Point3";
|