vec3 0.1.7 → 0.1.9
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/.github/dependabot.yml +7 -0
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/commands.yml +22 -0
- package/.github/workflows/publish.yml +17 -10
- package/HISTORY.md +11 -0
- package/README.md +54 -44
- package/index.d.ts +232 -74
- package/index.js +73 -22
- package/package.json +18 -5
- package/test/index.test-d.ts +60 -0
- package/test/test.js +188 -113
- package/wrapper.mjs +4 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
name: Repo Commands
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
issue_comment: # Handle comment commands
|
|
5
|
+
types: [created]
|
|
6
|
+
pull_request: # Handle renamed PRs
|
|
7
|
+
types: [edited]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
comment-trigger:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- name: Check out repository
|
|
14
|
+
uses: actions/checkout@v3
|
|
15
|
+
- name: Run command handlers
|
|
16
|
+
uses: PrismarineJS/prismarine-repo-actions@master
|
|
17
|
+
with:
|
|
18
|
+
# NOTE: You must specify a Personal Access Token (PAT) with repo access here. While you can use the default GITHUB_TOKEN, actions taken with it will not trigger other actions, so if you have a CI workflow, commits created by this action will not trigger it.
|
|
19
|
+
token: ${{ secrets.PAT_PASSWORD }}
|
|
20
|
+
# See `Options` section below for more info on these options
|
|
21
|
+
install-command: npm install
|
|
22
|
+
/fixlint.fix-command: npm run fix
|
|
@@ -13,13 +13,20 @@ jobs:
|
|
|
13
13
|
- name: Set up Node.js
|
|
14
14
|
uses: actions/setup-node@master
|
|
15
15
|
with:
|
|
16
|
-
node-version:
|
|
17
|
-
-
|
|
18
|
-
uses:
|
|
19
|
-
with:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
node-version: 18.0.0
|
|
17
|
+
- id: publish
|
|
18
|
+
uses: JS-DevTools/npm-publish@v1
|
|
19
|
+
with:
|
|
20
|
+
token: ${{ secrets.NPM_AUTH_TOKEN }}
|
|
21
|
+
- name: Create Release
|
|
22
|
+
if: steps.publish.outputs.type != 'none'
|
|
23
|
+
id: create_release
|
|
24
|
+
uses: actions/create-release@v1
|
|
25
|
+
env:
|
|
26
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
27
|
+
with:
|
|
28
|
+
tag_name: ${{ steps.publish.outputs.version }}
|
|
29
|
+
release_name: Release ${{ steps.publish.outputs.version }}
|
|
30
|
+
body: ${{ steps.publish.outputs.version }}
|
|
31
|
+
draft: false
|
|
32
|
+
prerelease: false
|
package/HISTORY.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
## 0.1.9
|
|
2
|
+
* [Add methods, fix .equals (#46)](https://github.com/PrismarineJS/node-vec3/commit/c6b94c4289cfba5fc460bed99d112cec85fe1cf3) (thanks @szdytom)
|
|
3
|
+
* [Add command gh workflow allowing to use release command in comments (#45)](https://github.com/PrismarineJS/node-vec3/commit/8675f8ecf6065278c0d1f889a585a5febf446cc0) (thanks @rom1504)
|
|
4
|
+
* [Update to node 18.0.0 (#44)](https://github.com/PrismarineJS/node-vec3/commit/358445025ff7d558c8f3fddec2d5786c8c468db5) (thanks @rom1504)
|
|
5
|
+
* [New publish workflow (#38)](https://github.com/PrismarineJS/node-vec3/commit/ed6ba10a9b3d163f5c1ee8cb1b78108296b98477) (thanks @KTibow)
|
|
6
|
+
|
|
7
|
+
## 0.1.8
|
|
8
|
+
|
|
9
|
+
* fix some typescript stuff
|
|
10
|
+
* new methods: rounded, round, multiply, and divide
|
|
11
|
+
|
|
1
12
|
## 0.1.7
|
|
2
13
|
* fix standard not being a dev dependency
|
|
3
14
|
|
package/README.md
CHANGED
|
@@ -29,55 +29,65 @@ More available functions are listed below in Test Coverage.
|
|
|
29
29
|
## Test Coverage
|
|
30
30
|
|
|
31
31
|
```
|
|
32
|
-
v()
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
v()
|
|
33
|
+
✔ no args
|
|
34
|
+
✔ x, y, z
|
|
35
|
+
✔ array
|
|
36
|
+
✔ object
|
|
37
|
+
✔ string coords
|
|
38
|
+
✔ deserialize
|
|
39
|
+
✔ invalid deserialize
|
|
40
40
|
|
|
41
41
|
vec3
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
42
|
+
✔ isZero
|
|
43
|
+
✔ at
|
|
44
|
+
✔ xz
|
|
45
|
+
✔ xy
|
|
46
|
+
✔ yz
|
|
47
|
+
✔ xzy
|
|
48
|
+
✔ rounded
|
|
49
|
+
✔ round
|
|
50
|
+
✔ floored
|
|
51
|
+
✔ floor
|
|
52
|
+
✔ offset
|
|
53
|
+
✔ translate
|
|
54
|
+
✔ plus
|
|
55
|
+
✔ minus
|
|
56
|
+
✔ scaled
|
|
57
|
+
✔ abs
|
|
58
|
+
✔ distanceTo
|
|
59
|
+
✔ distanceSquared
|
|
60
|
+
✔ equals
|
|
61
|
+
✔ toString
|
|
62
|
+
✔ clone
|
|
63
|
+
✔ add
|
|
64
|
+
✔ subtract
|
|
65
|
+
✔ multiply
|
|
66
|
+
✔ divide
|
|
67
|
+
✔ set
|
|
68
|
+
✔ modulus
|
|
69
|
+
✔ volume
|
|
70
|
+
✔ min
|
|
71
|
+
✔ max
|
|
72
|
+
✔ update
|
|
73
|
+
✔ norm
|
|
74
|
+
✔ dot
|
|
75
|
+
✔ cross
|
|
76
|
+
✔ unit
|
|
77
|
+
✔ normalize
|
|
78
|
+
✔ scale
|
|
79
|
+
✔ xyDistanceTo
|
|
80
|
+
✔ xzDistanceTo
|
|
81
|
+
✔ yzDistanceTo
|
|
82
|
+
✔ innerProduct
|
|
83
|
+
✔ manhattanDistanceTo
|
|
84
|
+
✔ toArray
|
|
85
|
+
|
|
86
|
+
50 passing (14ms)
|
|
77
87
|
```
|
|
78
88
|
|
|
79
89
|
More functions welcome in the form of pull requests.
|
|
80
90
|
|
|
81
91
|
## History
|
|
82
92
|
|
|
83
|
-
See [History](
|
|
93
|
+
See [History](HISTORY.md)
|
package/index.d.ts
CHANGED
|
@@ -1,79 +1,237 @@
|
|
|
1
1
|
export class Vec3 {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
2
|
+
constructor(x: number, y: number, z: number);
|
|
3
|
+
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
z: number;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns true when it is a zero vector.
|
|
10
|
+
*/
|
|
11
|
+
isZero(): boolean;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Access component by index
|
|
15
|
+
*/
|
|
16
|
+
at(id: number): number;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns an array component x, z
|
|
20
|
+
*/
|
|
21
|
+
xz(): [number, number];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns an array component x, y
|
|
25
|
+
*/
|
|
26
|
+
xy(): [number, number];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns an array component y, z
|
|
30
|
+
*/
|
|
31
|
+
yz(): [number, number];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns a vector with swapped y and z
|
|
35
|
+
*/
|
|
36
|
+
xzy(): Vec3;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Set own values to given x y z
|
|
40
|
+
* If some components is given null, then those components won't change
|
|
41
|
+
*/
|
|
42
|
+
set(x: number, y: number, z: number): this;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Set own values to values given by other
|
|
46
|
+
*/
|
|
47
|
+
update(other: Vec3): this;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Return a new instance with copied values that are rounded
|
|
51
|
+
*/
|
|
52
|
+
rounded(): Vec3;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Round own values to nearest integer
|
|
56
|
+
*/
|
|
57
|
+
round(): this;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Return a new instance with copied values that are floored
|
|
61
|
+
*/
|
|
62
|
+
floored(): Vec3;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Floor own values
|
|
66
|
+
*/
|
|
67
|
+
floor(): this;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Return a new instance with copied values that are offset by dx dy and dz
|
|
71
|
+
*/
|
|
72
|
+
offset(dx: number, dy: number, dz: number): Vec3;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Translate own values by dx dy and dz
|
|
76
|
+
*/
|
|
77
|
+
translate(dx: number, dy: number, dz: number): this;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Add to own values by vector
|
|
81
|
+
*/
|
|
82
|
+
add(other: Vec3): this;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Subtract own values by vector
|
|
86
|
+
*/
|
|
87
|
+
subtract(other: Vec3): this;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Multiply own values by value from vector
|
|
91
|
+
*/
|
|
92
|
+
multiply(other: Vec3): this;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Divide own values by value from vector
|
|
96
|
+
*/
|
|
97
|
+
divide(other: Vec3): this;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Return a new instance with copied values that are added to by vector
|
|
101
|
+
*/
|
|
102
|
+
plus(other: Vec3): Vec3;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Return a new instance with copied values that are subtracted by vector
|
|
106
|
+
*/
|
|
107
|
+
minus(other: Vec3): Vec3;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Return a new instance with copied values that are scaled by number
|
|
111
|
+
*/
|
|
112
|
+
scaled(scalar: number): Vec3;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Return a new instance with copied values that are absolute
|
|
116
|
+
*/
|
|
117
|
+
abs(): Vec3;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Return the volume off the vector
|
|
121
|
+
*/
|
|
122
|
+
volume(): number;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Return a new instance with copied values that are modulated by value from a vector
|
|
126
|
+
*/
|
|
127
|
+
modulus(other: Vec3): Vec3;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Return the euclidean distance to another vector
|
|
131
|
+
*/
|
|
132
|
+
distanceTo(other: Vec3): number;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Return the squared euclidean distance to another vector
|
|
136
|
+
*/
|
|
137
|
+
distanceSquared(other: Vec3): number;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check whether two vectors are equal
|
|
141
|
+
* Returns true if each components have at most `error` difference
|
|
142
|
+
*/
|
|
143
|
+
equals(other: Vec3, error?: number): boolean;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Converts own values to a string representation in the format `(x, y, z)`
|
|
147
|
+
*/
|
|
148
|
+
toString(): string;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Return a new instance with the same values
|
|
152
|
+
*/
|
|
153
|
+
clone(): Vec3;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Return a new instance with the min values by value compared to another vector
|
|
157
|
+
*/
|
|
158
|
+
min(other: Vec3): Vec3;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Return a new instance with the max values by value compared to another vector
|
|
162
|
+
*/
|
|
163
|
+
max(other: Vec3): Vec3;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns its own euclidean norm
|
|
167
|
+
*/
|
|
168
|
+
norm(): number;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns the dot product with another vector
|
|
172
|
+
*/
|
|
173
|
+
dot(other: Vec3): number;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns a new instance off the cross product to another vector
|
|
177
|
+
*/
|
|
178
|
+
cross(other: Vec3): Vec3;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns a new instance with copied values normed to the unit vector
|
|
182
|
+
*/
|
|
183
|
+
unit(): Vec3;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Normalize own values
|
|
187
|
+
*/
|
|
188
|
+
normalize(): Vec3;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Scale own values by a number
|
|
192
|
+
*/
|
|
193
|
+
scale(scalar: number): this;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Returns the xy distance to another vector
|
|
197
|
+
*/
|
|
198
|
+
xyDistanceTo(other: Vec3): number;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Returns the xz distance to another vector
|
|
202
|
+
*/
|
|
203
|
+
xzDistanceTo(other: Vec3): number;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Returns the yz distance to another vector
|
|
207
|
+
*/
|
|
208
|
+
yzDistanceTo(other: Vec3): number;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Returns the inner product to another vector
|
|
212
|
+
*/
|
|
213
|
+
innerProduct(other: Vec3): number;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Returns the manhattan distance to another vector
|
|
217
|
+
*/
|
|
218
|
+
manhattanDistanceTo(other: Vec3): number;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Returns an array with x y z in array form ie [x, y, z]
|
|
222
|
+
*/
|
|
223
|
+
toArray(): [number, number, number];
|
|
73
224
|
}
|
|
74
225
|
|
|
75
226
|
export default function v(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
227
|
+
coordinates:
|
|
228
|
+
| null
|
|
229
|
+
| string
|
|
230
|
+
| [number | string, number | string, number | string]
|
|
231
|
+
| { x: number | string; y: number | string; z: number | string }
|
|
232
|
+
): Vec3;
|
|
233
|
+
export default function v(
|
|
234
|
+
x: number | string,
|
|
235
|
+
y: number | string,
|
|
236
|
+
z: number | string
|
|
79
237
|
): Vec3;
|
package/index.js
CHANGED
|
@@ -7,6 +7,30 @@ class Vec3 {
|
|
|
7
7
|
this.z = z
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
isZero () {
|
|
11
|
+
return this.x === 0 && this.y === 0 && this.z === 0
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
at (id) {
|
|
15
|
+
return this.toArray()[id]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
xz () {
|
|
19
|
+
return [this.x, this.z]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
xy () {
|
|
23
|
+
return [this.x, this.y]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
yz () {
|
|
27
|
+
return [this.y, this.z]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
xzy () {
|
|
31
|
+
return new Vec3(this.x, this.z, this.y)
|
|
32
|
+
}
|
|
33
|
+
|
|
10
34
|
set (x, y, z) {
|
|
11
35
|
this.x = x
|
|
12
36
|
this.y = y
|
|
@@ -21,6 +45,17 @@ class Vec3 {
|
|
|
21
45
|
return this
|
|
22
46
|
}
|
|
23
47
|
|
|
48
|
+
rounded () {
|
|
49
|
+
return new Vec3(Math.round(this.x), Math.round(this.y), Math.round(this.z))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
round () {
|
|
53
|
+
this.x = Math.round(this.x)
|
|
54
|
+
this.y = Math.round(this.y)
|
|
55
|
+
this.z = Math.round(this.z)
|
|
56
|
+
return this
|
|
57
|
+
}
|
|
58
|
+
|
|
24
59
|
floored () {
|
|
25
60
|
return new Vec3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z))
|
|
26
61
|
}
|
|
@@ -57,6 +92,20 @@ class Vec3 {
|
|
|
57
92
|
return this
|
|
58
93
|
}
|
|
59
94
|
|
|
95
|
+
multiply (other) {
|
|
96
|
+
this.x *= other.x
|
|
97
|
+
this.y *= other.y
|
|
98
|
+
this.z *= other.z
|
|
99
|
+
return this
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
divide (other) {
|
|
103
|
+
this.x /= other.x
|
|
104
|
+
this.y /= other.y
|
|
105
|
+
this.z /= other.z
|
|
106
|
+
return this
|
|
107
|
+
}
|
|
108
|
+
|
|
60
109
|
plus (other) {
|
|
61
110
|
return this.offset(other.x, other.y, other.z)
|
|
62
111
|
}
|
|
@@ -85,21 +134,23 @@ class Vec3 {
|
|
|
85
134
|
}
|
|
86
135
|
|
|
87
136
|
distanceTo (other) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
137
|
+
const dx = other.x - this.x
|
|
138
|
+
const dy = other.y - this.y
|
|
139
|
+
const dz = other.z - this.z
|
|
91
140
|
return Math.sqrt(dx * dx + dy * dy + dz * dz)
|
|
92
141
|
}
|
|
93
142
|
|
|
94
143
|
distanceSquared (other) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
144
|
+
const dx = other.x - this.x
|
|
145
|
+
const dy = other.y - this.y
|
|
146
|
+
const dz = other.z - this.z
|
|
98
147
|
return dx * dx + dy * dy + dz * dz
|
|
99
148
|
}
|
|
100
149
|
|
|
101
|
-
equals (other) {
|
|
102
|
-
return this.x
|
|
150
|
+
equals (other, error = 0) {
|
|
151
|
+
return Math.abs(this.x - other.x) <= error &&
|
|
152
|
+
Math.abs(this.y - other.y) <= error &&
|
|
153
|
+
Math.abs(this.z - other.z) <= error
|
|
103
154
|
}
|
|
104
155
|
|
|
105
156
|
toString () {
|
|
@@ -157,20 +208,20 @@ class Vec3 {
|
|
|
157
208
|
}
|
|
158
209
|
|
|
159
210
|
xyDistanceTo (other) {
|
|
160
|
-
|
|
161
|
-
|
|
211
|
+
const dx = other.x - this.x
|
|
212
|
+
const dy = other.y - this.y
|
|
162
213
|
return Math.sqrt(dx * dx + dy * dy)
|
|
163
214
|
}
|
|
164
215
|
|
|
165
216
|
xzDistanceTo (other) {
|
|
166
|
-
|
|
167
|
-
|
|
217
|
+
const dx = other.x - this.x
|
|
218
|
+
const dz = other.z - this.z
|
|
168
219
|
return Math.sqrt(dx * dx + dz * dz)
|
|
169
220
|
}
|
|
170
221
|
|
|
171
222
|
yzDistanceTo (other) {
|
|
172
|
-
|
|
173
|
-
|
|
223
|
+
const dy = other.y - this.y
|
|
224
|
+
const dz = other.z - this.z
|
|
174
225
|
return Math.sqrt(dy * dy + dz * dz)
|
|
175
226
|
}
|
|
176
227
|
|
|
@@ -191,26 +242,26 @@ function v (x, y, z) {
|
|
|
191
242
|
if (x == null) {
|
|
192
243
|
return new Vec3(0, 0, 0)
|
|
193
244
|
} else if (Array.isArray(x)) {
|
|
194
|
-
return new Vec3(parseFloat(x[0]
|
|
245
|
+
return new Vec3(parseFloat(x[0]), parseFloat(x[1]), parseFloat(x[2]))
|
|
195
246
|
} else if (typeof x === 'object') {
|
|
196
|
-
return new Vec3(parseFloat(x.x
|
|
247
|
+
return new Vec3(parseFloat(x.x), parseFloat(x.y), parseFloat(x.z))
|
|
197
248
|
} else if (typeof x === 'string' && y == null) {
|
|
198
|
-
|
|
249
|
+
const match = x.match(re)
|
|
199
250
|
if (match) {
|
|
200
251
|
return new Vec3(
|
|
201
|
-
parseFloat(match[1]
|
|
202
|
-
parseFloat(match[2]
|
|
203
|
-
parseFloat(match[3]
|
|
252
|
+
parseFloat(match[1]),
|
|
253
|
+
parseFloat(match[2]),
|
|
254
|
+
parseFloat(match[3]))
|
|
204
255
|
} else {
|
|
205
256
|
throw new Error('vec3: cannot parse: ' + x)
|
|
206
257
|
}
|
|
207
258
|
} else {
|
|
208
|
-
return new Vec3(parseFloat(x
|
|
259
|
+
return new Vec3(parseFloat(x), parseFloat(y), parseFloat(z))
|
|
209
260
|
}
|
|
210
261
|
}
|
|
211
262
|
|
|
212
263
|
function euclideanMod (numerator, denominator) {
|
|
213
|
-
|
|
264
|
+
const result = numerator % denominator
|
|
214
265
|
return result < 0 ? result + denominator : result
|
|
215
266
|
}
|
|
216
267
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vec3",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "3d vector math with good unit tests",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "
|
|
8
|
+
"test": "npm run test-js && npm run test-types",
|
|
9
|
+
"test-js": "mocha --reporter spec",
|
|
10
|
+
"test-types": "tsd",
|
|
9
11
|
"pretest": "npm run lint",
|
|
10
12
|
"lint": "standard",
|
|
11
13
|
"fix": "standard --fix"
|
|
@@ -13,12 +15,23 @@
|
|
|
13
15
|
"keywords": [
|
|
14
16
|
"point"
|
|
15
17
|
],
|
|
18
|
+
"exports": {
|
|
19
|
+
"require": "./index.js",
|
|
20
|
+
"import": "./wrapper.mjs"
|
|
21
|
+
},
|
|
16
22
|
"author": "Andrew Kelley",
|
|
17
23
|
"license": "BSD",
|
|
18
24
|
"devDependencies": {
|
|
19
|
-
"mocha": "^
|
|
20
|
-
"standard": "^
|
|
25
|
+
"mocha": "^10.0.0",
|
|
26
|
+
"standard": "^17.0.0",
|
|
27
|
+
"tsd": "^0.25.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {},
|
|
30
|
+
"tsd": {
|
|
31
|
+
"directory": "test"
|
|
21
32
|
},
|
|
22
|
-
"
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/PrismarineJS/node-vec3.git"
|
|
23
36
|
}
|
|
24
37
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { expectType } from "tsd";
|
|
2
|
+
import vec3, { Vec3 } from "..";
|
|
3
|
+
|
|
4
|
+
const vec = vec3([1, 2, 3]);
|
|
5
|
+
expectType<Vec3>(vec);
|
|
6
|
+
expectType<Vec3>(vec3([1, 2, 3]));
|
|
7
|
+
expectType<Vec3>(vec3({ x: 1, y: 2, z: 3 }));
|
|
8
|
+
expectType<Vec3>(vec3("1, 2, 3"));
|
|
9
|
+
expectType<Vec3>(vec3(1, 2, 3));
|
|
10
|
+
expectType<Vec3>(new Vec3(1,2,3));
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
vec3(1, 2);
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
vec3("1", 2);
|
|
15
|
+
|
|
16
|
+
expectType<number>(vec.x);
|
|
17
|
+
expectType<number>(vec.y);
|
|
18
|
+
expectType<number>(vec.z);
|
|
19
|
+
|
|
20
|
+
expectType<boolean>(vec.isZero());
|
|
21
|
+
expectType<number>(vec.at(1));
|
|
22
|
+
expectType<[number, number]>(vec.xz());
|
|
23
|
+
expectType<[number, number]>(vec.xy());
|
|
24
|
+
expectType<[number, number]>(vec.yz());
|
|
25
|
+
expectType<Vec3>(vec.xzy());
|
|
26
|
+
expectType<Vec3>(vec.set(4, 5, 6));
|
|
27
|
+
expectType<Vec3>(vec.update(vec));
|
|
28
|
+
expectType<Vec3>(vec.floored());
|
|
29
|
+
expectType<Vec3>(vec.floor());
|
|
30
|
+
expectType<Vec3>(vec.offset(0, 0, 0));
|
|
31
|
+
expectType<Vec3>(vec.translate(0, 0, 0));
|
|
32
|
+
expectType<Vec3>(vec.add(vec));
|
|
33
|
+
expectType<Vec3>(vec.subtract(vec));
|
|
34
|
+
expectType<Vec3>(vec.multiply(vec));
|
|
35
|
+
expectType<Vec3>(vec.divide(vec));
|
|
36
|
+
expectType<Vec3>(vec.plus(vec));
|
|
37
|
+
expectType<Vec3>(vec.minus(vec));
|
|
38
|
+
expectType<Vec3>(vec.scaled(2));
|
|
39
|
+
expectType<Vec3>(vec.abs());
|
|
40
|
+
expectType<number>(vec.volume());
|
|
41
|
+
expectType<Vec3>(vec.modulus(vec));
|
|
42
|
+
expectType<number>(vec.distanceTo(vec));
|
|
43
|
+
expectType<number>(vec.distanceSquared(vec));
|
|
44
|
+
expectType<boolean>(vec.equals(vec));
|
|
45
|
+
expectType<string>(vec.toString());
|
|
46
|
+
expectType<Vec3>(vec.clone());
|
|
47
|
+
expectType<Vec3>(vec.min(vec));
|
|
48
|
+
expectType<Vec3>(vec.max(vec));
|
|
49
|
+
expectType<number>(vec.norm());
|
|
50
|
+
expectType<number>(vec.dot(vec));
|
|
51
|
+
expectType<Vec3>(vec.cross(vec));
|
|
52
|
+
expectType<Vec3>(vec.unit());
|
|
53
|
+
expectType<Vec3>(vec.normalize());
|
|
54
|
+
expectType<Vec3>(vec.scale(2));
|
|
55
|
+
expectType<number>(vec.xyDistanceTo(vec));
|
|
56
|
+
expectType<number>(vec.xzDistanceTo(vec));
|
|
57
|
+
expectType<number>(vec.yzDistanceTo(vec));
|
|
58
|
+
expectType<number>(vec.innerProduct(vec));
|
|
59
|
+
expectType<number>(vec.manhattanDistanceTo(vec));
|
|
60
|
+
expectType<[number, number, number]>(vec.toArray());
|
package/test/test.js
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
/* eslint-env mocha */
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const v = require('../')
|
|
4
|
+
const Vec3 = v.Vec3
|
|
5
|
+
const assert = require('assert')
|
|
6
6
|
|
|
7
7
|
describe('v()', function () {
|
|
8
8
|
it('no args', function () {
|
|
9
|
-
|
|
9
|
+
const v1 = v()
|
|
10
10
|
assert.strictEqual(v1.x, 0)
|
|
11
11
|
assert.strictEqual(v1.y, 0)
|
|
12
12
|
assert.strictEqual(v1.z, 0)
|
|
13
13
|
})
|
|
14
14
|
it('x, y, z', function () {
|
|
15
|
-
|
|
15
|
+
const v1 = v(-1, 5, 10.10)
|
|
16
16
|
assert.strictEqual(v1.x, -1)
|
|
17
17
|
assert.strictEqual(v1.y, 5)
|
|
18
18
|
assert.strictEqual(v1.z, 10.10)
|
|
19
19
|
})
|
|
20
20
|
it('array', function () {
|
|
21
|
-
|
|
21
|
+
const v1 = v([4, 5, 6])
|
|
22
22
|
assert.strictEqual(v1.x, 4)
|
|
23
23
|
assert.strictEqual(v1.y, 5)
|
|
24
24
|
assert.strictEqual(v1.z, 6)
|
|
25
25
|
})
|
|
26
26
|
it('object', function () {
|
|
27
|
-
|
|
27
|
+
const v1 = v({ x: 9, y: 8, z: 7 })
|
|
28
28
|
assert.strictEqual(v1.x, 9)
|
|
29
29
|
assert.strictEqual(v1.y, 8)
|
|
30
30
|
assert.strictEqual(v1.z, 7)
|
|
31
31
|
})
|
|
32
32
|
it('string coords', function () {
|
|
33
|
-
|
|
33
|
+
const v1 = v('1', '1.5', '-30.2')
|
|
34
34
|
assert.strictEqual(v1.x, 1)
|
|
35
35
|
assert.strictEqual(v1.y, 1.5)
|
|
36
36
|
assert.strictEqual(v1.z, -30.2)
|
|
37
37
|
})
|
|
38
38
|
it('deserialize', function () {
|
|
39
|
-
|
|
39
|
+
const v1 = v(v(1, -3.5, 0).toString())
|
|
40
40
|
assert.strictEqual(v1.x, 1)
|
|
41
41
|
assert.strictEqual(v1.y, -3.5)
|
|
42
42
|
assert.strictEqual(v1.z, 0)
|
|
43
|
-
|
|
43
|
+
const v2 = v(v(-111, 222, 9876543210.12345).toString())
|
|
44
44
|
assert.strictEqual(v2.x, -111)
|
|
45
45
|
assert.strictEqual(v2.y, 222)
|
|
46
|
-
assert.strictEqual(v2.z, 9876543210.
|
|
46
|
+
assert.strictEqual(v2.z, 9876543210.12345)
|
|
47
47
|
})
|
|
48
48
|
it('invalid deserialize', function () {
|
|
49
49
|
assert.throws(function () {
|
|
@@ -52,41 +52,94 @@ describe('v()', function () {
|
|
|
52
52
|
})
|
|
53
53
|
})
|
|
54
54
|
describe('vec3', function () {
|
|
55
|
+
it('isZero', function () {
|
|
56
|
+
const v1 = new Vec3(0, 1, 2)
|
|
57
|
+
const v2 = new Vec3(0, 0, 0)
|
|
58
|
+
assert.ok(!v1.isZero())
|
|
59
|
+
assert.ok(v2.isZero())
|
|
60
|
+
})
|
|
61
|
+
it('at', function () {
|
|
62
|
+
const v1 = new Vec3(0, 1, 2)
|
|
63
|
+
assert.strictEqual(v1.at(0), 0)
|
|
64
|
+
assert.strictEqual(v1.at(1), 1)
|
|
65
|
+
assert.strictEqual(v1.at(2), 2)
|
|
66
|
+
})
|
|
67
|
+
it('xz', function () {
|
|
68
|
+
const v1 = new Vec3(0, 1, 2)
|
|
69
|
+
const a = v1.xz()
|
|
70
|
+
assert.strictEqual(a[0], 0)
|
|
71
|
+
assert.strictEqual(a[1], 2)
|
|
72
|
+
})
|
|
73
|
+
it('xy', function () {
|
|
74
|
+
const v1 = new Vec3(0, 1, 2)
|
|
75
|
+
const a = v1.xy()
|
|
76
|
+
assert.strictEqual(a[0], 0)
|
|
77
|
+
assert.strictEqual(a[1], 1)
|
|
78
|
+
})
|
|
79
|
+
it('yz', function () {
|
|
80
|
+
const v1 = new Vec3(0, 1, 2)
|
|
81
|
+
const a = v1.yz()
|
|
82
|
+
assert.strictEqual(a[0], 1)
|
|
83
|
+
assert.strictEqual(a[1], 2)
|
|
84
|
+
})
|
|
85
|
+
it('xzy', function () {
|
|
86
|
+
const v1 = new Vec3(0, 1, 2)
|
|
87
|
+
const v2 = v1.xzy()
|
|
88
|
+
assert.strictEqual(v2.x, 0)
|
|
89
|
+
assert.strictEqual(v2.y, 2)
|
|
90
|
+
assert.strictEqual(v2.z, 1)
|
|
91
|
+
})
|
|
92
|
+
it('rounded', function () {
|
|
93
|
+
const v1 = new Vec3(1.1, -1.5, 1.9)
|
|
94
|
+
const v2 = v1.rounded()
|
|
95
|
+
v1.x = 10
|
|
96
|
+
assert.strictEqual(v2.x, 1)
|
|
97
|
+
assert.strictEqual(v2.y, -1)
|
|
98
|
+
assert.strictEqual(v2.z, 2)
|
|
99
|
+
})
|
|
100
|
+
it('round', function () {
|
|
101
|
+
const v1 = new Vec3(1.1, -1.5, 1.9)
|
|
102
|
+
const v2 = v1.round()
|
|
103
|
+
assert.strictEqual(v2, v1)
|
|
104
|
+
assert.strictEqual(v1.x, 1)
|
|
105
|
+
assert.strictEqual(v1.y, -1)
|
|
106
|
+
assert.strictEqual(v1.z, 2)
|
|
107
|
+
})
|
|
55
108
|
it('floored', function () {
|
|
56
|
-
|
|
57
|
-
|
|
109
|
+
const v1 = new Vec3(1.1, -1.5, 1.9)
|
|
110
|
+
const v2 = v1.floored()
|
|
58
111
|
v1.x = 10
|
|
59
112
|
assert.strictEqual(v2.x, 1)
|
|
60
113
|
assert.strictEqual(v2.y, -2)
|
|
61
114
|
assert.strictEqual(v2.z, 1)
|
|
62
115
|
})
|
|
63
116
|
it('floor', function () {
|
|
64
|
-
|
|
65
|
-
|
|
117
|
+
const v1 = new Vec3(1.1, -1.5, 1.9)
|
|
118
|
+
const v2 = v1.floor()
|
|
66
119
|
assert.strictEqual(v2, v1)
|
|
67
120
|
assert.strictEqual(v1.x, 1)
|
|
68
121
|
assert.strictEqual(v1.y, -2)
|
|
69
122
|
assert.strictEqual(v1.z, 1)
|
|
70
123
|
})
|
|
71
124
|
it('offset', function () {
|
|
72
|
-
|
|
73
|
-
|
|
125
|
+
const v1 = new Vec3(1, 2, 3)
|
|
126
|
+
const v2 = v1.offset(10, -10, 20)
|
|
74
127
|
v1.x = -100
|
|
75
128
|
assert.strictEqual(v2.x, 11)
|
|
76
129
|
assert.strictEqual(v2.y, -8)
|
|
77
130
|
assert.strictEqual(v2.z, 23)
|
|
78
131
|
})
|
|
79
132
|
it('translate', function () {
|
|
80
|
-
|
|
133
|
+
const v1 = new Vec3(1, 2, 3)
|
|
81
134
|
v1.translate(10, -10, 20)
|
|
82
135
|
assert.strictEqual(v1.x, 11)
|
|
83
136
|
assert.strictEqual(v1.y, -8)
|
|
84
137
|
assert.strictEqual(v1.z, 23)
|
|
85
138
|
})
|
|
86
139
|
it('plus', function () {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
140
|
+
const v1 = new Vec3(1, 2, 3)
|
|
141
|
+
const v2 = new Vec3(-1, 0, 1)
|
|
142
|
+
const v3 = v1.plus(v2)
|
|
90
143
|
assert.strictEqual(v1.x, 1)
|
|
91
144
|
assert.strictEqual(v1.y, 2)
|
|
92
145
|
assert.strictEqual(v1.z, 3)
|
|
@@ -98,9 +151,9 @@ describe('vec3', function () {
|
|
|
98
151
|
assert.strictEqual(v3.z, 4)
|
|
99
152
|
})
|
|
100
153
|
it('minus', function () {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
154
|
+
const v1 = new Vec3(1, 2, 3)
|
|
155
|
+
const v2 = new Vec3(-1, 0, 1)
|
|
156
|
+
const v3 = v1.minus(v2)
|
|
104
157
|
assert.strictEqual(v1.x, 1)
|
|
105
158
|
assert.strictEqual(v1.y, 2)
|
|
106
159
|
assert.strictEqual(v1.z, 3)
|
|
@@ -112,8 +165,8 @@ describe('vec3', function () {
|
|
|
112
165
|
assert.strictEqual(v3.z, 2)
|
|
113
166
|
})
|
|
114
167
|
it('scaled', function () {
|
|
115
|
-
|
|
116
|
-
|
|
168
|
+
const v1 = new Vec3(1, 2, 3)
|
|
169
|
+
const v2 = v1.scaled(2)
|
|
117
170
|
assert.strictEqual(v1.x, 1)
|
|
118
171
|
assert.strictEqual(v1.y, 2)
|
|
119
172
|
assert.strictEqual(v1.z, 3)
|
|
@@ -122,44 +175,48 @@ describe('vec3', function () {
|
|
|
122
175
|
assert.strictEqual(v2.z, 6)
|
|
123
176
|
})
|
|
124
177
|
it('abs', function () {
|
|
125
|
-
|
|
126
|
-
|
|
178
|
+
const v1 = new Vec3(1.1, -1.5, 1.9)
|
|
179
|
+
const v2 = v1.abs()
|
|
127
180
|
v1.x = 10
|
|
128
181
|
assert.strictEqual(v2.x, 1.1)
|
|
129
182
|
assert.strictEqual(v2.y, 1.5)
|
|
130
183
|
assert.strictEqual(v2.z, 1.9)
|
|
131
184
|
})
|
|
132
185
|
it('distanceTo', function () {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
186
|
+
const v1 = new Vec3(1, 1, 1)
|
|
187
|
+
const v2 = new Vec3(2, 2, 2)
|
|
188
|
+
const dist1 = v1.distanceTo(v2)
|
|
189
|
+
const dist2 = v2.distanceTo(v1)
|
|
190
|
+
const expected = 1.7320508075688772
|
|
138
191
|
assert.strictEqual(dist1, dist2)
|
|
139
192
|
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
|
|
140
193
|
})
|
|
141
194
|
it('distanceSquared', function () {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
195
|
+
const v1 = new Vec3(1, 1, 1)
|
|
196
|
+
const v2 = new Vec3(2, 2, 2)
|
|
197
|
+
const dist1 = v1.distanceSquared(v2)
|
|
198
|
+
const dist2 = v2.distanceSquared(v1)
|
|
199
|
+
const expected = 3
|
|
147
200
|
assert.strictEqual(dist1, dist2)
|
|
148
201
|
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
|
|
149
202
|
})
|
|
150
203
|
it('equals', function () {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
204
|
+
const v1 = new Vec3(1, 2, 3)
|
|
205
|
+
const v2 = v1.scaled(0.23424)
|
|
206
|
+
const v3 = v1.scaled(0.23424)
|
|
154
207
|
assert.ok(v2.equals(v3))
|
|
208
|
+
const v4 = new Vec3(0.1, 0, 0)
|
|
209
|
+
const v5 = new Vec3(0.2, 0, 0)
|
|
210
|
+
const v6 = new Vec3(0.3, 0, 0)
|
|
211
|
+
assert.ok(v4.plus(v5).equals(v6, Number.EPSILON))
|
|
155
212
|
})
|
|
156
213
|
it('toString', function () {
|
|
157
|
-
|
|
214
|
+
const v1 = new Vec3(1, -1, 3.14)
|
|
158
215
|
assert.strictEqual(v1.toString(), '(1, -1, 3.14)')
|
|
159
216
|
})
|
|
160
217
|
it('clone', function () {
|
|
161
|
-
|
|
162
|
-
|
|
218
|
+
const v1 = new Vec3(1, 2, 3)
|
|
219
|
+
const v2 = v1.clone()
|
|
163
220
|
v2.x = 10
|
|
164
221
|
assert.strictEqual(v1.x, 1)
|
|
165
222
|
assert.strictEqual(v1.y, 2)
|
|
@@ -169,35 +226,53 @@ describe('vec3', function () {
|
|
|
169
226
|
assert.strictEqual(v2.z, 3)
|
|
170
227
|
})
|
|
171
228
|
it('add', function () {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
229
|
+
const v1 = new Vec3(1, 2, 3)
|
|
230
|
+
const v2 = new Vec3(-1, -2, -3)
|
|
231
|
+
const v3 = v1.add(v2)
|
|
175
232
|
assert.strictEqual(v3, v1)
|
|
176
233
|
assert.strictEqual(v1.x, 0)
|
|
177
234
|
assert.strictEqual(v1.y, 0)
|
|
178
235
|
assert.strictEqual(v1.z, 0)
|
|
179
236
|
})
|
|
180
237
|
it('subtract', function () {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
238
|
+
const v1 = new Vec3(1, 2, 3)
|
|
239
|
+
const v2 = new Vec3(-1, -2, -3)
|
|
240
|
+
const v3 = v1.subtract(v2)
|
|
184
241
|
assert.strictEqual(v3, v1)
|
|
185
242
|
assert.strictEqual(v1.x, 2)
|
|
186
243
|
assert.strictEqual(v1.y, 4)
|
|
187
244
|
assert.strictEqual(v1.z, 6)
|
|
188
245
|
})
|
|
246
|
+
it('multiply', function () {
|
|
247
|
+
const v1 = new Vec3(1, 2, 3)
|
|
248
|
+
const v2 = new Vec3(-1, -2, -5)
|
|
249
|
+
const v3 = v1.multiply(v2)
|
|
250
|
+
assert.strictEqual(v3, v1)
|
|
251
|
+
assert.strictEqual(v1.x, -1)
|
|
252
|
+
assert.strictEqual(v1.y, -4)
|
|
253
|
+
assert.strictEqual(v1.z, -15)
|
|
254
|
+
})
|
|
255
|
+
it('divide', function () {
|
|
256
|
+
const v1 = new Vec3(10, 20, 30)
|
|
257
|
+
const v2 = new Vec3(2, 5, 3)
|
|
258
|
+
const v3 = v1.divide(v2)
|
|
259
|
+
assert.strictEqual(v3, v1)
|
|
260
|
+
assert.strictEqual(v1.x, 5)
|
|
261
|
+
assert.strictEqual(v1.y, 4)
|
|
262
|
+
assert.strictEqual(v1.z, 10)
|
|
263
|
+
})
|
|
189
264
|
it('set', function () {
|
|
190
|
-
|
|
191
|
-
|
|
265
|
+
const v1 = new Vec3(12, 32, 46)
|
|
266
|
+
const v2 = v1.set(0, 10, 100)
|
|
192
267
|
assert.strictEqual(v1, v2)
|
|
193
268
|
assert.strictEqual(v1.x, 0)
|
|
194
269
|
assert.strictEqual(v1.y, 10)
|
|
195
270
|
assert.strictEqual(v1.z, 100)
|
|
196
271
|
})
|
|
197
272
|
it('modulus', function () {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
273
|
+
const v1 = new Vec3(12, 32, -1)
|
|
274
|
+
const v2 = new Vec3(14, 32, 16)
|
|
275
|
+
const v3 = v1.modulus(v2)
|
|
201
276
|
assert.strictEqual(v1.x, 12)
|
|
202
277
|
assert.strictEqual(v1.y, 32)
|
|
203
278
|
assert.strictEqual(v1.z, -1)
|
|
@@ -209,29 +284,29 @@ describe('vec3', function () {
|
|
|
209
284
|
assert.strictEqual(v3.z, 15)
|
|
210
285
|
})
|
|
211
286
|
it('volume', function () {
|
|
212
|
-
|
|
287
|
+
const v1 = new Vec3(3, 4, 5)
|
|
213
288
|
assert.strictEqual(v1.volume(), 60)
|
|
214
289
|
})
|
|
215
290
|
it('min', function () {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
291
|
+
const v1 = new Vec3(-1, 0, 1)
|
|
292
|
+
const v2 = new Vec3(10, -10, 1.1)
|
|
293
|
+
const v3 = v1.min(v2)
|
|
219
294
|
assert.strictEqual(v3.x, -1)
|
|
220
295
|
assert.strictEqual(v3.y, -10)
|
|
221
296
|
assert.strictEqual(v3.z, 1)
|
|
222
297
|
})
|
|
223
298
|
it('max', function () {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
299
|
+
const v1 = new Vec3(-1, 0, 1)
|
|
300
|
+
const v2 = new Vec3(10, -10, 1.1)
|
|
301
|
+
const v3 = v1.max(v2)
|
|
227
302
|
assert.strictEqual(v3.x, 10)
|
|
228
303
|
assert.strictEqual(v3.y, 0)
|
|
229
304
|
assert.strictEqual(v3.z, 1.1)
|
|
230
305
|
})
|
|
231
306
|
it('update', function () {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
307
|
+
const v1 = new Vec3(-1, 0, 1)
|
|
308
|
+
const v2 = new Vec3(10, -10, 1.1)
|
|
309
|
+
const v3 = v1.update(v2)
|
|
235
310
|
assert.strictEqual(v3, v1)
|
|
236
311
|
assert.strictEqual(v1.x, 10)
|
|
237
312
|
assert.strictEqual(v1.y, -10)
|
|
@@ -241,97 +316,97 @@ describe('vec3', function () {
|
|
|
241
316
|
assert.strictEqual(v2.z, 1.1)
|
|
242
317
|
})
|
|
243
318
|
it('norm', function () {
|
|
244
|
-
|
|
245
|
-
assert.strictEqual(Math.round(v1.norm() * 100000), Math.round(14.
|
|
319
|
+
const v1 = new Vec3(-10, 0, 10)
|
|
320
|
+
assert.strictEqual(Math.round(v1.norm() * 100000), Math.round(14.1421356237 * 100000))
|
|
246
321
|
})
|
|
247
322
|
it('dot', function () {
|
|
248
|
-
|
|
249
|
-
|
|
323
|
+
const v1 = new Vec3(-1, -1, -1)
|
|
324
|
+
const v2 = new Vec3(1, 1, 1)
|
|
250
325
|
assert.strictEqual(v1.dot(v2), -3)
|
|
251
326
|
})
|
|
252
327
|
it('cross', function () {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
328
|
+
const v1 = new Vec3(1, 0, 0)
|
|
329
|
+
const v2 = new Vec3(0, 1, 0)
|
|
330
|
+
const v3 = new Vec3(0, 0, 1)
|
|
256
331
|
assert.ok(v1.cross(v2).equals(v3))
|
|
257
332
|
})
|
|
258
333
|
it('unit', function () {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.
|
|
262
|
-
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.
|
|
263
|
-
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.
|
|
264
|
-
|
|
265
|
-
|
|
334
|
+
const v1 = new Vec3(10, -10, 1.1)
|
|
335
|
+
const v2 = v1.unit()
|
|
336
|
+
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.70497744020 * 100000))
|
|
337
|
+
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.7049774402 * 100000))
|
|
338
|
+
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.07754751842 * 100000))
|
|
339
|
+
const v3 = new Vec3(0, 0, 0)
|
|
340
|
+
const v4 = v3.unit()
|
|
266
341
|
assert.strictEqual(v4.x, 0)
|
|
267
342
|
assert.strictEqual(v4.y, 0)
|
|
268
343
|
assert.strictEqual(v4.z, 0)
|
|
269
344
|
})
|
|
270
345
|
it('normalize', function () {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.
|
|
274
|
-
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.
|
|
275
|
-
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.
|
|
276
|
-
|
|
277
|
-
|
|
346
|
+
const v1 = new Vec3(10, -10, 1.1)
|
|
347
|
+
const v2 = v1.normalize()
|
|
348
|
+
assert.strictEqual(Math.round(v2.x * 100000), Math.round(0.70497744020 * 100000))
|
|
349
|
+
assert.strictEqual(Math.round(v2.y * 100000), Math.round(-0.7049774402 * 100000))
|
|
350
|
+
assert.strictEqual(Math.round(v2.z * 100000), Math.round(0.07754751842 * 100000))
|
|
351
|
+
const v3 = new Vec3(0, 0, 0)
|
|
352
|
+
const v4 = v3.normalize()
|
|
278
353
|
assert.strictEqual(v4.x, 0)
|
|
279
354
|
assert.strictEqual(v4.y, 0)
|
|
280
355
|
assert.strictEqual(v4.z, 0)
|
|
281
356
|
})
|
|
282
357
|
it('scale', function () {
|
|
283
|
-
|
|
284
|
-
|
|
358
|
+
const v1 = new Vec3(10, -10, 1.1)
|
|
359
|
+
const v2 = v1.scale(1.5)
|
|
285
360
|
assert.strictEqual(v2.x, 15)
|
|
286
361
|
assert.strictEqual(v2.y, -15)
|
|
287
362
|
assert.strictEqual(Math.round(v2.z * 100000), Math.round(1.65 * 100000))
|
|
288
363
|
})
|
|
289
364
|
it('xyDistanceTo', function () {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
365
|
+
const v1 = new Vec3(1, 1, 1)
|
|
366
|
+
const v2 = new Vec3(2, 2, 2)
|
|
367
|
+
const dist1 = v1.xyDistanceTo(v2)
|
|
368
|
+
const dist2 = v2.xyDistanceTo(v1)
|
|
369
|
+
const expected = 1.414213562
|
|
295
370
|
assert.strictEqual(dist1, dist2)
|
|
296
371
|
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
|
|
297
372
|
})
|
|
298
373
|
it('xzDistanceTo', function () {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
374
|
+
const v1 = new Vec3(1, 1, 1)
|
|
375
|
+
const v2 = new Vec3(2, 2, 2)
|
|
376
|
+
const dist1 = v1.xzDistanceTo(v2)
|
|
377
|
+
const dist2 = v2.xzDistanceTo(v1)
|
|
378
|
+
const expected = 1.41421356237
|
|
304
379
|
assert.strictEqual(dist1, dist2)
|
|
305
380
|
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
|
|
306
381
|
})
|
|
307
382
|
it('yzDistanceTo', function () {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
383
|
+
const v1 = new Vec3(1, 1, 1)
|
|
384
|
+
const v2 = new Vec3(2, 2, 2)
|
|
385
|
+
const dist1 = v1.yzDistanceTo(v2)
|
|
386
|
+
const dist2 = v2.yzDistanceTo(v1)
|
|
387
|
+
const expected = 1.41421356237
|
|
313
388
|
assert.strictEqual(dist1, dist2)
|
|
314
389
|
assert.strictEqual(Math.round(dist1 * 100000), Math.round(expected * 100000))
|
|
315
390
|
})
|
|
316
391
|
it('innerProduct', function () {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
392
|
+
const v1 = new Vec3(-1, 0, 1)
|
|
393
|
+
const v2 = new Vec3(0, 1, 0)
|
|
394
|
+
const ip1 = v1.innerProduct(v2)
|
|
395
|
+
const ip2 = v2.innerProduct(v1)
|
|
321
396
|
assert.strictEqual(ip1, ip2)
|
|
322
397
|
assert.strictEqual(ip1, 0)
|
|
323
398
|
})
|
|
324
399
|
it('manhattanDistanceTo', function () {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
400
|
+
const v1 = new Vec3(-1, 0, 1)
|
|
401
|
+
const v2 = new Vec3(10, -10, 1.1)
|
|
402
|
+
const dist1 = v1.manhattanDistanceTo(v2)
|
|
403
|
+
const dist2 = v2.manhattanDistanceTo(v1)
|
|
329
404
|
assert.strictEqual(dist1, dist2)
|
|
330
405
|
assert.strictEqual(dist1, 21.1)
|
|
331
406
|
})
|
|
332
407
|
it('toArray', function () {
|
|
333
|
-
|
|
334
|
-
|
|
408
|
+
const v1 = new Vec3(1, -1, 3.14)
|
|
409
|
+
const array = v1.toArray()
|
|
335
410
|
assert.strictEqual(v1.x, array[0])
|
|
336
411
|
assert.strictEqual(v1.y, array[1])
|
|
337
412
|
assert.strictEqual(v1.z, array[2])
|
package/wrapper.mjs
ADDED