@bitbybit-dev/base 1.2.0 → 1.3.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/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/inputs/base-inputs.d.ts +13 -12
- package/lib/api/inputs/color-inputs.d.ts +97 -48
- package/lib/api/inputs/color-inputs.js +95 -46
- package/lib/api/inputs/dates-inputs.d.ts +67 -26
- package/lib/api/inputs/dates-inputs.js +57 -16
- package/lib/api/inputs/io-inputs.d.ts +13 -15
- package/lib/api/inputs/io-inputs.js +7 -8
- package/lib/api/inputs/line-inputs.d.ts +88 -33
- package/lib/api/inputs/line-inputs.js +61 -11
- package/lib/api/inputs/lists-inputs.d.ts +207 -96
- package/lib/api/inputs/lists-inputs.js +153 -45
- package/lib/api/inputs/logic-inputs.d.ts +52 -22
- package/lib/api/inputs/logic-inputs.js +41 -12
- package/lib/api/inputs/math-inputs.d.ts +113 -52
- package/lib/api/inputs/math-inputs.js +112 -51
- package/lib/api/inputs/mesh-inputs.d.ts +30 -11
- package/lib/api/inputs/mesh-inputs.js +21 -3
- package/lib/api/inputs/point-inputs.d.ts +240 -107
- package/lib/api/inputs/point-inputs.js +199 -73
- package/lib/api/inputs/polyline-inputs.d.ts +68 -28
- package/lib/api/inputs/polyline-inputs.js +48 -13
- package/lib/api/inputs/text-inputs.d.ts +117 -46
- package/lib/api/inputs/text-inputs.js +111 -41
- package/lib/api/inputs/transforms-inputs.d.ts +65 -23
- package/lib/api/inputs/transforms-inputs.js +64 -22
- package/lib/api/inputs/vector-inputs.d.ts +154 -76
- package/lib/api/inputs/vector-inputs.js +132 -57
- package/lib/api/models/point/hex-grid-data.d.ts +1 -1
- package/lib/api/models/point/hex-grid-data.js +1 -1
- package/lib/api/services/color.d.ts +149 -70
- package/lib/api/services/color.js +149 -70
- package/lib/api/services/dates.d.ts +313 -150
- package/lib/api/services/dates.js +313 -150
- package/lib/api/services/line.d.ts +138 -68
- package/lib/api/services/line.js +138 -68
- package/lib/api/services/lists.d.ts +357 -177
- package/lib/api/services/lists.js +357 -177
- package/lib/api/services/logic.d.ts +98 -45
- package/lib/api/services/logic.js +98 -45
- package/lib/api/services/math.d.ts +363 -211
- package/lib/api/services/math.js +363 -211
- package/lib/api/services/mesh.d.ts +73 -31
- package/lib/api/services/mesh.js +73 -31
- package/lib/api/services/point.d.ts +367 -147
- package/lib/api/services/point.js +367 -147
- package/lib/api/services/polyline.d.ts +146 -71
- package/lib/api/services/polyline.js +146 -71
- package/lib/api/services/text.d.ts +320 -165
- package/lib/api/services/text.js +320 -165
- package/lib/api/services/transforms.d.ts +145 -58
- package/lib/api/services/transforms.js +145 -58
- package/lib/api/services/vector.d.ts +381 -156
- package/lib/api/services/vector.js +381 -156
- package/package.json +1 -1
package/lib/api/services/math.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import * as Inputs from "../inputs";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Arithmetic, rounding, ranges, random numbers and the trigonometric functions on plain numbers.
|
|
4
|
+
* The trigonometric functions take and give angles in radians; `degToRad` and `radToDeg` convert at
|
|
5
|
+
* the boundary, because almost every angle a user types is in degrees. The interpolation helpers
|
|
6
|
+
* (`lerp`, `remap`, `ease`, `smoothstep`, `pingPong`) are the building blocks of animation and
|
|
7
|
+
* parametric variation.
|
|
4
8
|
*/
|
|
5
9
|
export class MathBitByBit {
|
|
6
10
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
11
|
+
* Passes a number through unchanged, so a value can be given a name and reused.
|
|
12
|
+
*
|
|
13
|
+
* Example: 42 -> 42
|
|
14
|
+
* @param inputs - The number
|
|
15
|
+
* @returns The same number
|
|
11
16
|
* @group create
|
|
12
17
|
* @shortname number
|
|
13
18
|
* @drawable false
|
|
@@ -16,13 +21,21 @@ export class MathBitByBit {
|
|
|
16
21
|
return inputs.number;
|
|
17
22
|
}
|
|
18
23
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
24
|
+
* Applies one arithmetic operation to two numbers: add, subtract, multiply, divide, power or
|
|
25
|
+
* modulus.
|
|
26
|
+
*
|
|
27
|
+
* The operation reads `first` then `second`: subtract gives first minus second, power gives
|
|
28
|
+
* first to the power of second.
|
|
29
|
+
* Example: 5 add 3 -> 8, 10 modulus 3 -> 1, 2 power 3 -> 8
|
|
30
|
+
* @param inputs - The two numbers and the operation
|
|
31
|
+
* @returns The result of the operation
|
|
23
32
|
* @group operations
|
|
24
33
|
* @shortname two numbers
|
|
25
34
|
* @drawable false
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const result = bitbybit.math.twoNrOperation({ first: 2, second: 3, operation: Bit.Inputs.Math.mathTwoNrOperatorEnum.power });
|
|
38
|
+
* ```
|
|
26
39
|
*/
|
|
27
40
|
twoNrOperation(inputs) {
|
|
28
41
|
let result;
|
|
@@ -51,50 +64,76 @@ export class MathBitByBit {
|
|
|
51
64
|
return result;
|
|
52
65
|
}
|
|
53
66
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
67
|
+
* Finds the remainder after dividing one number by another.
|
|
68
|
+
*
|
|
69
|
+
* The sign follows the first number, as it does in JavaScript.
|
|
70
|
+
* Example: 10 modulus 3 -> 1, 17 modulus 5 -> 2
|
|
71
|
+
* @param inputs - The number to divide and the number to divide by
|
|
72
|
+
* @returns The remainder
|
|
58
73
|
* @group operations
|
|
59
74
|
* @shortname modulus
|
|
60
75
|
* @drawable false
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const remainder = bitbybit.math.modulus({ number: 17, modulus: 5 });
|
|
79
|
+
* ```
|
|
61
80
|
*/
|
|
62
81
|
modulus(inputs) {
|
|
63
82
|
return this.twoNrOperation({ first: inputs.number, second: inputs.modulus, operation: Inputs.Math.mathTwoNrOperatorEnum.modulus });
|
|
64
83
|
}
|
|
65
84
|
/**
|
|
66
|
-
* Rounds a number to
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* @
|
|
85
|
+
* Rounds a number to a given number of decimal places.
|
|
86
|
+
*
|
|
87
|
+
* Example: 1.32156 to 3 places -> 1.322
|
|
88
|
+
* @param inputs - The number and how many decimal places to keep
|
|
89
|
+
* @returns The rounded number
|
|
70
90
|
* @group operations
|
|
71
91
|
* @shortname round to decimals
|
|
72
92
|
* @drawable false
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const rounded = bitbybit.math.roundToDecimals({ number: 1.32156, decimalPlaces: 3 });
|
|
96
|
+
* ```
|
|
73
97
|
*/
|
|
74
98
|
roundToDecimals(inputs) {
|
|
75
99
|
return Math.round(inputs.number * Math.pow(10, inputs.decimalPlaces)) / Math.pow(10, inputs.decimalPlaces);
|
|
76
100
|
}
|
|
77
101
|
/**
|
|
78
|
-
* Rounds a number to
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
102
|
+
* Rounds a number to a given number of decimal places and drops the zeros at the end.
|
|
103
|
+
*
|
|
104
|
+
* As a number the result cannot carry trailing zeros anyway; the difference from
|
|
105
|
+
* `roundToDecimals` is that floating-point noise such as 1.320000001 is cleaned to 1.32.
|
|
106
|
+
* Example: 1.32156 to 3 places -> 1.322, 1.320000001 -> 1.32, 1.000 -> 1
|
|
107
|
+
* @param inputs - The number and how many decimal places to keep
|
|
108
|
+
* @returns The rounded number
|
|
82
109
|
* @group operations
|
|
83
110
|
* @shortname round trim zeros
|
|
84
111
|
* @drawable false
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const clean = bitbybit.math.roundAndRemoveTrailingZeros({ number: 1.320000001, decimalPlaces: 3 });
|
|
115
|
+
* ```
|
|
85
116
|
*/
|
|
86
117
|
roundAndRemoveTrailingZeros(inputs) {
|
|
87
118
|
const rounded = Math.round(inputs.number * Math.pow(10, inputs.decimalPlaces)) / Math.pow(10, inputs.decimalPlaces);
|
|
88
119
|
return parseFloat(rounded.toFixed(inputs.decimalPlaces));
|
|
89
120
|
}
|
|
90
121
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
122
|
+
* Applies one operation to a single number: absolute, negate, square root, rounding,
|
|
123
|
+
* logarithms, the trigonometric functions and their inverses, exponential, or a conversion
|
|
124
|
+
* between radians and degrees.
|
|
125
|
+
*
|
|
126
|
+
* The trigonometric functions work in radians.
|
|
127
|
+
* Example: sqrt of 5 -> 2.236, absolute of -3 -> 3
|
|
128
|
+
* @param inputs - The number and the operation
|
|
129
|
+
* @returns The result of the operation
|
|
95
130
|
* @group operations
|
|
96
131
|
* @shortname one number
|
|
97
132
|
* @drawable false
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const root = bitbybit.math.oneNrOperation({ number: 5, operation: Bit.Inputs.Math.mathOneNrOperatorEnum.sqrt });
|
|
136
|
+
* ```
|
|
98
137
|
*/
|
|
99
138
|
oneNrOperation(inputs) {
|
|
100
139
|
let result;
|
|
@@ -162,49 +201,66 @@ export class MathBitByBit {
|
|
|
162
201
|
return result;
|
|
163
202
|
}
|
|
164
203
|
/**
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
204
|
+
* Maps a number from one range onto another, keeping its relative position.
|
|
205
|
+
*
|
|
206
|
+
* A number outside the source range maps proportionally beyond the target range.
|
|
207
|
+
* Example: 5 from [0,10] to [0,100] -> 50, 0.5 from [0,1] to [-10,10] -> 0
|
|
208
|
+
* @param inputs - The number, the range it is in and the range to map it to
|
|
209
|
+
* @returns The number at the same relative position in the target range
|
|
210
|
+
* @group operations
|
|
211
|
+
* @shortname remap
|
|
212
|
+
* @drawable false
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* const percent = bitbybit.math.remap({ number: 5, fromLow: 0, fromHigh: 10, toLow: 0, toHigh: 100 });
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
173
218
|
remap(inputs) {
|
|
174
219
|
return (inputs.number - inputs.fromLow) * (inputs.toHigh - inputs.toLow) / (inputs.fromHigh - inputs.fromLow) + inputs.toLow;
|
|
175
220
|
}
|
|
176
221
|
/**
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
222
|
+
* Gives a random number from 0 up to, but not including, 1.
|
|
223
|
+
*
|
|
224
|
+
* Example: 0.342, 0.891 or any other value in that range
|
|
225
|
+
* @returns A random number between 0 and 1
|
|
226
|
+
* @group generate
|
|
227
|
+
* @shortname random 0 - 1
|
|
228
|
+
* @drawable false
|
|
229
|
+
*/
|
|
184
230
|
random() {
|
|
185
231
|
return Math.random();
|
|
186
232
|
}
|
|
187
233
|
/**
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
234
|
+
* Gives a random number between `low` and `high`.
|
|
235
|
+
*
|
|
236
|
+
* Example: low 0, high 10 -> 3.7, 8.2 or any other value between them
|
|
237
|
+
* @param inputs - The low and high ends of the range
|
|
238
|
+
* @returns A random number in the range
|
|
239
|
+
* @group generate
|
|
240
|
+
* @shortname random number
|
|
241
|
+
* @drawable false
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const value = bitbybit.math.randomNumber({ low: 0, high: 10 });
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
196
247
|
randomNumber(inputs) {
|
|
197
248
|
return Math.random() * (inputs.high - inputs.low) + inputs.low;
|
|
198
249
|
}
|
|
199
250
|
/**
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
251
|
+
* Gives a list of random numbers between `low` and `high`.
|
|
252
|
+
*
|
|
253
|
+
* Example: low 0, high 10, count 3 -> [2.5, 7.1, 4.8]
|
|
254
|
+
* @param inputs - The low and high ends of the range and how many numbers to make
|
|
255
|
+
* @returns The random numbers
|
|
256
|
+
* @group generate
|
|
257
|
+
* @shortname random numbers
|
|
258
|
+
* @drawable false
|
|
259
|
+
* @example
|
|
260
|
+
* ```typescript
|
|
261
|
+
* const values = bitbybit.math.randomNumbers({ low: 0, high: 10, count: 3 });
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
208
264
|
randomNumbers(inputs) {
|
|
209
265
|
const result = [];
|
|
210
266
|
for (let i = 0; i < inputs.count; i++) {
|
|
@@ -213,93 +269,127 @@ export class MathBitByBit {
|
|
|
213
269
|
return result;
|
|
214
270
|
}
|
|
215
271
|
/**
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
272
|
+
* Gives the constant pi, the ratio of a circle's circumference to its diameter.
|
|
273
|
+
*
|
|
274
|
+
* Example: 3.141592653589793
|
|
275
|
+
* @returns The number pi
|
|
276
|
+
* @group generate
|
|
277
|
+
* @shortname π
|
|
278
|
+
* @drawable false
|
|
279
|
+
*/
|
|
223
280
|
pi() {
|
|
224
281
|
return Math.PI;
|
|
225
282
|
}
|
|
226
283
|
/**
|
|
227
|
-
* Formats a number as
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
* @
|
|
284
|
+
* Formats a number as text with a fixed number of decimal places, keeping trailing zeros.
|
|
285
|
+
*
|
|
286
|
+
* Example: 3.14159 with 2 places -> '3.14', 5 with 3 places -> '5.000'
|
|
287
|
+
* @param inputs - The number and how many decimal places to show
|
|
288
|
+
* @returns The formatted text
|
|
231
289
|
* @group operations
|
|
232
290
|
* @shortname to fixed
|
|
233
291
|
* @drawable false
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* const label = bitbybit.math.toFixed({ number: 3.14159, decimalPlaces: 2 });
|
|
295
|
+
* ```
|
|
234
296
|
*/
|
|
235
297
|
toFixed(inputs) {
|
|
236
298
|
return inputs.number.toFixed(inputs.decimalPlaces);
|
|
237
299
|
}
|
|
238
300
|
/**
|
|
239
|
-
* Adds two numbers
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
* @
|
|
301
|
+
* Adds two numbers.
|
|
302
|
+
*
|
|
303
|
+
* Example: 5 and 3 -> 8, -2 and 7 -> 5
|
|
304
|
+
* @param inputs - The two numbers
|
|
305
|
+
* @returns Their sum
|
|
243
306
|
* @group basics
|
|
244
307
|
* @shortname add
|
|
245
308
|
* @drawable false
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* const sum = bitbybit.math.add({ first: 5, second: 3 });
|
|
312
|
+
* ```
|
|
246
313
|
*/
|
|
247
314
|
add(inputs) {
|
|
248
315
|
return inputs.first + inputs.second;
|
|
249
316
|
}
|
|
250
317
|
/**
|
|
251
318
|
* Subtracts the second number from the first.
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
* @
|
|
319
|
+
*
|
|
320
|
+
* Example: 10 and 3 -> 7, 5 and 8 -> -3
|
|
321
|
+
* @param inputs - The number to subtract from and the number to subtract
|
|
322
|
+
* @returns Their difference
|
|
255
323
|
* @group basics
|
|
256
324
|
* @shortname subtract
|
|
257
325
|
* @drawable false
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* const difference = bitbybit.math.subtract({ first: 10, second: 3 });
|
|
329
|
+
* ```
|
|
258
330
|
*/
|
|
259
331
|
subtract(inputs) {
|
|
260
332
|
return inputs.first - inputs.second;
|
|
261
333
|
}
|
|
262
334
|
/**
|
|
263
|
-
* Multiplies two numbers
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* @
|
|
335
|
+
* Multiplies two numbers.
|
|
336
|
+
*
|
|
337
|
+
* Example: 5 and 3 -> 15, -2 and 4 -> -8
|
|
338
|
+
* @param inputs - The two numbers
|
|
339
|
+
* @returns Their product
|
|
267
340
|
* @group basics
|
|
268
341
|
* @shortname multiply
|
|
269
342
|
* @drawable false
|
|
343
|
+
* @example
|
|
344
|
+
* ```typescript
|
|
345
|
+
* const product = bitbybit.math.multiply({ first: 5, second: 3 });
|
|
346
|
+
* ```
|
|
270
347
|
*/
|
|
271
348
|
multiply(inputs) {
|
|
272
349
|
return inputs.first * inputs.second;
|
|
273
350
|
}
|
|
274
351
|
/**
|
|
275
352
|
* Divides the first number by the second.
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
353
|
+
*
|
|
354
|
+
* Dividing by 0 gives Infinity, as in JavaScript.
|
|
355
|
+
* Example: 10 and 2 -> 5, 7 and 2 -> 3.5
|
|
356
|
+
* @param inputs - The number to divide and the number to divide by
|
|
357
|
+
* @returns Their quotient
|
|
279
358
|
* @group basics
|
|
280
359
|
* @shortname divide
|
|
281
360
|
* @drawable false
|
|
361
|
+
* @example
|
|
362
|
+
* ```typescript
|
|
363
|
+
* const quotient = bitbybit.math.divide({ first: 7, second: 2 });
|
|
364
|
+
* ```
|
|
282
365
|
*/
|
|
283
366
|
divide(inputs) {
|
|
284
367
|
return inputs.first / inputs.second;
|
|
285
368
|
}
|
|
286
369
|
/**
|
|
287
|
-
* Raises the first number to the power of the second
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
* @
|
|
370
|
+
* Raises the first number to the power of the second.
|
|
371
|
+
*
|
|
372
|
+
* Example: 2 to the 3 -> 8, 5 to the 2 -> 25, 10 to the -1 -> 0.1
|
|
373
|
+
* @param inputs - The base and the exponent
|
|
374
|
+
* @returns The power
|
|
291
375
|
* @group basics
|
|
292
376
|
* @shortname power
|
|
293
377
|
* @drawable false
|
|
378
|
+
* @example
|
|
379
|
+
* ```typescript
|
|
380
|
+
* const cube = bitbybit.math.power({ first: 2, second: 3 });
|
|
381
|
+
* ```
|
|
294
382
|
*/
|
|
295
383
|
power(inputs) {
|
|
296
384
|
return Math.pow(inputs.first, inputs.second);
|
|
297
385
|
}
|
|
298
386
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
387
|
+
* Finds the square root of a number.
|
|
388
|
+
*
|
|
389
|
+
* A negative number gives NaN.
|
|
390
|
+
* Example: 9 -> 3, 2 -> 1.414
|
|
391
|
+
* @param inputs - The number
|
|
392
|
+
* @returns The square root
|
|
303
393
|
* @group basics
|
|
304
394
|
* @shortname sqrt
|
|
305
395
|
* @drawable false
|
|
@@ -308,10 +398,11 @@ export class MathBitByBit {
|
|
|
308
398
|
return Math.sqrt(inputs.number);
|
|
309
399
|
}
|
|
310
400
|
/**
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
* @
|
|
401
|
+
* Drops the sign of a number, so the result is never negative.
|
|
402
|
+
*
|
|
403
|
+
* Example: -5 -> 5, 3 -> 3, 0 -> 0
|
|
404
|
+
* @param inputs - The number
|
|
405
|
+
* @returns The absolute value
|
|
315
406
|
* @group basics
|
|
316
407
|
* @shortname abs
|
|
317
408
|
* @drawable false
|
|
@@ -320,10 +411,11 @@ export class MathBitByBit {
|
|
|
320
411
|
return Math.abs(inputs.number);
|
|
321
412
|
}
|
|
322
413
|
/**
|
|
323
|
-
* Rounds a number to the nearest
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* @
|
|
414
|
+
* Rounds a number to the nearest whole number; halves round up.
|
|
415
|
+
*
|
|
416
|
+
* Example: 3.7 -> 4, 2.3 -> 2, 5.5 -> 6
|
|
417
|
+
* @param inputs - The number
|
|
418
|
+
* @returns The nearest whole number
|
|
327
419
|
* @group basics
|
|
328
420
|
* @shortname round
|
|
329
421
|
* @drawable false
|
|
@@ -332,10 +424,11 @@ export class MathBitByBit {
|
|
|
332
424
|
return Math.round(inputs.number);
|
|
333
425
|
}
|
|
334
426
|
/**
|
|
335
|
-
* Rounds a number down to the
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
* @
|
|
427
|
+
* Rounds a number down to the whole number below it.
|
|
428
|
+
*
|
|
429
|
+
* Example: 3.7 -> 3, -2.3 -> -3, 5 -> 5
|
|
430
|
+
* @param inputs - The number
|
|
431
|
+
* @returns The whole number below
|
|
339
432
|
* @group basics
|
|
340
433
|
* @shortname floor
|
|
341
434
|
* @drawable false
|
|
@@ -344,10 +437,11 @@ export class MathBitByBit {
|
|
|
344
437
|
return Math.floor(inputs.number);
|
|
345
438
|
}
|
|
346
439
|
/**
|
|
347
|
-
* Rounds a number up to the
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* @
|
|
440
|
+
* Rounds a number up to the whole number above it.
|
|
441
|
+
*
|
|
442
|
+
* Example: 3.2 -> 4, -2.8 -> -2, 5 -> 5
|
|
443
|
+
* @param inputs - The number
|
|
444
|
+
* @returns The whole number above
|
|
351
445
|
* @group basics
|
|
352
446
|
* @shortname ceil
|
|
353
447
|
* @drawable false
|
|
@@ -356,10 +450,11 @@ export class MathBitByBit {
|
|
|
356
450
|
return Math.ceil(inputs.number);
|
|
357
451
|
}
|
|
358
452
|
/**
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
* @
|
|
453
|
+
* Flips the sign of a number.
|
|
454
|
+
*
|
|
455
|
+
* Example: 5 -> -5, -3 -> 3, 0 -> 0
|
|
456
|
+
* @param inputs - The number
|
|
457
|
+
* @returns The number with the opposite sign
|
|
363
458
|
* @group basics
|
|
364
459
|
* @shortname negate
|
|
365
460
|
* @drawable false
|
|
@@ -368,10 +463,11 @@ export class MathBitByBit {
|
|
|
368
463
|
return -inputs.number;
|
|
369
464
|
}
|
|
370
465
|
/**
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
* @
|
|
466
|
+
* Finds the natural logarithm of a number: the power e must be raised to for that number.
|
|
467
|
+
*
|
|
468
|
+
* Example: 2.718 -> about 1, 1 -> 0
|
|
469
|
+
* @param inputs - The number, greater than 0
|
|
470
|
+
* @returns The natural logarithm
|
|
375
471
|
* @group basics
|
|
376
472
|
* @shortname ln
|
|
377
473
|
* @drawable false
|
|
@@ -380,10 +476,11 @@ export class MathBitByBit {
|
|
|
380
476
|
return Math.log(inputs.number);
|
|
381
477
|
}
|
|
382
478
|
/**
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
* @
|
|
479
|
+
* Finds the base-10 logarithm of a number: the power 10 must be raised to for that number.
|
|
480
|
+
*
|
|
481
|
+
* Example: 100 -> 2, 1000 -> 3, 10 -> 1
|
|
482
|
+
* @param inputs - The number, greater than 0
|
|
483
|
+
* @returns The base-10 logarithm
|
|
387
484
|
* @group basics
|
|
388
485
|
* @shortname log10
|
|
389
486
|
* @drawable false
|
|
@@ -392,10 +489,11 @@ export class MathBitByBit {
|
|
|
392
489
|
return Math.log10(inputs.number);
|
|
393
490
|
}
|
|
394
491
|
/**
|
|
395
|
-
* Raises 10 to the power of
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
* @
|
|
492
|
+
* Raises 10 to the power of a number.
|
|
493
|
+
*
|
|
494
|
+
* Example: 2 -> 100, 3 -> 1000, -1 -> 0.1
|
|
495
|
+
* @param inputs - The exponent
|
|
496
|
+
* @returns 10 to that power
|
|
399
497
|
* @group basics
|
|
400
498
|
* @shortname ten pow
|
|
401
499
|
* @drawable false
|
|
@@ -404,10 +502,11 @@ export class MathBitByBit {
|
|
|
404
502
|
return Math.pow(10, inputs.number);
|
|
405
503
|
}
|
|
406
504
|
/**
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
* @
|
|
505
|
+
* Finds the sine of an angle given in radians.
|
|
506
|
+
*
|
|
507
|
+
* Example: 0 -> 0, pi/2 -> 1
|
|
508
|
+
* @param inputs - The angle in radians
|
|
509
|
+
* @returns The sine, between -1 and 1
|
|
411
510
|
* @group basics
|
|
412
511
|
* @shortname sin
|
|
413
512
|
* @drawable false
|
|
@@ -416,10 +515,11 @@ export class MathBitByBit {
|
|
|
416
515
|
return Math.sin(inputs.number);
|
|
417
516
|
}
|
|
418
517
|
/**
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* @
|
|
518
|
+
* Finds the cosine of an angle given in radians.
|
|
519
|
+
*
|
|
520
|
+
* Example: 0 -> 1, pi -> -1
|
|
521
|
+
* @param inputs - The angle in radians
|
|
522
|
+
* @returns The cosine, between -1 and 1
|
|
423
523
|
* @group basics
|
|
424
524
|
* @shortname cos
|
|
425
525
|
* @drawable false
|
|
@@ -428,10 +528,11 @@ export class MathBitByBit {
|
|
|
428
528
|
return Math.cos(inputs.number);
|
|
429
529
|
}
|
|
430
530
|
/**
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
* @
|
|
531
|
+
* Finds the tangent of an angle given in radians.
|
|
532
|
+
*
|
|
533
|
+
* Example: 0 -> 0, pi/4 -> about 1
|
|
534
|
+
* @param inputs - The angle in radians
|
|
535
|
+
* @returns The tangent
|
|
435
536
|
* @group basics
|
|
436
537
|
* @shortname tan
|
|
437
538
|
* @drawable false
|
|
@@ -440,10 +541,11 @@ export class MathBitByBit {
|
|
|
440
541
|
return Math.tan(inputs.number);
|
|
441
542
|
}
|
|
442
543
|
/**
|
|
443
|
-
*
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
* @
|
|
544
|
+
* Finds the angle, in radians, whose sine is the given number.
|
|
545
|
+
*
|
|
546
|
+
* Example: 0 -> 0, 1 -> pi/2 (about 1.57)
|
|
547
|
+
* @param inputs - A number between -1 and 1
|
|
548
|
+
* @returns The angle in radians
|
|
447
549
|
* @group basics
|
|
448
550
|
* @shortname asin
|
|
449
551
|
* @drawable false
|
|
@@ -452,10 +554,11 @@ export class MathBitByBit {
|
|
|
452
554
|
return Math.asin(inputs.number);
|
|
453
555
|
}
|
|
454
556
|
/**
|
|
455
|
-
*
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
* @
|
|
557
|
+
* Finds the angle, in radians, whose cosine is the given number.
|
|
558
|
+
*
|
|
559
|
+
* Example: 1 -> 0, -1 -> pi (about 3.14)
|
|
560
|
+
* @param inputs - A number between -1 and 1
|
|
561
|
+
* @returns The angle in radians
|
|
459
562
|
* @group basics
|
|
460
563
|
* @shortname acos
|
|
461
564
|
* @drawable false
|
|
@@ -464,10 +567,11 @@ export class MathBitByBit {
|
|
|
464
567
|
return Math.acos(inputs.number);
|
|
465
568
|
}
|
|
466
569
|
/**
|
|
467
|
-
*
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
* @
|
|
570
|
+
* Finds the angle, in radians, whose tangent is the given number.
|
|
571
|
+
*
|
|
572
|
+
* Example: 0 -> 0, 1 -> pi/4 (about 0.785)
|
|
573
|
+
* @param inputs - The number
|
|
574
|
+
* @returns The angle in radians, between -pi/2 and pi/2
|
|
471
575
|
* @group basics
|
|
472
576
|
* @shortname atan
|
|
473
577
|
* @drawable false
|
|
@@ -476,10 +580,11 @@ export class MathBitByBit {
|
|
|
476
580
|
return Math.atan(inputs.number);
|
|
477
581
|
}
|
|
478
582
|
/**
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* @
|
|
583
|
+
* Raises e, the base of the natural logarithm, to the power of a number.
|
|
584
|
+
*
|
|
585
|
+
* Example: 0 -> 1, 1 -> about 2.718, 2 -> about 7.389
|
|
586
|
+
* @param inputs - The exponent
|
|
587
|
+
* @returns e to that power
|
|
483
588
|
* @group basics
|
|
484
589
|
* @shortname exp
|
|
485
590
|
* @drawable false
|
|
@@ -489,9 +594,10 @@ export class MathBitByBit {
|
|
|
489
594
|
}
|
|
490
595
|
/**
|
|
491
596
|
* Converts an angle from degrees to radians.
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
* @
|
|
597
|
+
*
|
|
598
|
+
* Example: 180 -> pi (about 3.14159), 90 -> pi/2
|
|
599
|
+
* @param inputs - The angle in degrees
|
|
600
|
+
* @returns The angle in radians
|
|
495
601
|
* @group basics
|
|
496
602
|
* @shortname deg to rad
|
|
497
603
|
* @drawable false
|
|
@@ -501,9 +607,10 @@ export class MathBitByBit {
|
|
|
501
607
|
}
|
|
502
608
|
/**
|
|
503
609
|
* Converts an angle from radians to degrees.
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
* @
|
|
610
|
+
*
|
|
611
|
+
* Example: pi -> 180, pi/2 -> 90
|
|
612
|
+
* @param inputs - The angle in radians
|
|
613
|
+
* @returns The angle in degrees
|
|
507
614
|
* @group basics
|
|
508
615
|
* @shortname rad to deg
|
|
509
616
|
* @drawable false
|
|
@@ -512,14 +619,21 @@ export class MathBitByBit {
|
|
|
512
619
|
return inputs.number * 180 / Math.PI;
|
|
513
620
|
}
|
|
514
621
|
/**
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
*
|
|
622
|
+
* Maps a value from 0 to 1 onto the range `min` to `max` along an easing curve, so the result
|
|
623
|
+
* speeds up or slows down instead of changing evenly.
|
|
624
|
+
*
|
|
625
|
+
* An `easeIn` curve starts slowly, an `easeOut` curve ends slowly, an `easeInOut` curve does
|
|
626
|
+
* both.
|
|
627
|
+
* Example: 0.5 from [0,100] with easeInQuad -> 25
|
|
628
|
+
* @param inputs - The value between 0 and 1, the target range and the easing curve
|
|
629
|
+
* @returns The eased value in the target range
|
|
520
630
|
* @group operations
|
|
521
631
|
* @shortname ease
|
|
522
632
|
* @drawable false
|
|
633
|
+
* @example
|
|
634
|
+
* ```typescript
|
|
635
|
+
* const eased = bitbybit.math.ease({ x: 0.5, min: 0, max: 100, ease: Bit.Inputs.Math.easeEnum.easeInQuad });
|
|
636
|
+
* ```
|
|
523
637
|
*/
|
|
524
638
|
ease(inputs) {
|
|
525
639
|
const x = inputs.x;
|
|
@@ -530,39 +644,55 @@ export class MathBitByBit {
|
|
|
530
644
|
return res;
|
|
531
645
|
}
|
|
532
646
|
/**
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
* @
|
|
647
|
+
* Keeps a number within a range: below `min` becomes `min`, above `max` becomes `max`.
|
|
648
|
+
*
|
|
649
|
+
* Example: 5 in [0,3] -> 3, -1 in [0,3] -> 0, 1.5 in [0,3] -> 1.5
|
|
650
|
+
* @param inputs - The number and the range to keep it in
|
|
651
|
+
* @returns The number, limited to the range
|
|
537
652
|
* @group operations
|
|
538
653
|
* @shortname clamp
|
|
539
654
|
* @drawable false
|
|
655
|
+
* @example
|
|
656
|
+
* ```typescript
|
|
657
|
+
* const limited = bitbybit.math.clamp({ number: 5, min: 0, max: 3 });
|
|
658
|
+
* ```
|
|
540
659
|
*/
|
|
541
660
|
clamp(inputs) {
|
|
542
661
|
return Math.max(inputs.min, Math.min(inputs.max, inputs.number));
|
|
543
662
|
}
|
|
544
663
|
/**
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
664
|
+
* Blends from a start value to an end value by a fraction `t`: 0 gives the start, 1 the end,
|
|
665
|
+
* 0.5 the midpoint.
|
|
666
|
+
*
|
|
667
|
+
* A `t` outside 0 to 1 extrapolates past the ends.
|
|
668
|
+
* Example: 0 to 100 at 0.5 -> 50, 10 to 20 at 0.25 -> 12.5
|
|
669
|
+
* @param inputs - The start value, the end value and the fraction
|
|
670
|
+
* @returns The blended value
|
|
550
671
|
* @group operations
|
|
551
672
|
* @shortname lerp
|
|
552
673
|
* @drawable false
|
|
674
|
+
* @example
|
|
675
|
+
* ```typescript
|
|
676
|
+
* const mid = bitbybit.math.lerp({ start: 10, end: 20, t: 0.25 });
|
|
677
|
+
* ```
|
|
553
678
|
*/
|
|
554
679
|
lerp(inputs) {
|
|
555
680
|
return inputs.start + (inputs.end - inputs.start) * inputs.t;
|
|
556
681
|
}
|
|
557
682
|
/**
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
* @
|
|
683
|
+
* Finds where a value sits between a start and an end, as a fraction: the `t` that `lerp` would
|
|
684
|
+
* need to produce it.
|
|
685
|
+
*
|
|
686
|
+
* Example: 5 in [0,10] -> 0.5, 2.5 in [0,10] -> 0.25
|
|
687
|
+
* @param inputs - The start value, the end value and the value to locate
|
|
688
|
+
* @returns The fraction from start to end
|
|
563
689
|
* @group operations
|
|
564
690
|
* @shortname inverse lerp
|
|
565
691
|
* @drawable false
|
|
692
|
+
* @example
|
|
693
|
+
* ```typescript
|
|
694
|
+
* const fraction = bitbybit.math.inverseLerp({ start: 0, end: 10, value: 2.5 });
|
|
695
|
+
* ```
|
|
566
696
|
*/
|
|
567
697
|
inverseLerp(inputs) {
|
|
568
698
|
if (inputs.start === inputs.end) {
|
|
@@ -571,11 +701,12 @@ export class MathBitByBit {
|
|
|
571
701
|
return (inputs.value - inputs.start) / (inputs.end - inputs.start);
|
|
572
702
|
}
|
|
573
703
|
/**
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
* @
|
|
704
|
+
* Turns a value from 0 to 1 into a smooth S-curve that starts and ends gently; the value is
|
|
705
|
+
* clamped to that range first.
|
|
706
|
+
*
|
|
707
|
+
* Example: 0 -> 0, 0.5 -> 0.5, 0.25 -> 0.156
|
|
708
|
+
* @param inputs - The value between 0 and 1
|
|
709
|
+
* @returns The smoothed value between 0 and 1
|
|
579
710
|
* @group operations
|
|
580
711
|
* @shortname smoothstep
|
|
581
712
|
* @drawable false
|
|
@@ -585,11 +716,11 @@ export class MathBitByBit {
|
|
|
585
716
|
return t * t * (3 - 2 * t);
|
|
586
717
|
}
|
|
587
718
|
/**
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
* @param inputs
|
|
592
|
-
* @returns -1, 0
|
|
719
|
+
* Tells the sign of a number: -1 when negative, 0 when zero, 1 when positive.
|
|
720
|
+
*
|
|
721
|
+
* Example: -5 -> -1, 0 -> 0, 3.14 -> 1
|
|
722
|
+
* @param inputs - The number
|
|
723
|
+
* @returns -1, 0 or 1
|
|
593
724
|
* @group operations
|
|
594
725
|
* @shortname sign
|
|
595
726
|
* @drawable false
|
|
@@ -598,11 +729,12 @@ export class MathBitByBit {
|
|
|
598
729
|
return Math.sign(inputs.number);
|
|
599
730
|
}
|
|
600
731
|
/**
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
* @
|
|
732
|
+
* Keeps the part of a number after the decimal point, measured up from the whole number below
|
|
733
|
+
* it, so the result is always from 0 up to 1.
|
|
734
|
+
*
|
|
735
|
+
* Example: 3.14 -> 0.14, -2.3 -> 0.7
|
|
736
|
+
* @param inputs - The number
|
|
737
|
+
* @returns The fractional part, from 0 up to 1
|
|
606
738
|
* @group operations
|
|
607
739
|
* @shortname fract
|
|
608
740
|
* @drawable false
|
|
@@ -611,14 +743,20 @@ export class MathBitByBit {
|
|
|
611
743
|
return inputs.number - Math.floor(inputs.number);
|
|
612
744
|
}
|
|
613
745
|
/**
|
|
614
|
-
* Wraps a number
|
|
615
|
-
*
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
746
|
+
* Wraps a number into a range so it cycles round: past `max` it comes back in at `min`, and
|
|
747
|
+
* below `min` it comes back in at `max`.
|
|
748
|
+
*
|
|
749
|
+
* Useful for angles and repeating patterns; unlike a plain modulus it handles negative numbers.
|
|
750
|
+
* Example: 1.5 in [0,1) -> 0.5, -0.3 in [0,1) -> 0.7, 370 in [0,360) -> 10
|
|
751
|
+
* @param inputs - The number and the range to wrap it into
|
|
752
|
+
* @returns The wrapped number, from min up to max
|
|
619
753
|
* @group operations
|
|
620
754
|
* @shortname wrap
|
|
621
755
|
* @drawable false
|
|
756
|
+
* @example
|
|
757
|
+
* ```typescript
|
|
758
|
+
* const angle = bitbybit.math.wrap({ number: 370, min: 0, max: 360 });
|
|
759
|
+
* ```
|
|
622
760
|
*/
|
|
623
761
|
wrap(inputs) {
|
|
624
762
|
const range = inputs.max - inputs.min;
|
|
@@ -629,29 +767,37 @@ export class MathBitByBit {
|
|
|
629
767
|
return normalized < 0 ? normalized + inputs.max : normalized + inputs.min;
|
|
630
768
|
}
|
|
631
769
|
/**
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
* @param inputs
|
|
637
|
-
* @returns value
|
|
770
|
+
* Bounces a value back and forth between 0 and `length` as `t` grows: up to `length`, back down
|
|
771
|
+
* to 0, and again.
|
|
772
|
+
*
|
|
773
|
+
* Example: length 1 at t 0.5 -> 0.5, t 1 -> 1, t 1.5 -> 0.5, t 2 -> 0
|
|
774
|
+
* @param inputs - The running value and the length to bounce within
|
|
775
|
+
* @returns The bounced value between 0 and length
|
|
638
776
|
* @group operations
|
|
639
777
|
* @shortname ping pong
|
|
640
778
|
* @drawable false
|
|
779
|
+
* @example
|
|
780
|
+
* ```typescript
|
|
781
|
+
* const bounce = bitbybit.math.pingPong({ t: 1.5, length: 1 });
|
|
782
|
+
* ```
|
|
641
783
|
*/
|
|
642
784
|
pingPong(inputs) {
|
|
643
785
|
const t = Math.abs(inputs.t) % (inputs.length * 2);
|
|
644
786
|
return t > inputs.length ? inputs.length * 2 - t : t;
|
|
645
787
|
}
|
|
646
788
|
/**
|
|
647
|
-
* Moves a value toward a target by
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
* @param inputs current value, target
|
|
651
|
-
* @returns
|
|
789
|
+
* Moves a value toward a target by at most `maxDelta`, without overshooting it.
|
|
790
|
+
*
|
|
791
|
+
* Example: 0 toward 10 by 3 -> 3, 8 toward 10 by 3 -> 10
|
|
792
|
+
* @param inputs - The current value, the target and the largest step allowed
|
|
793
|
+
* @returns The value after one step
|
|
652
794
|
* @group operations
|
|
653
795
|
* @shortname move towards
|
|
654
796
|
* @drawable false
|
|
797
|
+
* @example
|
|
798
|
+
* ```typescript
|
|
799
|
+
* const next = bitbybit.math.moveTowards({ current: 8, target: 10, maxDelta: 3 });
|
|
800
|
+
* ```
|
|
655
801
|
*/
|
|
656
802
|
moveTowards(inputs) {
|
|
657
803
|
const delta = inputs.target - inputs.current;
|
|
@@ -661,15 +807,21 @@ export class MathBitByBit {
|
|
|
661
807
|
return inputs.current + Math.sign(delta) * inputs.maxDelta;
|
|
662
808
|
}
|
|
663
809
|
/**
|
|
664
|
-
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
810
|
+
* Works out a simple arithmetic expression written as text: numbers, +, -, the multiplication
|
|
811
|
+
* sign, /, parentheses and spaces.
|
|
812
|
+
*
|
|
813
|
+
* The expression is parsed and computed by the library itself, never handed to the JavaScript
|
|
814
|
+
* engine to run, so it is safe with text a user typed.
|
|
815
|
+
* Example: '(3+2) times 4' written with the sign -> 20, '10/3' -> 3.3333
|
|
816
|
+
* @param inputs - The expression text
|
|
817
|
+
* @returns The computed value
|
|
670
818
|
* @group operations
|
|
671
819
|
* @shortname eval arithmetic
|
|
672
820
|
* @drawable false
|
|
821
|
+
* @example
|
|
822
|
+
* ```typescript
|
|
823
|
+
* const value = bitbybit.math.evalArithmetic({ expression: "(3 + 2) * 4" });
|
|
824
|
+
* ```
|
|
673
825
|
*/
|
|
674
826
|
evalArithmetic(inputs) {
|
|
675
827
|
var _a;
|