toosoon-utils 3.1.0 → 4.0.1
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/README.md +1 -1
- package/lib/classes/_pool.d.ts +2 -2
- package/lib/classes/_pool.js +22 -50
- package/lib/classes/color-scale.d.ts +1 -1
- package/lib/classes/color-scale.js +67 -88
- package/lib/classes/frame-rate.js +19 -27
- package/lib/colors.d.ts +1 -1
- package/lib/colors.js +85 -123
- package/lib/constants.js +6 -9
- package/lib/dom.js +10 -17
- package/lib/files.js +8 -15
- package/lib/functions.d.ts +1 -1
- package/lib/functions.js +27 -40
- package/lib/geometry.d.ts +11 -11
- package/lib/geometry.js +30 -43
- package/lib/maths.js +22 -51
- package/lib/prng.js +43 -70
- package/lib/query.js +9 -15
- package/lib/random.d.ts +1 -1
- package/lib/random.js +30 -62
- package/lib/strings.js +17 -30
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +6 -6
- package/lib/types.js +1 -2
- package/package.json +3 -1
- package/tsconfig.json +17 -11
package/lib/prng.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.randomGaussian = exports.randomIndex = exports.randomObjectProperty = exports.randomItem = exports.randomHexColor = exports.randomInt = exports.randomFloat = exports.randomSign = exports.randomBoolean = exports.random = exports.xoshiro128ss = exports.jsf32 = exports.mulberry32 = exports.splitmix32 = exports.sfc32 = exports.cyrb128 = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Produce a 128-bit hash value from a seed
|
|
6
3
|
*
|
|
7
4
|
* @param {string} seed Initial seed state
|
|
8
5
|
* @returns {[number, number, number, number]} Hash numbers
|
|
9
6
|
*/
|
|
10
|
-
function cyrb128(seed) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
for (
|
|
7
|
+
export function cyrb128(seed) {
|
|
8
|
+
let h1 = 1779033703;
|
|
9
|
+
let h2 = 3144134277;
|
|
10
|
+
let h3 = 1013904242;
|
|
11
|
+
let h4 = 2773480762;
|
|
12
|
+
for (let i = 0, k; i < seed.length; i++) {
|
|
16
13
|
k = seed.charCodeAt(i);
|
|
17
14
|
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
|
18
15
|
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
|
@@ -25,7 +22,6 @@ function cyrb128(seed) {
|
|
|
25
22
|
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
|
|
26
23
|
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
|
|
27
24
|
}
|
|
28
|
-
exports.cyrb128 = cyrb128;
|
|
29
25
|
// *********************
|
|
30
26
|
// PRNG Algorithms
|
|
31
27
|
// *********************
|
|
@@ -38,12 +34,12 @@ exports.cyrb128 = cyrb128;
|
|
|
38
34
|
* @param {number} d
|
|
39
35
|
* @returns {number} Pseudo-random number
|
|
40
36
|
*/
|
|
41
|
-
function sfc32(a, b, c, d) {
|
|
37
|
+
export function sfc32(a, b, c, d) {
|
|
42
38
|
a >>>= 0;
|
|
43
39
|
b >>>= 0;
|
|
44
40
|
c >>>= 0;
|
|
45
41
|
d >>>= 0;
|
|
46
|
-
|
|
42
|
+
let t = (a + b) | 0;
|
|
47
43
|
a = b ^ (b >>> 9);
|
|
48
44
|
b = (c + (c << 3)) | 0;
|
|
49
45
|
c = (c << 21) | (c >>> 11);
|
|
@@ -52,14 +48,13 @@ function sfc32(a, b, c, d) {
|
|
|
52
48
|
c = (c + t) | 0;
|
|
53
49
|
return (t >>> 0) / 4294967296;
|
|
54
50
|
}
|
|
55
|
-
exports.sfc32 = sfc32;
|
|
56
51
|
/**
|
|
57
52
|
* SplitMix32, Generator with a 32-bit state
|
|
58
53
|
*
|
|
59
54
|
* @param {number} a
|
|
60
55
|
* @returns {number} Pseudo-random number
|
|
61
56
|
*/
|
|
62
|
-
function splitmix32(a) {
|
|
57
|
+
export function splitmix32(a) {
|
|
63
58
|
a |= 0;
|
|
64
59
|
a = (a + 0x9e3779b9) | 0;
|
|
65
60
|
var t = a ^ (a >>> 16);
|
|
@@ -68,48 +63,45 @@ function splitmix32(a) {
|
|
|
68
63
|
t = Math.imul(t, 0x735a2d97);
|
|
69
64
|
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
|
|
70
65
|
}
|
|
71
|
-
exports.splitmix32 = splitmix32;
|
|
72
66
|
/**
|
|
73
67
|
* Mulberry32, Generator with a 32-bit state
|
|
74
68
|
*
|
|
75
69
|
* @param {number} a
|
|
76
70
|
* @returns {number} Pseudo-random number
|
|
77
71
|
*/
|
|
78
|
-
function mulberry32(a) {
|
|
79
|
-
|
|
72
|
+
export function mulberry32(a) {
|
|
73
|
+
let t = (a += 0x6d2b79f5);
|
|
80
74
|
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
81
75
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
82
76
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
83
77
|
}
|
|
84
|
-
exports.mulberry32 = mulberry32;
|
|
85
78
|
/**
|
|
86
79
|
* Jenkins' Small Fast, Generator with a 32-bit state
|
|
87
80
|
*
|
|
88
81
|
* @param {number} a
|
|
89
82
|
* @returns {number} Pseudo-random number
|
|
90
83
|
*/
|
|
91
|
-
function jsf32(a, b, c, d) {
|
|
84
|
+
export function jsf32(a, b, c, d) {
|
|
92
85
|
a |= 0;
|
|
93
86
|
b |= 0;
|
|
94
87
|
c |= 0;
|
|
95
88
|
d |= 0;
|
|
96
|
-
|
|
89
|
+
let t = (a - ((b << 27) | (b >>> 5))) | 0;
|
|
97
90
|
a = b ^ ((c << 17) | (c >>> 15));
|
|
98
91
|
b = (c + d) | 0;
|
|
99
92
|
c = (d + t) | 0;
|
|
100
93
|
d = (a + t) | 0;
|
|
101
94
|
return (d >>> 0) / 4294967296;
|
|
102
95
|
}
|
|
103
|
-
exports.jsf32 = jsf32;
|
|
104
96
|
/**
|
|
105
97
|
* xoshiro128**, Generator with a 128-bit state
|
|
106
98
|
*
|
|
107
99
|
* @param {number} a
|
|
108
100
|
* @returns {number} Pseudo-random number
|
|
109
101
|
*/
|
|
110
|
-
function xoshiro128ss(a, b, c, d) {
|
|
111
|
-
|
|
112
|
-
|
|
102
|
+
export function xoshiro128ss(a, b, c, d) {
|
|
103
|
+
let t = b << 9;
|
|
104
|
+
let r = a * 5;
|
|
113
105
|
r = ((r << 7) | (r >>> 25)) * 9;
|
|
114
106
|
c ^= a;
|
|
115
107
|
d ^= b;
|
|
@@ -119,7 +111,6 @@ function xoshiro128ss(a, b, c, d) {
|
|
|
119
111
|
d = (d << 11) | (d >>> 21);
|
|
120
112
|
return (r >>> 0) / 4294967296;
|
|
121
113
|
}
|
|
122
|
-
exports.xoshiro128ss = xoshiro128ss;
|
|
123
114
|
/**
|
|
124
115
|
* Generate a pseudo-random number in the interval [0, 1]
|
|
125
116
|
* PRNG equivalent of `Math.random()`
|
|
@@ -127,13 +118,12 @@ exports.xoshiro128ss = xoshiro128ss;
|
|
|
127
118
|
* @param {PRNGParameters} prng PRNG parameters
|
|
128
119
|
* @returns {number} Pseudo-random number
|
|
129
120
|
*/
|
|
130
|
-
function random(prng) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return algorithm
|
|
121
|
+
export function random(prng) {
|
|
122
|
+
const seed = typeof prng === 'string' ? prng : prng.seed;
|
|
123
|
+
const algorithm = typeof prng === 'string' ? splitmix32 : prng.algorithm;
|
|
124
|
+
const hashes = cyrb128(seed);
|
|
125
|
+
return algorithm(...hashes);
|
|
135
126
|
}
|
|
136
|
-
exports.random = random;
|
|
137
127
|
/**
|
|
138
128
|
* Generate a pseudo-random boolean (true or false)
|
|
139
129
|
*
|
|
@@ -141,11 +131,9 @@ exports.random = random;
|
|
|
141
131
|
* @param {number} [probability=0.5] Probability to get true
|
|
142
132
|
* @returns {boolean} Either `true` or `false`
|
|
143
133
|
*/
|
|
144
|
-
function randomBoolean(prng, probability) {
|
|
145
|
-
if (probability === void 0) { probability = 0.5; }
|
|
134
|
+
export function randomBoolean(prng, probability = 0.5) {
|
|
146
135
|
return random(prng) < probability;
|
|
147
136
|
}
|
|
148
|
-
exports.randomBoolean = randomBoolean;
|
|
149
137
|
/**
|
|
150
138
|
* Generate a pseudo-random sign (1 or -1)
|
|
151
139
|
*
|
|
@@ -153,11 +141,9 @@ exports.randomBoolean = randomBoolean;
|
|
|
153
141
|
* @param {number} [probability=0.5] Probability to get 1
|
|
154
142
|
* @returns {number} Either 1 or -1
|
|
155
143
|
*/
|
|
156
|
-
function randomSign(prng, probability) {
|
|
157
|
-
if (probability === void 0) { probability = 0.5; }
|
|
144
|
+
export function randomSign(prng, probability = 0.5) {
|
|
158
145
|
return randomBoolean(prng, probability) ? 1 : -1;
|
|
159
146
|
}
|
|
160
|
-
exports.randomSign = randomSign;
|
|
161
147
|
/**
|
|
162
148
|
* Generate a pseudo-random floating-point number within a specified range
|
|
163
149
|
*
|
|
@@ -167,13 +153,9 @@ exports.randomSign = randomSign;
|
|
|
167
153
|
* @param {number} [precision=2] Number of digits after the decimal point
|
|
168
154
|
* @returns {number} Generated float
|
|
169
155
|
*/
|
|
170
|
-
function randomFloat(prng, min, max, precision) {
|
|
171
|
-
if (min === void 0) { min = 0; }
|
|
172
|
-
if (max === void 0) { max = 1; }
|
|
173
|
-
if (precision === void 0) { precision = 2; }
|
|
156
|
+
export function randomFloat(prng, min = 0, max = 1, precision = 2) {
|
|
174
157
|
return parseFloat(Math.min(min + random(prng) * (max - min), max).toFixed(precision));
|
|
175
158
|
}
|
|
176
|
-
exports.randomFloat = randomFloat;
|
|
177
159
|
/**
|
|
178
160
|
* Generate a pseudo-random integer number within a specified range
|
|
179
161
|
*
|
|
@@ -182,20 +164,18 @@ exports.randomFloat = randomFloat;
|
|
|
182
164
|
* @param {number} max Maximum boundary
|
|
183
165
|
* @returns {number} Generated integer
|
|
184
166
|
*/
|
|
185
|
-
function randomInt(prng, min, max) {
|
|
167
|
+
export function randomInt(prng, min, max) {
|
|
186
168
|
return Math.floor(random(prng) * (max - min + 1) + min);
|
|
187
169
|
}
|
|
188
|
-
exports.randomInt = randomInt;
|
|
189
170
|
/**
|
|
190
171
|
* Generate a pseudo-random hexadecimal color
|
|
191
172
|
*
|
|
192
173
|
* @param {PRNGParameters} prng PRNG parameters
|
|
193
174
|
* @returns {string} Generated hexadecimal color
|
|
194
175
|
*/
|
|
195
|
-
function randomHexColor(prng) {
|
|
176
|
+
export function randomHexColor(prng) {
|
|
196
177
|
return '#' + ('00000' + ((random(prng) * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
197
178
|
}
|
|
198
|
-
exports.randomHexColor = randomHexColor;
|
|
199
179
|
/**
|
|
200
180
|
* Pick a pseudo-random item from a given array
|
|
201
181
|
*
|
|
@@ -203,12 +183,11 @@ exports.randomHexColor = randomHexColor;
|
|
|
203
183
|
* @param {T[]} array Array to pick the item from
|
|
204
184
|
* @returns {T|undefined} Random item picked
|
|
205
185
|
*/
|
|
206
|
-
function randomItem(prng, array) {
|
|
186
|
+
export function randomItem(prng, array) {
|
|
207
187
|
if (array.length === 0)
|
|
208
188
|
return undefined;
|
|
209
189
|
return array[randomInt(prng, 0, array.length - 1)];
|
|
210
190
|
}
|
|
211
|
-
exports.randomItem = randomItem;
|
|
212
191
|
/**
|
|
213
192
|
* Pick a pseudo-random property value from a given object
|
|
214
193
|
*
|
|
@@ -216,14 +195,13 @@ exports.randomItem = randomItem;
|
|
|
216
195
|
* @param {object} object Object to pick the property from
|
|
217
196
|
* @returns {T|undefined} Random item picked
|
|
218
197
|
*/
|
|
219
|
-
function randomObjectProperty(prng, object) {
|
|
220
|
-
|
|
221
|
-
|
|
198
|
+
export function randomObjectProperty(prng, object) {
|
|
199
|
+
const keys = Object.keys(object);
|
|
200
|
+
const key = randomItem(prng, keys);
|
|
222
201
|
if (key && object.hasOwnProperty(key)) {
|
|
223
202
|
return object[key];
|
|
224
203
|
}
|
|
225
204
|
}
|
|
226
|
-
exports.randomObjectProperty = randomObjectProperty;
|
|
227
205
|
/**
|
|
228
206
|
* Select a pseudo-random index from an array of weighted items
|
|
229
207
|
*
|
|
@@ -231,25 +209,23 @@ exports.randomObjectProperty = randomObjectProperty;
|
|
|
231
209
|
* @param {number[]} weights Array of weights
|
|
232
210
|
* @returns {number} Random index based on weights
|
|
233
211
|
*/
|
|
234
|
-
function randomIndex(prng, weights) {
|
|
212
|
+
export function randomIndex(prng, weights) {
|
|
235
213
|
if (weights.length === 0)
|
|
236
214
|
return -1;
|
|
237
|
-
|
|
238
|
-
for (
|
|
239
|
-
|
|
240
|
-
totalWeight += weight_1;
|
|
215
|
+
let totalWeight = 0;
|
|
216
|
+
for (let weight of weights) {
|
|
217
|
+
totalWeight += weight;
|
|
241
218
|
}
|
|
242
219
|
if (totalWeight <= 0)
|
|
243
220
|
console.warn('PRNG randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
244
|
-
|
|
245
|
-
for (
|
|
221
|
+
let weight = random(prng) * totalWeight;
|
|
222
|
+
for (let i = 0; i < weights.length; i++) {
|
|
246
223
|
if (weight < weights[i])
|
|
247
224
|
return i;
|
|
248
225
|
weight -= weights[i];
|
|
249
226
|
}
|
|
250
227
|
return 0;
|
|
251
228
|
}
|
|
252
|
-
exports.randomIndex = randomIndex;
|
|
253
229
|
/**
|
|
254
230
|
* Generate a pseudo-random number fitting a Gaussian (normal) distribution
|
|
255
231
|
*
|
|
@@ -258,15 +234,12 @@ exports.randomIndex = randomIndex;
|
|
|
258
234
|
* @param {number} [spread=1] Standard deviation
|
|
259
235
|
* @returns {number} Generated number
|
|
260
236
|
*/
|
|
261
|
-
function randomGaussian(prng, mean, spread) {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
var v = algorithm.apply(void 0, hashes.reverse());
|
|
269
|
-
var z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
|
|
237
|
+
export function randomGaussian(prng, mean = 0, spread = 1) {
|
|
238
|
+
const seed = typeof prng === 'string' ? prng : prng.seed;
|
|
239
|
+
const algorithm = typeof prng === 'string' ? splitmix32 : prng.algorithm;
|
|
240
|
+
const hashes = cyrb128(seed);
|
|
241
|
+
const u = algorithm(...hashes);
|
|
242
|
+
const v = algorithm(...hashes.reverse());
|
|
243
|
+
const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
|
|
270
244
|
return mean + z * spread;
|
|
271
245
|
}
|
|
272
|
-
exports.randomGaussian = randomGaussian;
|
package/lib/query.js
CHANGED
|
@@ -1,39 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.hasQuery = exports.setQuery = exports.getQuery = void 0;
|
|
4
|
-
var search = typeof window !== 'undefined' ? window.location.search : undefined;
|
|
5
|
-
var history = typeof window !== 'undefined' ? window.history : undefined;
|
|
1
|
+
const search = typeof window !== 'undefined' ? window.location.search : undefined;
|
|
2
|
+
const history = typeof window !== 'undefined' ? window.history : undefined;
|
|
6
3
|
/**
|
|
7
4
|
* Get a query parameter
|
|
8
5
|
*
|
|
9
6
|
* @param {string} property Query property to check
|
|
10
7
|
* @returns {string|null} Value associated to the given search parameter
|
|
11
8
|
*/
|
|
12
|
-
function getQuery(property) {
|
|
13
|
-
|
|
9
|
+
export function getQuery(property) {
|
|
10
|
+
const params = new URLSearchParams(search);
|
|
14
11
|
return params.get(property);
|
|
15
12
|
}
|
|
16
|
-
exports.getQuery = getQuery;
|
|
17
13
|
/**
|
|
18
14
|
* Set a query parameter
|
|
19
15
|
*
|
|
20
16
|
* @param {string} property Query property to set
|
|
21
17
|
* @param {string} value Value to set
|
|
22
18
|
*/
|
|
23
|
-
function setQuery(property, value) {
|
|
24
|
-
|
|
19
|
+
export function setQuery(property, value) {
|
|
20
|
+
const params = new URLSearchParams(search);
|
|
25
21
|
params.set(property, value);
|
|
26
|
-
|
|
27
|
-
history
|
|
22
|
+
const string = '?' + params.toString().replace(/\=$/, '').replace(/\=\&/g, '&');
|
|
23
|
+
history?.pushState({ str: string }, '', string);
|
|
28
24
|
}
|
|
29
|
-
exports.setQuery = setQuery;
|
|
30
25
|
/**
|
|
31
26
|
* Check if a query parameter exists
|
|
32
27
|
*
|
|
33
28
|
* @param {string} property Query property to check
|
|
34
29
|
* @returns {boolean} True if the given property has a query parameter value, false otherwise
|
|
35
30
|
*/
|
|
36
|
-
function hasQuery(property) {
|
|
31
|
+
export function hasQuery(property) {
|
|
37
32
|
return getQuery(property) !== null;
|
|
38
33
|
}
|
|
39
|
-
exports.hasQuery = hasQuery;
|
package/lib/random.d.ts
CHANGED
package/lib/random.js
CHANGED
|
@@ -1,29 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.insideSphere = exports.onSphere = exports.insideCircle = exports.onCircle = exports.randomGaussian = exports.randomIndex = exports.randomObjectProperty = exports.randomItem = exports.randomHexColor = exports.randomInt = exports.randomFloat = exports.randomSign = exports.randomBoolean = void 0;
|
|
4
|
-
var geometry_1 = require("./geometry");
|
|
1
|
+
import { radToSphere } from './geometry';
|
|
5
2
|
/**
|
|
6
3
|
* Generate a random boolean (true or false)
|
|
7
4
|
*
|
|
8
5
|
* @param {number} [probability=0.5] Probability to get true
|
|
9
6
|
* @returns {boolean} Either `true` or `false`
|
|
10
7
|
*/
|
|
11
|
-
function randomBoolean(probability) {
|
|
12
|
-
if (probability === void 0) { probability = 0.5; }
|
|
8
|
+
export function randomBoolean(probability = 0.5) {
|
|
13
9
|
return Math.random() < probability;
|
|
14
10
|
}
|
|
15
|
-
exports.randomBoolean = randomBoolean;
|
|
16
11
|
/**
|
|
17
12
|
* Generate a random sign (1 or -1)
|
|
18
13
|
*
|
|
19
14
|
* @param {number} [probability=0.5] Probability to get 1
|
|
20
15
|
* @returns {number} Either 1 or -1
|
|
21
16
|
*/
|
|
22
|
-
function randomSign(probability) {
|
|
23
|
-
if (probability === void 0) { probability = 0.5; }
|
|
17
|
+
export function randomSign(probability = 0.5) {
|
|
24
18
|
return randomBoolean(probability) ? 1 : -1;
|
|
25
19
|
}
|
|
26
|
-
exports.randomSign = randomSign;
|
|
27
20
|
/**
|
|
28
21
|
* Generate a random floating-point number within a specified range
|
|
29
22
|
*
|
|
@@ -32,13 +25,9 @@ exports.randomSign = randomSign;
|
|
|
32
25
|
* @param {number} [precision=2] Number of digits after the decimal point
|
|
33
26
|
* @returns {number} Generated float
|
|
34
27
|
*/
|
|
35
|
-
function randomFloat(min, max, precision) {
|
|
36
|
-
if (min === void 0) { min = 0; }
|
|
37
|
-
if (max === void 0) { max = 1; }
|
|
38
|
-
if (precision === void 0) { precision = 2; }
|
|
28
|
+
export function randomFloat(min = 0, max = 1, precision = 2) {
|
|
39
29
|
return parseFloat(Math.min(min + Math.random() * (max - min), max).toFixed(precision));
|
|
40
30
|
}
|
|
41
|
-
exports.randomFloat = randomFloat;
|
|
42
31
|
/**
|
|
43
32
|
* Generate a random integer number within a specified range
|
|
44
33
|
*
|
|
@@ -46,71 +35,65 @@ exports.randomFloat = randomFloat;
|
|
|
46
35
|
* @param {number} max Maximum boundary
|
|
47
36
|
* @returns {number} Generated integer
|
|
48
37
|
*/
|
|
49
|
-
function randomInt(min, max) {
|
|
38
|
+
export function randomInt(min, max) {
|
|
50
39
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
51
40
|
}
|
|
52
|
-
exports.randomInt = randomInt;
|
|
53
41
|
/**
|
|
54
42
|
* Generate a random hexadecimal color
|
|
55
43
|
*
|
|
56
44
|
* @returns {string} Generated hexadecimal color
|
|
57
45
|
*/
|
|
58
|
-
function randomHexColor() {
|
|
46
|
+
export function randomHexColor() {
|
|
59
47
|
return '#' + ('00000' + ((Math.random() * (1 << 24)) | 0).toString(16)).slice(-6);
|
|
60
48
|
}
|
|
61
|
-
exports.randomHexColor = randomHexColor;
|
|
62
49
|
/**
|
|
63
50
|
* Pick a random item from a given array
|
|
64
51
|
*
|
|
65
52
|
* @param {T[]} array Array to pick the item from
|
|
66
53
|
* @returns {T|undefined} Random item picked
|
|
67
54
|
*/
|
|
68
|
-
function randomItem(array) {
|
|
55
|
+
export function randomItem(array) {
|
|
69
56
|
if (array.length === 0)
|
|
70
57
|
return undefined;
|
|
71
58
|
return array[randomInt(0, array.length - 1)];
|
|
72
59
|
}
|
|
73
|
-
exports.randomItem = randomItem;
|
|
74
60
|
/**
|
|
75
61
|
* Pick a random property value from a given object
|
|
76
62
|
*
|
|
77
63
|
* @param {object} object Object to pick the property from
|
|
78
64
|
* @returns {T|undefined} Random item picked
|
|
79
65
|
*/
|
|
80
|
-
function randomObjectProperty(object) {
|
|
81
|
-
|
|
82
|
-
|
|
66
|
+
export function randomObjectProperty(object) {
|
|
67
|
+
const keys = Object.keys(object);
|
|
68
|
+
const key = randomItem(keys);
|
|
83
69
|
if (key && object.hasOwnProperty(key)) {
|
|
84
70
|
return object[key];
|
|
85
71
|
}
|
|
86
72
|
}
|
|
87
|
-
exports.randomObjectProperty = randomObjectProperty;
|
|
88
73
|
/**
|
|
89
74
|
* Select a random index from an array of weighted items
|
|
90
75
|
*
|
|
91
76
|
* @param {number[]} weights Array of weights
|
|
92
77
|
* @returns {number} Random index based on weights
|
|
93
78
|
*/
|
|
94
|
-
function randomIndex(weights) {
|
|
79
|
+
export function randomIndex(weights) {
|
|
95
80
|
if (weights.length === 0)
|
|
96
81
|
return -1;
|
|
97
|
-
|
|
98
|
-
for (
|
|
99
|
-
|
|
100
|
-
totalWeight += weight_1;
|
|
82
|
+
let totalWeight = 0;
|
|
83
|
+
for (let weight of weights) {
|
|
84
|
+
totalWeight += weight;
|
|
101
85
|
}
|
|
102
86
|
if (totalWeight <= 0) {
|
|
103
87
|
console.warn('randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
104
88
|
}
|
|
105
|
-
|
|
106
|
-
for (
|
|
89
|
+
let weight = Math.random() * totalWeight;
|
|
90
|
+
for (let i = 0; i < weights.length; i++) {
|
|
107
91
|
if (weight < weights[i])
|
|
108
92
|
return i;
|
|
109
93
|
weight -= weights[i];
|
|
110
94
|
}
|
|
111
95
|
return 0;
|
|
112
96
|
}
|
|
113
|
-
exports.randomIndex = randomIndex;
|
|
114
97
|
/**
|
|
115
98
|
* Generate a random number fitting a Gaussian (normal) distribution
|
|
116
99
|
*
|
|
@@ -118,15 +101,12 @@ exports.randomIndex = randomIndex;
|
|
|
118
101
|
* @param {number} [spread=1] Standard deviation
|
|
119
102
|
* @returns {number} Generated number
|
|
120
103
|
*/
|
|
121
|
-
function randomGaussian(mean, spread) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
var v = Math.random();
|
|
126
|
-
var z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
|
|
104
|
+
export function randomGaussian(mean = 0, spread = 1) {
|
|
105
|
+
const u = Math.random();
|
|
106
|
+
const v = Math.random();
|
|
107
|
+
const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
|
|
127
108
|
return mean + z * spread;
|
|
128
109
|
}
|
|
129
|
-
exports.randomGaussian = randomGaussian;
|
|
130
110
|
// *********************
|
|
131
111
|
// Geometry
|
|
132
112
|
// *********************
|
|
@@ -137,15 +117,12 @@ exports.randomGaussian = randomGaussian;
|
|
|
137
117
|
* @param {Vector2} [target] Target vector
|
|
138
118
|
* @returns {Vector2} Random 2D point on circle
|
|
139
119
|
*/
|
|
140
|
-
function onCircle(radius, target) {
|
|
141
|
-
|
|
142
|
-
if (target === void 0) { target = { x: 0, y: 0 }; }
|
|
143
|
-
var angle = Math.random() * 2.0 * Math.PI;
|
|
120
|
+
export function onCircle(radius = 1, target = { x: 0, y: 0 }) {
|
|
121
|
+
const angle = Math.random() * 2.0 * Math.PI;
|
|
144
122
|
target.x = radius * Math.cos(angle);
|
|
145
123
|
target.y = radius * Math.sin(angle);
|
|
146
124
|
return target;
|
|
147
125
|
}
|
|
148
|
-
exports.onCircle = onCircle;
|
|
149
126
|
/**
|
|
150
127
|
* Produce a random 2D point inside a unit circle
|
|
151
128
|
*
|
|
@@ -153,13 +130,10 @@ exports.onCircle = onCircle;
|
|
|
153
130
|
* @param {Vector2} [target] Target vector
|
|
154
131
|
* @returns {Vector2} Random 2D point inside circle
|
|
155
132
|
*/
|
|
156
|
-
function insideCircle(radius, target) {
|
|
157
|
-
if (radius === void 0) { radius = 1; }
|
|
158
|
-
if (target === void 0) { target = { x: 0, y: 0 }; }
|
|
133
|
+
export function insideCircle(radius = 1, target = { x: 0, y: 0 }) {
|
|
159
134
|
radius *= Math.random();
|
|
160
135
|
return onCircle(radius, target);
|
|
161
136
|
}
|
|
162
|
-
exports.insideCircle = insideCircle;
|
|
163
137
|
/**
|
|
164
138
|
* Produce a random 3D point on the surface of a unit sphere
|
|
165
139
|
*
|
|
@@ -167,16 +141,13 @@ exports.insideCircle = insideCircle;
|
|
|
167
141
|
* @param {Vector3} [target] Target vector
|
|
168
142
|
* @returns {Vector3} Random 3D point on sphere
|
|
169
143
|
*/
|
|
170
|
-
function onSphere(radius, target) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
var theta = Math.acos(v);
|
|
177
|
-
return (0, geometry_1.radToSphere)(radius, phi, theta, target);
|
|
144
|
+
export function onSphere(radius = 1, target = { x: 0, y: 0, z: 0 }) {
|
|
145
|
+
const u = Math.random() * Math.PI * 2;
|
|
146
|
+
const v = Math.random() * 2 - 1;
|
|
147
|
+
const phi = u;
|
|
148
|
+
const theta = Math.acos(v);
|
|
149
|
+
return radToSphere(radius, phi, theta, target);
|
|
178
150
|
}
|
|
179
|
-
exports.onSphere = onSphere;
|
|
180
151
|
/**
|
|
181
152
|
* Produce a random 3D point inside a unit sphere
|
|
182
153
|
*
|
|
@@ -184,10 +155,7 @@ exports.onSphere = onSphere;
|
|
|
184
155
|
* @param {Vector3} [target] Target vector
|
|
185
156
|
* @returns {Vector3} Random 3D point inside sphere
|
|
186
157
|
*/
|
|
187
|
-
function insideSphere(radius, target) {
|
|
188
|
-
if (radius === void 0) { radius = 1; }
|
|
189
|
-
if (target === void 0) { target = { x: 0, y: 0, z: 0 }; }
|
|
158
|
+
export function insideSphere(radius = 1, target = { x: 0, y: 0, z: 0 }) {
|
|
190
159
|
radius *= Math.random();
|
|
191
160
|
return onSphere(radius, target);
|
|
192
161
|
}
|
|
193
|
-
exports.insideSphere = insideSphere;
|