cno-bedrock 0.0.4 → 0.1.0
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 +38 -0
- package/package.json +116 -116
- package/scripts/generate-docs.js +0 -0
- package/src/cno-assert.js +222 -15
- package/src/cno-assert.test.js +331 -0
- package/src/cno-base64.js +230 -0
- package/src/cno-base64.test.js +262 -0
- package/src/cno-bytes.js +387 -0
- package/src/cno-bytes.test.js +222 -0
- package/src/cno-constants.js +121 -0
- package/src/cno-files.js +2135 -0
- package/src/cno-files.test.js +939 -0
- package/src/cno-math.js +407 -0
- package/src/cno-math.test.js +249 -0
- package/src/cno-noop.js +6 -2
- package/src/cno-options.js +73 -4
- package/src/cno-options.test.js +65 -0
- package/src/cno-sha2-256.js +831 -0
- package/src/cno-sha2-256.test.js +697 -0
- package/src/cno-test-matrix.js +709 -0
- package/src/cno-test-matrix.test.js +199 -0
- package/src/cno-timed-cache-map.js +103 -0
- package/src/cno-timed-cache-map.test.js +113 -0
- package/src/cno-tree.js +365 -0
- package/src/cno-tree.test.js +147 -0
- package/src/cno-utility.js +545 -2
- package/src/cno-utility.test.js +276 -0
- package/src/cno-vector.js +1053 -0
- package/src/cno-vector.test.js +523 -0
- package/src/lib.js +5 -138
- package/src/lib.test.js +19 -7
- package/src/stream-test.js +116 -0
- package/test/big-lorem.txt +29 -0
|
@@ -0,0 +1,1053 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-vector.js](src/cno-vector.js)
|
|
4
|
+
> Robust vectors with arbitrary dimensions.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2024 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import { SQRT1_2, TAU, PI, HALF_PI, QUARTER_PI, DEG_TO_RAD, RAD_TO_DEG } from './cno-constants.js';
|
|
34
|
+
import { overflow, normalisePolarAngle } from './cno-math.js';
|
|
35
|
+
import { assertGreaterThan, assertLessThan, deepStrictEqual } from './cno-assert.js';
|
|
36
|
+
import deriveOptions from './cno-options.js';
|
|
37
|
+
//## Standard
|
|
38
|
+
//## External
|
|
39
|
+
//# Constants
|
|
40
|
+
const FILENAME = 'cno-vector.js';
|
|
41
|
+
//## Errors
|
|
42
|
+
|
|
43
|
+
//# Global Variables
|
|
44
|
+
/**## Functions*/
|
|
45
|
+
/**
|
|
46
|
+
### Vector
|
|
47
|
+
> Vector type smart constructor.
|
|
48
|
+
#### Parametres
|
|
49
|
+
| name | type | description |
|
|
50
|
+
| --- | --- | --- |
|
|
51
|
+
| input_options | object? | Additional options to pass to the smart constructor. |
|
|
52
|
+
|
|
53
|
+
##### Options Properties
|
|
54
|
+
| name | type | description |
|
|
55
|
+
| --- | --- | --- |
|
|
56
|
+
| packageMeta | PackageMeta? | An instance of [simple-package-meta](https://github.com/Anadian/simple-package-meta) to be used by this instance and any subclasses initialised along with it. |
|
|
57
|
+
| logger | object? | The logger to be used by this instance. |
|
|
58
|
+
| config | ConfigManager? | The [cno-config-manager] instance to be used by the created instance. |
|
|
59
|
+
| dimensions | number? | Create the Vector with this many dimensions filled with `defaultValue` [default: 1] |
|
|
60
|
+
| defaultValue | number? | The default value to fill the vector with [default: 0] |
|
|
61
|
+
|
|
62
|
+
#### Throws
|
|
63
|
+
| code | type | condition |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if `input_options` is neither an object nor `null` |
|
|
66
|
+
|
|
67
|
+
#### History
|
|
68
|
+
| version | change |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| 0.1.1 | Use `new.target`. |
|
|
71
|
+
| 0.0.0 | Introduced |
|
|
72
|
+
*/
|
|
73
|
+
export default function Vector( input_options ){
|
|
74
|
+
if( !new.target ){
|
|
75
|
+
var argArr = Array.from( arguments );
|
|
76
|
+
return ( new Vector( ...argArr ) );
|
|
77
|
+
}
|
|
78
|
+
const FUNCTION_NAME = 'Vector';
|
|
79
|
+
const DEFAULT_OPTIONS = {
|
|
80
|
+
dimensions: 1,
|
|
81
|
+
defaultValue: 0
|
|
82
|
+
};
|
|
83
|
+
this.dim = [];
|
|
84
|
+
Object.defineProperties( this, {
|
|
85
|
+
//base
|
|
86
|
+
length: {
|
|
87
|
+
get(){ return this.dim.length }
|
|
88
|
+
},
|
|
89
|
+
x: {
|
|
90
|
+
get(){ return this.dim[0]; },
|
|
91
|
+
set( value ){ this.dim[0] = value; }
|
|
92
|
+
},
|
|
93
|
+
y: {
|
|
94
|
+
get(){ return this.dim[1]; },
|
|
95
|
+
set( value ){ this.dim[1] = value; }
|
|
96
|
+
},
|
|
97
|
+
z: {
|
|
98
|
+
get(){ return this.dim[2]; },
|
|
99
|
+
set( value ){ this.dim[2] = value; }
|
|
100
|
+
},
|
|
101
|
+
t: {
|
|
102
|
+
get(){ return this.dim[3]; },
|
|
103
|
+
set( value ){ this.dim[3] = value; }
|
|
104
|
+
},
|
|
105
|
+
array2d: {
|
|
106
|
+
get(){
|
|
107
|
+
return [ this.x, this.y ];
|
|
108
|
+
},
|
|
109
|
+
set( value ){
|
|
110
|
+
this.x = value[0];
|
|
111
|
+
this.y = value[1];
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
array3d: {
|
|
115
|
+
get(){
|
|
116
|
+
return [ this.x, this.y, this.z ];
|
|
117
|
+
},
|
|
118
|
+
set( value ){
|
|
119
|
+
this.array2d = value;
|
|
120
|
+
this.z = value[2];
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
array4d: {
|
|
124
|
+
get(){
|
|
125
|
+
return [ this.x, this.y, this.z, this.t ];
|
|
126
|
+
},
|
|
127
|
+
set( value ){
|
|
128
|
+
this.array3d = value;
|
|
129
|
+
this.t = value[3];
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
vec2d: {
|
|
133
|
+
get(){
|
|
134
|
+
return new Vector( this.array2d );
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
vec3d: {
|
|
138
|
+
get(){
|
|
139
|
+
return new Vector( this.array3d );
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
vec4d: {
|
|
143
|
+
get(){
|
|
144
|
+
return new Vector( this.array4d );
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
//multiple
|
|
148
|
+
r: {
|
|
149
|
+
get(){
|
|
150
|
+
var _return = Number.NaN;
|
|
151
|
+
if( this.length === 2 ){
|
|
152
|
+
_return = this.radius;
|
|
153
|
+
} else{
|
|
154
|
+
_return = this.x;
|
|
155
|
+
}
|
|
156
|
+
return _return;
|
|
157
|
+
},
|
|
158
|
+
set( value ){
|
|
159
|
+
if( this.length === 2 ){
|
|
160
|
+
this.radius = value;
|
|
161
|
+
} else{
|
|
162
|
+
this.x = value;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
a: {
|
|
167
|
+
get(){
|
|
168
|
+
var _return = Number.NaN;
|
|
169
|
+
if( this.length === 2 ){
|
|
170
|
+
_return = this.innerAngle;
|
|
171
|
+
} else{
|
|
172
|
+
_return = this.t;
|
|
173
|
+
}
|
|
174
|
+
return _return;
|
|
175
|
+
},
|
|
176
|
+
set( value ){
|
|
177
|
+
if( this.length === 2 ){
|
|
178
|
+
this.innerAngle = value;
|
|
179
|
+
} else{
|
|
180
|
+
this.t = value;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
w: {
|
|
185
|
+
get(){
|
|
186
|
+
var _return = Number.NaN;
|
|
187
|
+
if( this.length >= 5 ){
|
|
188
|
+
_return = this.t;
|
|
189
|
+
} else{
|
|
190
|
+
_return = this.z;
|
|
191
|
+
}
|
|
192
|
+
return _return;
|
|
193
|
+
},
|
|
194
|
+
set( value ){
|
|
195
|
+
if( this.length >= 5 ){
|
|
196
|
+
this.t = value;
|
|
197
|
+
} else{
|
|
198
|
+
this.z = value;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
h: {
|
|
203
|
+
get(){
|
|
204
|
+
var _return = Number.NaN;
|
|
205
|
+
if( this.length >= 5 ){
|
|
206
|
+
_return = this.dim[4];
|
|
207
|
+
} else{
|
|
208
|
+
_return = this.t;
|
|
209
|
+
}
|
|
210
|
+
return _return;
|
|
211
|
+
},
|
|
212
|
+
set( value ){
|
|
213
|
+
if( this.length >= 5 ){
|
|
214
|
+
this.dim[4] = value;
|
|
215
|
+
} else{
|
|
216
|
+
this.t = value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
d: {
|
|
221
|
+
get(){
|
|
222
|
+
var _return = Number.NaN;
|
|
223
|
+
if( this.length >= 5 ){
|
|
224
|
+
_return = this.dim[5];
|
|
225
|
+
} else{
|
|
226
|
+
_return = this.direction;
|
|
227
|
+
}
|
|
228
|
+
return _return;
|
|
229
|
+
},
|
|
230
|
+
set( value ){
|
|
231
|
+
if( this.length >= 5 ){
|
|
232
|
+
this.dim[5] = value;
|
|
233
|
+
} else{
|
|
234
|
+
console.log( `Vector:d:set before ${this.direction} value ${value}` );
|
|
235
|
+
this.direction = value;
|
|
236
|
+
console.log( `Vector:d:set after ${this.direction}` );
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
//derived
|
|
241
|
+
radius: {
|
|
242
|
+
get(){
|
|
243
|
+
var _return = Number.NaN;
|
|
244
|
+
if( this.length >= 2 ){
|
|
245
|
+
_return = ((this.x**2) + (this.y**2))**(1/2);
|
|
246
|
+
} else{
|
|
247
|
+
_return = this.x;
|
|
248
|
+
}
|
|
249
|
+
return _return;
|
|
250
|
+
},
|
|
251
|
+
set( value ){
|
|
252
|
+
var radius = this.radius;
|
|
253
|
+
this.x *= value / radius;
|
|
254
|
+
this.y *= value / radius;
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
magnitude: {
|
|
258
|
+
get(){
|
|
259
|
+
var ans = this.radius;
|
|
260
|
+
for( var i = 2; i < this.length; i++ ){
|
|
261
|
+
ans = ((ans**2) + (this.dim[i]**2))**(1/2);
|
|
262
|
+
}
|
|
263
|
+
return ans;
|
|
264
|
+
},
|
|
265
|
+
set( value ){
|
|
266
|
+
this.scale( value / this.magnitude );
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
innerAngle: {
|
|
270
|
+
get(){
|
|
271
|
+
//return Math.atan( Math.abs(this.y) / Math.abs(this.x) );
|
|
272
|
+
return this.angleRadiansEastAnti;
|
|
273
|
+
},
|
|
274
|
+
set( value ){
|
|
275
|
+
this.angleRadiansEastAnti = value;
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
cartesianAngle: {
|
|
279
|
+
get(){
|
|
280
|
+
return Vector.cartesianAngle( this );
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
area: {
|
|
284
|
+
get(){
|
|
285
|
+
var _return = Number.NaN;
|
|
286
|
+
if( this.length >= 4 ){
|
|
287
|
+
_return = (this.z * this.t);
|
|
288
|
+
} else if( this.length >= 2 ){
|
|
289
|
+
_return = (this.x * this.y);
|
|
290
|
+
}
|
|
291
|
+
return _return;
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
volume: {
|
|
295
|
+
get(){
|
|
296
|
+
var _return = Number.NaN;
|
|
297
|
+
if( this.length >=6 ){
|
|
298
|
+
_return = (this.w * this.h * this.d);
|
|
299
|
+
} else if( this.length >= 3 ){
|
|
300
|
+
_return = ( this.x * this.y * this.z );
|
|
301
|
+
}
|
|
302
|
+
return _return;
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
centre: {
|
|
306
|
+
get(){
|
|
307
|
+
var _return = Number.NaN;
|
|
308
|
+
if( this.length >= 6 ){
|
|
309
|
+
_return = new Vector( (this.x + (this.w / 2)), (this.y + (this.h / 2)), (this.z + (this.d / 2)) );
|
|
310
|
+
} else if( this.length >= 4 ){
|
|
311
|
+
_return = new Vector( (this.x + (this.w / 2)), (this.y + (this.h / 2)) );
|
|
312
|
+
} else if( this.length == 3 ){
|
|
313
|
+
_return = new Vector( (this.x / 2), (this.y / 2), (this.z / 2) );
|
|
314
|
+
} else if( this.length == 2 ){
|
|
315
|
+
_return = new Vector( (this.x / 2), (this.y / 2) );
|
|
316
|
+
}
|
|
317
|
+
return _return;
|
|
318
|
+
},
|
|
319
|
+
set( vector ){
|
|
320
|
+
if( this.length >= 4 ){
|
|
321
|
+
this.x = (vector.x - (this.w / 2));
|
|
322
|
+
this.y = (vector.y - (this.h / 2));
|
|
323
|
+
if( this.length >= 6 ) this.z = (vector.z - (this.d / 2));
|
|
324
|
+
} else{
|
|
325
|
+
this.x = vector.x * 2;
|
|
326
|
+
this.y = vector.y * 2;
|
|
327
|
+
if( this.length === 3 ) this.z = vector.z * 2;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
lowerBound: {
|
|
332
|
+
get(){ return Math.min( this.x, this.y ); },
|
|
333
|
+
},
|
|
334
|
+
upperBound: {
|
|
335
|
+
get(){ return Math.max( this.x, this.y ); },
|
|
336
|
+
},
|
|
337
|
+
lowest: { // Closest to -Inifinity
|
|
338
|
+
get(){ return Math.min( ...this.dim ); }
|
|
339
|
+
},
|
|
340
|
+
highest: { // Closest to +Inifinity
|
|
341
|
+
get(){ return Math.max( ...this.dim ); }
|
|
342
|
+
},
|
|
343
|
+
smallest: { //Closest to 0
|
|
344
|
+
get(){
|
|
345
|
+
var closest = this.x;
|
|
346
|
+
for( let i = 1; i < this.length; i++ ){
|
|
347
|
+
if( Math.abs( closest ) > Math.abs( this.dim[i] ) ){
|
|
348
|
+
closest = this.dim[i];
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return closest;
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
largest: {
|
|
355
|
+
get(){
|
|
356
|
+
var closest = this.x;
|
|
357
|
+
for( let i = 1; i < this.length; i++ ){
|
|
358
|
+
if( Math.abs( closest ) < Math.abs( this.dim[i] ) ){
|
|
359
|
+
closest = this.dim[i];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return closest;
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
//alias
|
|
366
|
+
dimensions: {
|
|
367
|
+
get(){ return this.length; }
|
|
368
|
+
},
|
|
369
|
+
dims: {
|
|
370
|
+
get(){ return this.length; }
|
|
371
|
+
},
|
|
372
|
+
m: {
|
|
373
|
+
get(){ return this.magnitude; },
|
|
374
|
+
set( value ){
|
|
375
|
+
this.magnitude = value;
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
direction: {
|
|
379
|
+
get(){ return this.angleDegreesNorthClockwise; },
|
|
380
|
+
set( value ){
|
|
381
|
+
console.log( `Vector:direction:set before=${this.angleDegreesNorthClockwise} value=${value}` );
|
|
382
|
+
this.angleDegreesNorthClockwise = value;
|
|
383
|
+
console.log( `Vector:direction:set after=${this.angleDegreesNorthClockwise}` );
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
g: {
|
|
387
|
+
get(){ return this.y; },
|
|
388
|
+
set( value ){ this.y = value; }
|
|
389
|
+
},
|
|
390
|
+
b: {
|
|
391
|
+
get(){ return this.z; },
|
|
392
|
+
set( value ){ this.z = value; }
|
|
393
|
+
},
|
|
394
|
+
start: {
|
|
395
|
+
get(){ return this.x; },
|
|
396
|
+
set( value ){ this.x = value; }
|
|
397
|
+
},
|
|
398
|
+
end: {
|
|
399
|
+
get(){ return this.y; },
|
|
400
|
+
set( value ){ this.y = value; }
|
|
401
|
+
},
|
|
402
|
+
//other forms
|
|
403
|
+
array: {
|
|
404
|
+
get(){ return this.dim; }
|
|
405
|
+
},
|
|
406
|
+
rect: {
|
|
407
|
+
get(){
|
|
408
|
+
return { x: this.x, y: this.y, w: this.w, h: this.h };
|
|
409
|
+
},
|
|
410
|
+
set( value ){
|
|
411
|
+
this.x = value.x;
|
|
412
|
+
this.y = value.y;
|
|
413
|
+
this.w = value.w;
|
|
414
|
+
this.h = value.h;
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
prism: {
|
|
418
|
+
get(){
|
|
419
|
+
return { x: this.x, y: this.y, z: this.z, w: this.w, h: this.h, d: this.d };
|
|
420
|
+
},
|
|
421
|
+
set( value ){
|
|
422
|
+
this.x = value.x;
|
|
423
|
+
this.y = value.y;
|
|
424
|
+
this.z = value.z;
|
|
425
|
+
this.w = value.w;
|
|
426
|
+
this.h = value.h;
|
|
427
|
+
this.d = value.d;
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
xy: {
|
|
431
|
+
get(){
|
|
432
|
+
return { x: this.x, y: this.y };
|
|
433
|
+
},
|
|
434
|
+
set( value ){
|
|
435
|
+
this.x = value.x;
|
|
436
|
+
this.y = value.y;
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
xyz: {
|
|
440
|
+
get(){
|
|
441
|
+
return { x: this.x, y: this.y, z: this.z };
|
|
442
|
+
},
|
|
443
|
+
set( value ){
|
|
444
|
+
this.x = value.x;
|
|
445
|
+
this.y = value.y;
|
|
446
|
+
this.z = value.z;
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
rgb: {
|
|
450
|
+
get(){
|
|
451
|
+
return { r: this.x, g: this.y, b: this.z };
|
|
452
|
+
},
|
|
453
|
+
set( value ){
|
|
454
|
+
this.r = value.r;
|
|
455
|
+
this.g = value.g;
|
|
456
|
+
this.b = value.b;
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
rgba: {
|
|
460
|
+
get(){
|
|
461
|
+
return { r: this.x, g: this.y, b: this.z, a: this.t };
|
|
462
|
+
},
|
|
463
|
+
set( value ){
|
|
464
|
+
this.r = value.r;
|
|
465
|
+
this.g = value.g;
|
|
466
|
+
this.b = value.b;
|
|
467
|
+
this.a = value.a;
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
origin: {
|
|
471
|
+
get(){
|
|
472
|
+
var _return = Number.NaN;
|
|
473
|
+
if( this.length === 3 || this.length > 4 ){
|
|
474
|
+
_return = this.xyz;
|
|
475
|
+
} else{
|
|
476
|
+
_return = this.xy;
|
|
477
|
+
}
|
|
478
|
+
return _return;
|
|
479
|
+
},
|
|
480
|
+
set( value ){
|
|
481
|
+
if( this.length === 3 || this.length > 4 ){
|
|
482
|
+
this.xyz = value;
|
|
483
|
+
} else{
|
|
484
|
+
this.xy = value;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
offset: {
|
|
489
|
+
get(){
|
|
490
|
+
var _return = Number.NaN;
|
|
491
|
+
if( this.length === 4 ){
|
|
492
|
+
_return = { x: this.z, y: this.t };
|
|
493
|
+
} else{
|
|
494
|
+
_return = { x: this.w, y: this.h, d: this.d };
|
|
495
|
+
}
|
|
496
|
+
return _return;
|
|
497
|
+
},
|
|
498
|
+
set( value ){
|
|
499
|
+
if( this.length === 4 ){
|
|
500
|
+
this.z = value.x;
|
|
501
|
+
this.t = value.y;
|
|
502
|
+
} else{
|
|
503
|
+
this.w = value.x;
|
|
504
|
+
this.h = value.y;
|
|
505
|
+
this.d = ( value.d !== undefined )? value.d : value.z; //Accept either the getter's own key (`d`) or the spatial alias (`z`).
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
},
|
|
509
|
+
line: {
|
|
510
|
+
get(){
|
|
511
|
+
return { start: this.origin, end: this.offset };
|
|
512
|
+
},
|
|
513
|
+
set( value ){
|
|
514
|
+
this.origin = value.start;
|
|
515
|
+
this.offset = value.end;
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
euclidian: {
|
|
519
|
+
get(){
|
|
520
|
+
return { magnitude: this.magnitude, direction: this.innerAngle }
|
|
521
|
+
},
|
|
522
|
+
set( value ){
|
|
523
|
+
var mag = value.m || value.magnitude || value.radius;
|
|
524
|
+
var dir = value.d || value.direction || value.angle;
|
|
525
|
+
this.x = (Math.cos(dir) * mag );
|
|
526
|
+
this.y = (Math.sin(dir) * mag );
|
|
527
|
+
}
|
|
528
|
+
},
|
|
529
|
+
sortedLowestFirst: {
|
|
530
|
+
get(){
|
|
531
|
+
return new Vector( this.toSorted( Vector.SORT_LOWEST_FIRST_FUNCTION ) );
|
|
532
|
+
}
|
|
533
|
+
},
|
|
534
|
+
sortedHighestFirst: {
|
|
535
|
+
get(){
|
|
536
|
+
return new Vector( this.toSorted( Vector.SORT_HIGHEST_FIRST_FUNCTION ) );
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
sortedSmallestFirst: {
|
|
540
|
+
get(){
|
|
541
|
+
return new Vector( this.toSorted( Vector.SORT_SMALLEST_FIRST_FUNCTION ) );
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
sortedLargestFirst: {
|
|
545
|
+
get(){
|
|
546
|
+
return new Vector( this.toSorted( Vector.SORT_LARGEST_FIRST_FUNCTION ) );
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
angleRadiansEastAnti: {
|
|
550
|
+
get(){
|
|
551
|
+
return Math.atan2( this.y, this.x );
|
|
552
|
+
},
|
|
553
|
+
set( value ){
|
|
554
|
+
var radius = this.radius;
|
|
555
|
+
console.log( `Vector:angleRadiansEastAnti:set before x=${this.x} y=${this.y} radius=${radius} value=${value}` );
|
|
556
|
+
this.x = radius * Math.cos( value );
|
|
557
|
+
console.log( `Vector:angleRadiansEastAnti:set during x=${this.x} y=${this.y} radius=${radius} value=${value}` );
|
|
558
|
+
this.y = radius * Math.sin( value );
|
|
559
|
+
console.log( `Vector:angleRadiansEastAnti:set after x=${this.x} y=${this.y} radius=${radius} value=${value}` );
|
|
560
|
+
}
|
|
561
|
+
},
|
|
562
|
+
angleRadiansNorthClockwise: {
|
|
563
|
+
get(){
|
|
564
|
+
return Math.atan2( this.x, this.y );
|
|
565
|
+
},
|
|
566
|
+
set( value ){
|
|
567
|
+
var normalised_value = overflow( HALF_PI - value, 0, TAU );
|
|
568
|
+
console.log( `Vector:angleRadiansNorthClockwise:set before=${this.angleRadiansEastAnti} value=${value} normalised=${normalised_value}` );
|
|
569
|
+
this.angleRadiansEastAnti = normalised_value;
|
|
570
|
+
console.log( `Vector:angleRadiansNorthClockwise:set after=${this.angleRadiansEastAnti}` );
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
angleDegreesEastAnti: {
|
|
574
|
+
get(){
|
|
575
|
+
return this.angleRadiansEastAnti * RAD_TO_DEG;
|
|
576
|
+
},
|
|
577
|
+
set( value ){
|
|
578
|
+
console.log( `Vector:angleDegreesEastAnti:set before=${this.angleDegreesEastAnti} degrees=${value} radians=${value * DEG_TO_RAD}` );
|
|
579
|
+
this.angleRadiansEastAnti = value * DEG_TO_RAD;
|
|
580
|
+
console.log( `Vector:angleDegreesEastAnti:set after=${this.angleDegreesEastAnti}` );
|
|
581
|
+
}
|
|
582
|
+
},
|
|
583
|
+
angleDegreesNorthClockwise: {
|
|
584
|
+
get(){
|
|
585
|
+
//return normalisePolarAngle( 450/*(360+90)*/ - this.angleDegreesEastAnti );
|
|
586
|
+
return this.angleRadiansNorthClockwise * RAD_TO_DEG;
|
|
587
|
+
},
|
|
588
|
+
set( value ){
|
|
589
|
+
console.log( `Vector:angleDegreesNorthClockwise:set before=${this.angleRadiansNorthClockwise} degrees=${value} radians=${value * DEG_TO_RAD}` );
|
|
590
|
+
this.angleRadiansNorthClockwise = value * DEG_TO_RAD;
|
|
591
|
+
console.log( `Vector:angleDegreesNorthClockwise:set after=${this.angleRadiansNorthClockwise}` );
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
} );
|
|
595
|
+
var options = null;
|
|
596
|
+
if( arguments.length === 1 ){
|
|
597
|
+
if( Vector.isVector( input_options ) === true ){
|
|
598
|
+
this.dim = [ ...input_options.dim ];
|
|
599
|
+
} else if( Array.isArray( input_options ) === true ){
|
|
600
|
+
this.dim = input_options;
|
|
601
|
+
} else if( typeof(input_options) === 'number' ){
|
|
602
|
+
assertGreaterThan( input_options, 0 );
|
|
603
|
+
assertLessThan( input_options, Number.MAX_SAFE_INTEGER );
|
|
604
|
+
for( var i = 0; i < input_options; i++ ){
|
|
605
|
+
this.dim.push( 0 );
|
|
606
|
+
}
|
|
607
|
+
} else{
|
|
608
|
+
options = deriveOptions( input_options, DEFAULT_OPTIONS );
|
|
609
|
+
assertGreaterThan( options.dimensions, 0 );
|
|
610
|
+
assertLessThan( options.dimensions, Number.MAX_SAFE_INTEGER );
|
|
611
|
+
for( var i = 0; i < options.dimensions; i++ ){
|
|
612
|
+
this.dim.push( options.defaultValue );
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
} else{
|
|
616
|
+
for( var i = 0; i < arguments.length; i++ ){
|
|
617
|
+
deepStrictEqual( typeof( arguments[i] ), 'number', 'Multiple parametres given but some are not numbers.' );
|
|
618
|
+
this.dim.push( arguments[i] );
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
/*for( const key of Object.keys(options) ){
|
|
622
|
+
this[key] = options[key];
|
|
623
|
+
}*/
|
|
624
|
+
for( const key of Object.getOwnPropertyNames( Array.prototype ) ){
|
|
625
|
+
if( typeof( Array.prototype[key] ) === 'function' && !( key in this ) ) this[key] = Array.prototype[key].bind( this.dim ); //Inherit array methods onto Vector, without shadowing Vector's own members
|
|
626
|
+
}
|
|
627
|
+
return this;
|
|
628
|
+
}
|
|
629
|
+
Vector.prototype.toString = function( concise = false ){
|
|
630
|
+
var _return = '[';
|
|
631
|
+
if( concise !== true ){
|
|
632
|
+
_return += ' ';
|
|
633
|
+
}
|
|
634
|
+
for( var i = 0; i < this.dim.length; i++ ){
|
|
635
|
+
_return += this.dim[i];
|
|
636
|
+
if( i < ( this.dim.length - 1 ) ){
|
|
637
|
+
_return += ',';
|
|
638
|
+
}
|
|
639
|
+
if( concise !== true ){
|
|
640
|
+
_return += ' ';
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
_return += ']';
|
|
644
|
+
return _return;
|
|
645
|
+
}
|
|
646
|
+
Vector.prototype.add = function( otherVector ){
|
|
647
|
+
var vector = otherVector;
|
|
648
|
+
if( !( otherVector instanceof Vector ) ){
|
|
649
|
+
vector = new Vector( otherVector );
|
|
650
|
+
}
|
|
651
|
+
var limit = Math.min( this.dim.length, vector.dim.length );
|
|
652
|
+
for( var i = 0; i < limit; i++ ){
|
|
653
|
+
this.dim[i] += vector.dim[i];
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
Vector.prototype.sub = function( otherVector ){
|
|
657
|
+
var vector = otherVector;
|
|
658
|
+
if( !( otherVector instanceof Vector ) ){
|
|
659
|
+
vector = new Vector( otherVector );
|
|
660
|
+
}
|
|
661
|
+
var limit = Math.min( this.dim.length, vector.dim.length );
|
|
662
|
+
for( var i = 0; i < limit; i++ ){
|
|
663
|
+
this.dim[i] -= vector.dim[i];
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
Vector.prototype.scale = function( scalar ){
|
|
667
|
+
for( var i = 0; i < this.dim.length; i++ ){
|
|
668
|
+
this.dim[i] *= scalar;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
Vector.prototype.dot = function( vector ){
|
|
672
|
+
var ans = 0;
|
|
673
|
+
var limit = Math.min( this.dim.length, vector.dim.length );
|
|
674
|
+
for( var i = 0; i < limit; i++ ){
|
|
675
|
+
ans += (this.dim[i] * vector.dim[i]);
|
|
676
|
+
}
|
|
677
|
+
return ans;
|
|
678
|
+
}
|
|
679
|
+
Vector.prototype.closestTo = function( point ){
|
|
680
|
+
var closest = this.x;
|
|
681
|
+
var difference = Math.abs( closest - point );
|
|
682
|
+
for( let i = 1; i < this.length; i++ ){
|
|
683
|
+
if( Math.abs( this.dim[i] - point ) < difference ){
|
|
684
|
+
closest = this.dim[i];
|
|
685
|
+
difference = Math.abs( closest - point );
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return closest;
|
|
689
|
+
}
|
|
690
|
+
Vector.prototype.sortComponentsLowestFirst = function(){
|
|
691
|
+
this.dim.sort( ( a, b ) => {
|
|
692
|
+
return a - b;
|
|
693
|
+
} );
|
|
694
|
+
return this;
|
|
695
|
+
}
|
|
696
|
+
Vector.prototype.sortComponentsHighestFirst = function(){
|
|
697
|
+
this.dim.sort( ( a, b ) => {
|
|
698
|
+
return b - a;
|
|
699
|
+
} );
|
|
700
|
+
return this;
|
|
701
|
+
}
|
|
702
|
+
Vector.prototype.sortComponentsSmallestFirst = function(){
|
|
703
|
+
this.dim.sort( ( a, b ) => {
|
|
704
|
+
return Math.abs(a) - Math.abs(b);
|
|
705
|
+
} );
|
|
706
|
+
return this;
|
|
707
|
+
}
|
|
708
|
+
Vector.prototype.sortComponentsLargestFirst = function(){
|
|
709
|
+
this.dim.sort( ( a, b ) => {
|
|
710
|
+
return Math.abs(b) - Math.abs(a);
|
|
711
|
+
} );
|
|
712
|
+
return this;
|
|
713
|
+
}
|
|
714
|
+
const COMPASS_ENUM = {};
|
|
715
|
+
Object.defineProperties( COMPASS_ENUM, {
|
|
716
|
+
NORTHWEST: {
|
|
717
|
+
value: -3,
|
|
718
|
+
enumerable: true
|
|
719
|
+
},
|
|
720
|
+
NORTH: {
|
|
721
|
+
value: -2,
|
|
722
|
+
enumerable: true
|
|
723
|
+
},
|
|
724
|
+
Y_POSITIVE: {
|
|
725
|
+
value: -2,
|
|
726
|
+
enumerable: true
|
|
727
|
+
},
|
|
728
|
+
NORTHEAST: {
|
|
729
|
+
value: -1,
|
|
730
|
+
enumerable: true
|
|
731
|
+
},
|
|
732
|
+
EAST: {
|
|
733
|
+
value: 0,
|
|
734
|
+
enumerable: true
|
|
735
|
+
},
|
|
736
|
+
X_POSITIVE: {
|
|
737
|
+
value: 0,
|
|
738
|
+
enumerable: true
|
|
739
|
+
},
|
|
740
|
+
SOUTHEAST: {
|
|
741
|
+
value: 1,
|
|
742
|
+
enumerable: true
|
|
743
|
+
},
|
|
744
|
+
SOUTH: {
|
|
745
|
+
value: 2,
|
|
746
|
+
enumerable: true
|
|
747
|
+
},
|
|
748
|
+
Y_NEGATIVE: {
|
|
749
|
+
value: 2,
|
|
750
|
+
enumerable: true
|
|
751
|
+
},
|
|
752
|
+
SOUTHWEST: {
|
|
753
|
+
value: 3,
|
|
754
|
+
enumerable: true
|
|
755
|
+
},
|
|
756
|
+
WEST: {
|
|
757
|
+
value: 4,
|
|
758
|
+
enumerable: true
|
|
759
|
+
},
|
|
760
|
+
X_NEGATIVE: {
|
|
761
|
+
value: 4,
|
|
762
|
+
enumerable: true
|
|
763
|
+
}
|
|
764
|
+
} );
|
|
765
|
+
const COMPASS_UNIT_VECTORS = {};
|
|
766
|
+
Object.defineProperties( COMPASS_UNIT_VECTORS, {
|
|
767
|
+
NORTHWEST: {
|
|
768
|
+
get(){ return new Vector( -SQRT1_2, SQRT1_2 ); },
|
|
769
|
+
enumerable: true
|
|
770
|
+
},
|
|
771
|
+
NORTH: {
|
|
772
|
+
get(){ return new Vector( 0, 1 ); },
|
|
773
|
+
enumerable: true
|
|
774
|
+
},
|
|
775
|
+
NORTHEAST: {
|
|
776
|
+
get(){ return new Vector( SQRT1_2, SQRT1_2 ); },
|
|
777
|
+
enumerable: true
|
|
778
|
+
},
|
|
779
|
+
EAST: {
|
|
780
|
+
get(){ return new Vector( 1, 0 ); },
|
|
781
|
+
enumerable: true
|
|
782
|
+
},
|
|
783
|
+
SOUTHEAST: {
|
|
784
|
+
get(){ return new Vector( SQRT1_2, -SQRT1_2 ); },
|
|
785
|
+
enumerable: true
|
|
786
|
+
},
|
|
787
|
+
SOUTH: {
|
|
788
|
+
get(){ return new Vector( 0, -1 ); },
|
|
789
|
+
enumerable: true
|
|
790
|
+
},
|
|
791
|
+
SOUTHWEST: {
|
|
792
|
+
get(){ return new Vector( -SQRT1_2, -SQRT1_2 ); },
|
|
793
|
+
enumerable: true
|
|
794
|
+
},
|
|
795
|
+
WEST: {
|
|
796
|
+
get(){ return new Vector( -1, 0 ); },
|
|
797
|
+
enumerable: true
|
|
798
|
+
}
|
|
799
|
+
} );
|
|
800
|
+
Object.defineProperties( Vector, {
|
|
801
|
+
PI: {
|
|
802
|
+
value: 3.14159265358979323
|
|
803
|
+
},
|
|
804
|
+
COMPASS_ENUM: {
|
|
805
|
+
value: Object.freeze( COMPASS_ENUM ),
|
|
806
|
+
enumerable: true
|
|
807
|
+
},
|
|
808
|
+
COMPASS_UNIT_VECTORS: {
|
|
809
|
+
value: Object.freeze( COMPASS_UNIT_VECTORS ),
|
|
810
|
+
enumerable: true
|
|
811
|
+
},
|
|
812
|
+
/*ORIGIN_ARRAY_6D: {
|
|
813
|
+
value: [ 0, 0, 0, 0, 0, 0 ]
|
|
814
|
+
},
|
|
815
|
+
INDENTITY_ARRAY_6D:{
|
|
816
|
+
value: [ 1, 1, 1, 1, 1, 1 ]
|
|
817
|
+
},
|
|
818
|
+
ORIGIN_VECTOR_1D: {
|
|
819
|
+
value: new Vector( [ 0 ] )
|
|
820
|
+
},
|
|
821
|
+
IDENTITY_VECTOR_1D: {
|
|
822
|
+
value: new Vector( [ 1 ] )
|
|
823
|
+
},
|
|
824
|
+
ORIGIN_VECTOR_2D: {
|
|
825
|
+
value: new Vector( [ 0, 0 ] )
|
|
826
|
+
},
|
|
827
|
+
IDENTITY_VECTOR_2D: {
|
|
828
|
+
value: new Vector( [ 1, 1 ] )
|
|
829
|
+
},
|
|
830
|
+
ORIGIN_VECTOR_3D: {
|
|
831
|
+
value: new Vector( [ 0, 0, 0 ] )
|
|
832
|
+
},
|
|
833
|
+
IDENTITY_VECTOR_3D: {
|
|
834
|
+
value: new Vector( [ 1, 1, 1 ] )
|
|
835
|
+
},
|
|
836
|
+
ORIGIN_VECTOR_4D: {
|
|
837
|
+
value: new Vector( [ 0, 0, 0, 0 ] )
|
|
838
|
+
},
|
|
839
|
+
IDENTITY_VECTOR_4D: {
|
|
840
|
+
value: new Vector( [ 1, 1, 1, 1 ] )
|
|
841
|
+
},
|
|
842
|
+
ORIGIN_VECTOR_5D: {
|
|
843
|
+
value: new Vector( [ 0, 0, 0, 0, 0 ] )
|
|
844
|
+
},
|
|
845
|
+
IDENTITY_VECTOR_5D: {
|
|
846
|
+
value: new Vector( [ 1, 1, 1, 1, 1 ] )
|
|
847
|
+
},
|
|
848
|
+
ORIGIN_VECTOR_6D: {
|
|
849
|
+
value: new Vector( [ 0, 0, 0, 0, 0, 0 ] )
|
|
850
|
+
},
|
|
851
|
+
IDENTITY_VECTOR_6D: {
|
|
852
|
+
value: new Vector( [ 1, 1, 1, 1, 1, 1 ] )
|
|
853
|
+
},
|
|
854
|
+
SORT_LOWEST_FIRST_FUNCTION: {
|
|
855
|
+
value: ( a, b ) => {
|
|
856
|
+
return a - b;
|
|
857
|
+
}
|
|
858
|
+
},
|
|
859
|
+
SORT_HIGHEST_FIRST_FUNCTION: {
|
|
860
|
+
value: ( a, b ) => {
|
|
861
|
+
return b - a;
|
|
862
|
+
}
|
|
863
|
+
},
|
|
864
|
+
SORT_SMALLEST_FIRST_FUNCTION: {
|
|
865
|
+
value: ( a, b ) => {
|
|
866
|
+
return Math.abs(a) - Math.abs(b);
|
|
867
|
+
}
|
|
868
|
+
},
|
|
869
|
+
SORT_LARGEST_FIRST_FUNCTION: {
|
|
870
|
+
value: ( a, b ) => {
|
|
871
|
+
return Math.abs(b) - Math.abs(a);
|
|
872
|
+
}
|
|
873
|
+
}*/
|
|
874
|
+
} );
|
|
875
|
+
// Sort comparator functions, used by the `sorted*` accessors and by the `sortComponents*` prototype methods.
|
|
876
|
+
Object.defineProperties( Vector, {
|
|
877
|
+
SORT_LOWEST_FIRST_FUNCTION: {
|
|
878
|
+
value: ( a, b ) => {
|
|
879
|
+
return a - b;
|
|
880
|
+
},
|
|
881
|
+
enumerable: true
|
|
882
|
+
},
|
|
883
|
+
SORT_HIGHEST_FIRST_FUNCTION: {
|
|
884
|
+
value: ( a, b ) => {
|
|
885
|
+
return b - a;
|
|
886
|
+
},
|
|
887
|
+
enumerable: true
|
|
888
|
+
},
|
|
889
|
+
SORT_SMALLEST_FIRST_FUNCTION: {
|
|
890
|
+
value: ( a, b ) => {
|
|
891
|
+
return Math.abs(a) - Math.abs(b);
|
|
892
|
+
},
|
|
893
|
+
enumerable: true
|
|
894
|
+
},
|
|
895
|
+
SORT_LARGEST_FIRST_FUNCTION: {
|
|
896
|
+
value: ( a, b ) => {
|
|
897
|
+
return Math.abs(b) - Math.abs(a);
|
|
898
|
+
},
|
|
899
|
+
enumerable: true
|
|
900
|
+
}
|
|
901
|
+
} );
|
|
902
|
+
// Vector static methods
|
|
903
|
+
Vector.isVector = function( vec ){
|
|
904
|
+
var _return = false;
|
|
905
|
+
if( vec instanceof Vector ) _return = true;
|
|
906
|
+
return _return;
|
|
907
|
+
}
|
|
908
|
+
Vector.add = function( vecA, vecB ){
|
|
909
|
+
var arr = [];
|
|
910
|
+
const limit = Math.min( vecA.dim.length, vecB.dim.length );
|
|
911
|
+
for( var i = 0; i < limit; i++ ){
|
|
912
|
+
arr.push( vecA.dim[i] + vecB.dim[i] );
|
|
913
|
+
}
|
|
914
|
+
return new Vector( arr );
|
|
915
|
+
}
|
|
916
|
+
Vector.sub = function( vecA, vecB ){
|
|
917
|
+
var arr = [];
|
|
918
|
+
const limit = Math.min( vecA.dim.length, vecB.dim.length );
|
|
919
|
+
for( var i = 0; i < limit; i++ ){
|
|
920
|
+
arr.push( vecA.dim[i] - vecB.dim[i] );
|
|
921
|
+
}
|
|
922
|
+
return new Vector( arr );
|
|
923
|
+
}
|
|
924
|
+
Vector.dot = function( vecA, vecB ){
|
|
925
|
+
var ans = 0;
|
|
926
|
+
const limit = Math.min( vecA.dim.length, vecB.dim.length );
|
|
927
|
+
for( var i = 0; i < limit; i++ ){
|
|
928
|
+
ans += (vecA.dim[i] * vecB.dim[i]);
|
|
929
|
+
}
|
|
930
|
+
return ans;
|
|
931
|
+
}
|
|
932
|
+
Vector.scale = function( vecA, scalar ){
|
|
933
|
+
var arr = [];
|
|
934
|
+
for( var i = 0; i < vecA.dim.length; i++ ){
|
|
935
|
+
arr.push( vecA.dim[i] * scalar );
|
|
936
|
+
}
|
|
937
|
+
return new Vector( arr );
|
|
938
|
+
}
|
|
939
|
+
Vector.cartesianAngle = function( vector ){
|
|
940
|
+
var _return = Number.NaN;
|
|
941
|
+
const x = vector.dim[0];
|
|
942
|
+
const y = vector.dim[1];
|
|
943
|
+
if( typeof(x) === 'number' && typeof(y) === 'number' && !( x === 0 && y === 0 ) ){
|
|
944
|
+
_return = Math.atan2( y, x );
|
|
945
|
+
if( _return < 0 ){
|
|
946
|
+
_return += (2 * Math.PI); //Normalise into [0, 2π).
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return _return;
|
|
950
|
+
}
|
|
951
|
+
function angleNorthClockwise( x, y ){
|
|
952
|
+
return Math.atan2( x, y );
|
|
953
|
+
}
|
|
954
|
+
function angleEastAnti( x, y ){
|
|
955
|
+
return Math.atan2( y, x );
|
|
956
|
+
}
|
|
957
|
+
Vector.angleBetween = function( vecA, vecB ){
|
|
958
|
+
return Math.acos( Vector.dot( vecA, vecB ) / ( vecA.magnitude * vecB.magnitude ) );
|
|
959
|
+
}
|
|
960
|
+
Vector.normal = function( vecA, vecB ){
|
|
961
|
+
var _return = -1;
|
|
962
|
+
// Compass-bearing difference in degrees, wrapped into [0, 360); a positive
|
|
963
|
+
// difference (clockwise) of less than a half-turn is a positive normal.
|
|
964
|
+
var angle_difference = normalisePolarAngle( vecB.angleDegreesNorthClockwise - vecA.angleDegreesNorthClockwise );
|
|
965
|
+
if( angle_difference < 180 ){
|
|
966
|
+
_return = 1;
|
|
967
|
+
}
|
|
968
|
+
return _return;
|
|
969
|
+
}
|
|
970
|
+
Vector.cross = function( vecA, vecB ){
|
|
971
|
+
//console.log(`${vecA} ${vecB}`);
|
|
972
|
+
var normal = Vector.normal( vecA, vecB );
|
|
973
|
+
var angle = Vector.angleBetween( vecA, vecB );
|
|
974
|
+
//console.log( normal, angle );
|
|
975
|
+
return new Vector( [ 0, 0, ( vecA.magnitude * vecB.magnitude * Math.sin( angle ) * normal ) ] );
|
|
976
|
+
};
|
|
977
|
+
Vector.slice = function( vector, start, end ){
|
|
978
|
+
return new Vector( vector.dim.slice( start, end ) );
|
|
979
|
+
};
|
|
980
|
+
Vector.reduce = function( vector, dimensions ){
|
|
981
|
+
return new Vector( vector.dim.slice( 0, dimensions ) );
|
|
982
|
+
};
|
|
983
|
+
Vector.pythagoreanDistance = function( vecA, vecB ){
|
|
984
|
+
const x = vecB.x - vecA.x;
|
|
985
|
+
const y = vecB.y - vecA.y;
|
|
986
|
+
return Math.sqrt( x**2 + y**2 ); //Pythagorean identity
|
|
987
|
+
}
|
|
988
|
+
Vector.equal = function( vecA, vecB ){
|
|
989
|
+
var _return = true;
|
|
990
|
+
const limit = Math.min( vecA.length, vecB.length );
|
|
991
|
+
for( var i = 0; i < limit; i++ ){
|
|
992
|
+
if( vecA.dim[i] != vecB.dim[i] ){
|
|
993
|
+
_return = false;
|
|
994
|
+
i = limit;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return _return;
|
|
998
|
+
}
|
|
999
|
+
Vector.assertEqual = function( vecA, vecB ){
|
|
1000
|
+
var _return = Vector.equal( vecA, vecB );
|
|
1001
|
+
var return_error = null;
|
|
1002
|
+
if( _return === false ){
|
|
1003
|
+
return_error = new Error( `vecA (${vecA}) is not equal to vecB (${vecB})` );
|
|
1004
|
+
throw return_error;
|
|
1005
|
+
}
|
|
1006
|
+
return _return;
|
|
1007
|
+
}
|
|
1008
|
+
Vector.originFromDimensions = function( dimensions = 1 ){
|
|
1009
|
+
var _return = new Vector( dimensions );
|
|
1010
|
+
return _return;
|
|
1011
|
+
}
|
|
1012
|
+
Vector.identityFromDimensions = function( dimensions = 1 ){
|
|
1013
|
+
var _return = new Vector( { dimensions: dimensions, defaultValue: 1 } );
|
|
1014
|
+
return _return;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// Generate static constants doesn't work
|
|
1018
|
+
for( let i = 1; i <= 6; i++ ){
|
|
1019
|
+
const dimensions = i;
|
|
1020
|
+
const origin = [];
|
|
1021
|
+
const identity = [];
|
|
1022
|
+
for( let j = 0; j < dimensions; j++ ){
|
|
1023
|
+
origin.push( 0 );
|
|
1024
|
+
identity.push( 1 );
|
|
1025
|
+
}
|
|
1026
|
+
Object.defineProperty( Vector, `ORIGIN_ARRAY_${dimensions}D`, {
|
|
1027
|
+
get(){ return [ ...origin ]; }
|
|
1028
|
+
} );
|
|
1029
|
+
Object.defineProperty( Vector, `IDENTITY_ARRAY_${dimensions}D`, {
|
|
1030
|
+
get(){ return [ ...identity ]; }
|
|
1031
|
+
} );
|
|
1032
|
+
Object.defineProperty( Vector, `ORIGIN_VECTOR_${dimensions}D`, {
|
|
1033
|
+
get(){
|
|
1034
|
+
console.log( `Vector.ORIGIN_VECTOR_${dimensions}D:get` );
|
|
1035
|
+
assertGreaterThan( dimensions, 0 );
|
|
1036
|
+
assertLessThan( dimensions, 7 );
|
|
1037
|
+
var _return = Vector.originFromDimensions( dimensions );
|
|
1038
|
+
console.log( `Vector.ORIGIN_VECTOR_${dimensions}D:get returned ${_return}` );
|
|
1039
|
+
return _return;
|
|
1040
|
+
}
|
|
1041
|
+
} );
|
|
1042
|
+
Object.defineProperty( Vector, `IDENTITY_VECTOR_${dimensions}D`, {
|
|
1043
|
+
get(){
|
|
1044
|
+
console.log( `Vector.IDENTITY_VECTOR_${dimensions}D:get` );
|
|
1045
|
+
assertGreaterThan( dimensions, 0 );
|
|
1046
|
+
assertLessThan( dimensions, 7 );
|
|
1047
|
+
var _return = Vector.identityFromDimensions( dimensions );
|
|
1048
|
+
console.log( `Vector.IDENTITY_VECTOR_${dimensions}D:get returned ${_return}` );
|
|
1049
|
+
return _return;
|
|
1050
|
+
}
|
|
1051
|
+
} );
|
|
1052
|
+
console.log( `for ${dimensions}: ${Vector[`ORIGIN_ARRAY_${dimensions}D`]} ${Vector[`IDENTITY_ARRAY_${dimensions}D`]} ${Vector[`ORIGIN_VECTOR_${dimensions}D`]} ${Vector[`IDENTITY_VECTOR_${dimensions}D`]}`);
|
|
1053
|
+
}
|