@immugio/three-math-extensions 0.2.4 → 0.2.6
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 +16 -16
- package/README.md +82 -2
- package/cjs/Line2D.js +22 -25
- package/cjs/MathConstants.js +4 -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 +22 -25
- package/esm/MathConstants.js +1 -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 +26 -27
- package/src/MathConstants.ts +1 -0
- package/src/index.ts +4 -1
- package/src/normalizeAngleDegrees.ts +4 -0
- package/src/normalizeAngleRadians.ts +11 -5
- package/types/Line2D.d.ts +6 -12
- package/types/MathConstants.d.ts +1 -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,8 @@
|
|
|
1
1
|
import { Vector2 } from "three";
|
|
2
2
|
import { Vec2 } from "./Vec2";
|
|
3
|
+
import { MathUtils } from "three";
|
|
4
|
+
const _startP = /*@__PURE__*/ new Vec2();
|
|
5
|
+
const _startEnd = /*@__PURE__*/ new Vec2();
|
|
3
6
|
export class Line2D {
|
|
4
7
|
start;
|
|
5
8
|
end;
|
|
@@ -326,34 +329,28 @@ export class Line2D {
|
|
|
326
329
|
return result;
|
|
327
330
|
}
|
|
328
331
|
/**
|
|
329
|
-
* Returns the closest point
|
|
332
|
+
* Returns the closest point on the line to the given point.
|
|
330
333
|
* @param point
|
|
334
|
+
* @param clampToLine boolean (optional)
|
|
335
|
+
* @param target Vec2 (optional)
|
|
331
336
|
*/
|
|
332
|
-
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
const startEnd2 = startEnd.dot(startEnd);
|
|
336
|
-
const startEnd_startP = startEnd.dot(startP);
|
|
337
|
-
return startEnd_startP / startEnd2;
|
|
337
|
+
closestPointToPoint(point, clampToLine, target) {
|
|
338
|
+
const t = this.closestPointToPointParameter(point, clampToLine);
|
|
339
|
+
return this.delta(target || new Vec2()).multiplyScalar(t).add(this.start);
|
|
338
340
|
}
|
|
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);
|
|
341
|
+
delta(target) {
|
|
342
|
+
return target.subVectors(this.end, this.start);
|
|
346
343
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (
|
|
354
|
-
|
|
344
|
+
closestPointToPointParameter(point, clampToLine) {
|
|
345
|
+
_startP.subVectors(point, this.start);
|
|
346
|
+
_startEnd.subVectors(this.end, this.start);
|
|
347
|
+
const startEnd2 = _startEnd.dot(_startEnd);
|
|
348
|
+
const startEnd_startP = _startEnd.dot(_startP);
|
|
349
|
+
let t = startEnd_startP / startEnd2;
|
|
350
|
+
if (clampToLine) {
|
|
351
|
+
t = MathUtils.clamp(t, 0, 1);
|
|
355
352
|
}
|
|
356
|
-
return
|
|
353
|
+
return t;
|
|
357
354
|
}
|
|
358
355
|
/**
|
|
359
356
|
* Returns the distance between the **infinite** line and the point.
|
|
@@ -463,11 +460,11 @@ export class Line2D {
|
|
|
463
460
|
return;
|
|
464
461
|
}
|
|
465
462
|
if (!this.isPointOnLineSection(lineToTrim.start)) {
|
|
466
|
-
const closest = this.
|
|
463
|
+
const closest = this.closestPointToPoint(lineToTrim.start, true);
|
|
467
464
|
lineToTrim.start.copy(closest);
|
|
468
465
|
}
|
|
469
466
|
if (!this.isPointOnLineSection(lineToTrim.end)) {
|
|
470
|
-
const closest = this.
|
|
467
|
+
const closest = this.closestPointToPoint(lineToTrim.end, true);
|
|
471
468
|
lineToTrim.end.copy(closest);
|
|
472
469
|
}
|
|
473
470
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TwoPI = 2 * Math.PI;
|
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,10 @@
|
|
|
1
1
|
import { Point2 } from "./Point2";
|
|
2
2
|
import { Vector2 } from "three";
|
|
3
3
|
import { Vec2 } from "./Vec2";
|
|
4
|
+
import { MathUtils } from "three";
|
|
5
|
+
|
|
6
|
+
const _startP = /*@__PURE__*/ new Vec2();
|
|
7
|
+
const _startEnd = /*@__PURE__*/ new Vec2();
|
|
4
8
|
|
|
5
9
|
export class Line2D {
|
|
6
10
|
|
|
@@ -381,39 +385,34 @@ export class Line2D {
|
|
|
381
385
|
}
|
|
382
386
|
|
|
383
387
|
/**
|
|
384
|
-
* Returns the closest point
|
|
388
|
+
* Returns the closest point on the line to the given point.
|
|
385
389
|
* @param point
|
|
390
|
+
* @param clampToLine boolean (optional)
|
|
391
|
+
* @param target Vec2 (optional)
|
|
386
392
|
*/
|
|
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;
|
|
393
|
+
public closestPointToPoint(point: Vector2, clampToLine?: boolean, target?: Vec2): Vec2 {
|
|
394
|
+
const t = this.closestPointToPointParameter(point, clampToLine);
|
|
395
|
+
return this.delta(target || new Vec2()).multiplyScalar(t).add(this.start);
|
|
395
396
|
}
|
|
396
397
|
|
|
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);
|
|
398
|
+
public delta(target: Vec2): Vec2 {
|
|
399
|
+
return target.subVectors(this.end, this.start);
|
|
404
400
|
}
|
|
405
401
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const
|
|
412
|
-
|
|
413
|
-
|
|
402
|
+
public closestPointToPointParameter(point, clampToLine): number {
|
|
403
|
+
_startP.subVectors(point, this.start);
|
|
404
|
+
_startEnd.subVectors(this.end, this.start);
|
|
405
|
+
|
|
406
|
+
const startEnd2 = _startEnd.dot(_startEnd);
|
|
407
|
+
const startEnd_startP = _startEnd.dot(_startP);
|
|
408
|
+
|
|
409
|
+
let t = startEnd_startP / startEnd2;
|
|
410
|
+
|
|
411
|
+
if (clampToLine) {
|
|
412
|
+
t = MathUtils.clamp(t, 0, 1);
|
|
414
413
|
}
|
|
415
414
|
|
|
416
|
-
return
|
|
415
|
+
return t;
|
|
417
416
|
}
|
|
418
417
|
|
|
419
418
|
/**
|
|
@@ -541,12 +540,12 @@ export class Line2D {
|
|
|
541
540
|
}
|
|
542
541
|
|
|
543
542
|
if (!this.isPointOnLineSection(lineToTrim.start)) {
|
|
544
|
-
const closest = this.
|
|
543
|
+
const closest = this.closestPointToPoint(lineToTrim.start, true);
|
|
545
544
|
lineToTrim.start.copy(closest);
|
|
546
545
|
}
|
|
547
546
|
|
|
548
547
|
if (!this.isPointOnLineSection(lineToTrim.end)) {
|
|
549
|
-
const closest = this.
|
|
548
|
+
const closest = this.closestPointToPoint(lineToTrim.end, true);
|
|
550
549
|
lineToTrim.end.copy(closest);
|
|
551
550
|
}
|
|
552
551
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const TwoPI = 2 * Math.PI;
|
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";
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
import { TwoPI } from "./MathConstants";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalize an angle in radians to the range of 0 to 2π.
|
|
5
|
+
* @param angle in radians
|
|
6
|
+
*/
|
|
1
7
|
export function normalizeAngleRadians(angle: number): number {
|
|
2
|
-
|
|
3
|
-
|
|
8
|
+
angle = angle % TwoPI; // Use modulus to get the angle within the range of 0 to 2π
|
|
9
|
+
|
|
4
10
|
if (angle < 0) { // Add 2π if the angle is negative
|
|
5
|
-
angle = angle +
|
|
11
|
+
angle = angle + TwoPI;
|
|
6
12
|
}
|
|
7
|
-
return angle;
|
|
8
|
-
}
|
|
9
13
|
|
|
14
|
+
return angle;
|
|
15
|
+
}
|
package/types/Line2D.d.ts
CHANGED
|
@@ -144,20 +144,14 @@ export declare class Line2D {
|
|
|
144
144
|
*/
|
|
145
145
|
chunk(maxSegmentLength: number): Line2D[];
|
|
146
146
|
/**
|
|
147
|
-
* Returns the closest point
|
|
147
|
+
* Returns the closest point on the line to the given point.
|
|
148
148
|
* @param point
|
|
149
|
+
* @param clampToLine boolean (optional)
|
|
150
|
+
* @param target Vec2 (optional)
|
|
149
151
|
*/
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
* @param point
|
|
154
|
-
*/
|
|
155
|
-
closestPointOnInfiniteLine(point: Vector2): Vec2;
|
|
156
|
-
/**
|
|
157
|
-
* Returns the closest point on the line **section** to the given point.
|
|
158
|
-
* @param point
|
|
159
|
-
*/
|
|
160
|
-
closestPointOnLine(point: Vector2): Vec2;
|
|
152
|
+
closestPointToPoint(point: Vector2, clampToLine?: boolean, target?: Vec2): Vec2;
|
|
153
|
+
delta(target: Vec2): Vec2;
|
|
154
|
+
closestPointToPointParameter(point: any, clampToLine: any): number;
|
|
161
155
|
/**
|
|
162
156
|
* Returns the distance between the **infinite** line and the point.
|
|
163
157
|
* @param point
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const TwoPI: number;
|
package/types/index.d.ts
CHANGED
|
@@ -8,3 +8,6 @@ export { BoundingBox } from "./BoundingBox";
|
|
|
8
8
|
export { Rectangle } from "./Rectangle";
|
|
9
9
|
export { normalizeAngleDegrees } from "./normalizeAngleDegrees";
|
|
10
10
|
export { normalizeAngleRadians } from "./normalizeAngleRadians";
|
|
11
|
+
export { TwoPI } from "./MathConstants";
|
|
12
|
+
export { Point2 } from "./Point2";
|
|
13
|
+
export { Point3 } from "./Point3";
|
package/HowToRelease.txt
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
Update documentation:
|
|
2
|
-
npx typedoc --plugin typedoc-plugin-markdown --out docs src/index.ts --excludeExternals --excludeProtected --excludePrivate --githubPages false
|
|
3
|
-
|
|
4
|
-
Before release
|
|
5
|
-
- Ensure clean working directory
|
|
6
|
-
- Ensure that the last release commit has a tag that matches the latest release number in the format "0.0.11". This step should have been completed in the previous release. It's only mentioned here in case it's for any reason missing.
|
|
7
|
-
- Run "prerelease" script to verify that the code builds and the tests pass
|
|
8
|
-
|
|
9
|
-
Release:
|
|
10
|
-
- Bump up version in package.json e.g. "0.0.11" -> "0.0.12"
|
|
11
|
-
- Run "version" script, it should add the new commits since the last release into `CHANGELOG.md`
|
|
12
|
-
- Commit and push the changes in `package.json` and `CHANGELOG.md`, ideally, call the commit as the new release e.g. "`0.0.12`". This is not necessary but makes it easier to find
|
|
13
|
-
- **Tag the commit** the new version number. In this example "`0.0.12`" and push. This is required for CI to build and publish to npm.
|