@bitbybit-dev/base 1.2.0-rc.2 → 1.3.0-rc.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.
Files changed (55) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/inputs/base-inputs.d.ts +13 -12
  3. package/lib/api/inputs/color-inputs.d.ts +97 -48
  4. package/lib/api/inputs/color-inputs.js +95 -46
  5. package/lib/api/inputs/dates-inputs.d.ts +67 -26
  6. package/lib/api/inputs/dates-inputs.js +57 -16
  7. package/lib/api/inputs/io-inputs.d.ts +13 -15
  8. package/lib/api/inputs/io-inputs.js +7 -8
  9. package/lib/api/inputs/line-inputs.d.ts +88 -33
  10. package/lib/api/inputs/line-inputs.js +61 -11
  11. package/lib/api/inputs/lists-inputs.d.ts +207 -96
  12. package/lib/api/inputs/lists-inputs.js +153 -45
  13. package/lib/api/inputs/logic-inputs.d.ts +52 -22
  14. package/lib/api/inputs/logic-inputs.js +41 -12
  15. package/lib/api/inputs/math-inputs.d.ts +113 -52
  16. package/lib/api/inputs/math-inputs.js +112 -51
  17. package/lib/api/inputs/mesh-inputs.d.ts +30 -11
  18. package/lib/api/inputs/mesh-inputs.js +21 -3
  19. package/lib/api/inputs/point-inputs.d.ts +240 -107
  20. package/lib/api/inputs/point-inputs.js +199 -73
  21. package/lib/api/inputs/polyline-inputs.d.ts +68 -28
  22. package/lib/api/inputs/polyline-inputs.js +48 -13
  23. package/lib/api/inputs/text-inputs.d.ts +117 -46
  24. package/lib/api/inputs/text-inputs.js +111 -41
  25. package/lib/api/inputs/transforms-inputs.d.ts +65 -23
  26. package/lib/api/inputs/transforms-inputs.js +64 -22
  27. package/lib/api/inputs/vector-inputs.d.ts +154 -76
  28. package/lib/api/inputs/vector-inputs.js +132 -57
  29. package/lib/api/models/point/hex-grid-data.d.ts +1 -1
  30. package/lib/api/models/point/hex-grid-data.js +1 -1
  31. package/lib/api/services/color.d.ts +149 -70
  32. package/lib/api/services/color.js +149 -70
  33. package/lib/api/services/dates.d.ts +313 -150
  34. package/lib/api/services/dates.js +313 -150
  35. package/lib/api/services/line.d.ts +138 -68
  36. package/lib/api/services/line.js +138 -68
  37. package/lib/api/services/lists.d.ts +357 -177
  38. package/lib/api/services/lists.js +357 -177
  39. package/lib/api/services/logic.d.ts +98 -45
  40. package/lib/api/services/logic.js +98 -45
  41. package/lib/api/services/math.d.ts +363 -211
  42. package/lib/api/services/math.js +363 -211
  43. package/lib/api/services/mesh.d.ts +73 -31
  44. package/lib/api/services/mesh.js +73 -31
  45. package/lib/api/services/point.d.ts +367 -147
  46. package/lib/api/services/point.js +367 -147
  47. package/lib/api/services/polyline.d.ts +146 -71
  48. package/lib/api/services/polyline.js +146 -71
  49. package/lib/api/services/text.d.ts +320 -165
  50. package/lib/api/services/text.js +320 -165
  51. package/lib/api/services/transforms.d.ts +145 -58
  52. package/lib/api/services/transforms.js +145 -58
  53. package/lib/api/services/vector.d.ts +381 -156
  54. package/lib/api/services/vector.js +381 -156
  55. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Contains various methods for vector mathematics. Vector in bitbybit is simply an array, usually
3
- * containing numbers. In 3D [x, y, z] form describes space, where y is the up vector. Because of this form
4
- * Vector can be interchanged with Point, which also is an array in [x, y, z] form.
2
+ * Vector maths on plain number arrays. A vector is an array of numbers; in 3D it is `[x, y, z]`
3
+ * with Y pointing up, the same shape as a point, so the two can be passed to each other's methods.
4
+ * Every method returns a new array or a number and never changes its inputs. Angles are in degrees.
5
5
  */
6
6
  export class Vector {
7
7
  constructor(math, geometryHelper) {
@@ -9,49 +9,82 @@ export class Vector {
9
9
  this.geometryHelper = geometryHelper;
10
10
  }
11
11
  /**
12
- * Removes all duplicate vectors from the input array (keeps only unique vectors).
13
- * Example: [[1,2,3], [4,5,6], [1,2,3], [7,8,9]] → [[1,2,3], [4,5,6], [7,8,9]]
14
- * @param inputs Contains vectors and a tolerance value
15
- * @returns Array of vectors without duplicates
12
+ * Removes every repeated vector from a list, keeping the first occurrence of each.
13
+ *
14
+ * Two vectors count as the same when every entry differs by less than `tolerance`.
15
+ * Example: [[1,2,3], [4,5,6], [1,2,3], [7,8,9]] -> [[1,2,3], [4,5,6], [7,8,9]]
16
+ * @param inputs - Vectors to filter and the tolerance
17
+ * @returns The vectors without repeats, in their original order
16
18
  * @group remove
17
19
  * @shortname remove all duplicates
18
20
  * @drawable false
21
+ * @example
22
+ * ```typescript
23
+ * const unique = bitbybit.vector.removeAllDuplicateVectors({
24
+ * vectors: [[1, 2, 3], [4, 5, 6], [1, 2, 3]],
25
+ * tolerance: 1e-7,
26
+ * });
27
+ * ```
19
28
  */
20
29
  removeAllDuplicateVectors(inputs) {
21
30
  return this.geometryHelper.removeAllDuplicateVectors(inputs.vectors, inputs.tolerance);
22
31
  }
23
32
  /**
24
- * Removes consecutive duplicate vectors from the input array (only removes duplicates that appear next to each other).
25
- * Example: [[1,2], [1,2], [3,4], [1,2]] → [[1,2], [3,4], [1,2]] (only removed consecutive duplicate)
26
- * @param inputs Contains vectors and a tolerance value
27
- * @returns Array of vectors without duplicates
33
+ * Removes a vector when it repeats the one right before it; the same vector further away is
34
+ * kept.
35
+ *
36
+ * With `checkFirstAndLast` on, a last vector that repeats the first is dropped too, which
37
+ * closes a loop of points cleanly. Entries within `tolerance` of each other count as equal.
38
+ * Example: [[1,2], [1,2], [3,4], [1,2]] -> [[1,2], [3,4], [1,2]]
39
+ * @param inputs - Vectors to filter, whether to compare the first and last, and the tolerance
40
+ * @returns The vectors without consecutive repeats
28
41
  * @group remove
29
42
  * @shortname remove consecutive duplicates
30
43
  * @drawable false
44
+ * @example
45
+ * ```typescript
46
+ * const cleaned = bitbybit.vector.removeConsecutiveDuplicateVectors({
47
+ * vectors: [[0, 0], [0, 0], [1, 1], [0, 0]],
48
+ * checkFirstAndLast: true,
49
+ * tolerance: 1e-7,
50
+ * });
51
+ * ```
31
52
  */
32
53
  removeConsecutiveDuplicateVectors(inputs) {
33
54
  return this.geometryHelper.removeConsecutiveVectorDuplicates(inputs.vectors, inputs.checkFirstAndLast, inputs.tolerance);
34
55
  }
35
56
  /**
36
- * Checks if two vectors are the same within a given tolerance (accounts for floating point precision).
37
- * Example: [1,2,3] vs [1.0001,2.0001,3.0001] with tolerance 0.001 → true
38
- * @param inputs Contains two vectors and a tolerance value
39
- * @returns Boolean indicating if vectors are the same
57
+ * Tells whether two vectors are the same within a tolerance, entry by entry.
58
+ *
59
+ * Vectors of different length are never the same.
60
+ * Example: [1,2,3] and [1.0001,2.0001,3.0001] with tolerance 0.001 -> true
61
+ * @param inputs - The two vectors and the tolerance
62
+ * @returns True when every entry differs by less than the tolerance
40
63
  * @group validate
41
64
  * @shortname vectors the same
42
65
  * @drawable false
66
+ * @example
67
+ * ```typescript
68
+ * const same = bitbybit.vector.vectorsTheSame({ vec1: [1, 2, 3], vec2: [1, 2, 3.0000001], tolerance: 1e-6 });
69
+ * ```
43
70
  */
44
71
  vectorsTheSame(inputs) {
45
72
  return this.geometryHelper.vectorsTheSame(inputs.vec1, inputs.vec2, inputs.tolerance);
46
73
  }
47
74
  /**
48
- * Measures the angle between two vectors in degrees (always returns positive angle 0-180°).
49
- * Example: [1,0,0] and [0,1,0] → 90° (perpendicular vectors)
50
- * @param inputs Contains two vectors represented as number arrays
75
+ * Measures the angle between two vectors in degrees, always between 0 and 180.
76
+ *
77
+ * The direction of turning is not considered; use `signedAngleBetween` for that.
78
+ * Example: [1,0,0] and [0,1,0] -> 90
79
+ * @param inputs - The two vectors
80
+ * @returns Angle in degrees
51
81
  * @group angles
52
82
  * @shortname angle
53
- * @returns Number in degrees
54
83
  * @drawable false
84
+ * @example
85
+ * ```typescript
86
+ * const angle = bitbybit.vector.angleBetween({ first: [1, 0, 0], second: [0, 1, 0] });
87
+ * ```
55
88
  */
56
89
  angleBetween(inputs) {
57
90
  return this.math.radToDeg({
@@ -59,13 +92,20 @@ export class Vector {
59
92
  });
60
93
  }
61
94
  /**
62
- * Measures the normalized 2D angle between two vectors in degrees (considers direction, can be negative).
63
- * Example: [1,0] to [0,1] → 90°, [0,1] to [1,0] → -90°
64
- * @param inputs Contains two vectors represented as number arrays
65
- * @returns Number in degrees
95
+ * Measures the signed angle from the first 2D vector to the second, in degrees from -180 to
96
+ * 180.
97
+ *
98
+ * Only the first two entries of each vector are used; a positive angle turns counter-clockwise.
99
+ * Example: [1,0] to [0,1] -> 90, [0,1] to [1,0] -> -90
100
+ * @param inputs - The two 2D vectors
101
+ * @returns Signed angle in degrees
66
102
  * @group angles
67
103
  * @shortname angle normalized 2d
68
104
  * @drawable false
105
+ * @example
106
+ * ```typescript
107
+ * const angle = bitbybit.vector.angleBetweenNormalized2d({ first: [1, 0], second: [0, 1] });
108
+ * ```
69
109
  */
70
110
  angleBetweenNormalized2d(inputs) {
71
111
  const perpDot = inputs.first[0] * inputs.second[1] - inputs.first[1] * inputs.second[0];
@@ -74,26 +114,39 @@ export class Vector {
74
114
  });
75
115
  }
76
116
  /**
77
- * Measures a positive angle between two vectors given the reference vector in degrees (always 0-360°).
78
- * Example: converts negative signed angles to positive by adding 360° when needed
79
- * @param inputs Contains information of two vectors and a reference vector
80
- * @returns Number in degrees
117
+ * Measures the angle from the first vector to the second, turning around a reference direction,
118
+ * in degrees from 0 to 360.
119
+ *
120
+ * The turn is counter-clockwise when the reference vector points toward you.
121
+ * Example: [1,0,0] to [0,0,-1] around [0,1,0] -> 90
122
+ * @param inputs - The two vectors and the reference direction to turn around
123
+ * @returns Angle in degrees from 0 to 360
81
124
  * @group angles
82
125
  * @shortname positive angle
83
126
  * @drawable false
127
+ * @example
128
+ * ```typescript
129
+ * const angle = bitbybit.vector.positiveAngleBetween({ first: [1, 0, 0], second: [0, 0, -1], reference: [0, 1, 0] });
130
+ * ```
84
131
  */
85
132
  positiveAngleBetween(inputs) {
86
133
  const angle = this.signedAngleBetween(inputs);
87
134
  return angle < 0 ? 360 + angle : angle;
88
135
  }
89
136
  /**
90
- * Adds all vector xyz values together element-wise and creates a new vector.
91
- * Example: [[1,2,3], [4,5,6], [7,8,9]] → [12,15,18] (sums each column)
92
- * @param inputs Vectors to be added
93
- * @returns New vector that has xyz values as sums of all the vectors
137
+ * Adds a list of vectors together entry by entry into one vector.
138
+ *
139
+ * The result has as many entries as the first vector.
140
+ * Example: [[1,2,3], [4,5,6], [7,8,9]] -> [12,15,18]
141
+ * @param inputs - Vectors to add
142
+ * @returns The vector of sums
94
143
  * @group sum
95
144
  * @shortname add all
96
145
  * @drawable false
146
+ * @example
147
+ * ```typescript
148
+ * const total = bitbybit.vector.addAll({ vectors: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] });
149
+ * ```
97
150
  */
98
151
  addAll(inputs) {
99
152
  const res = [];
@@ -107,13 +160,18 @@ export class Vector {
107
160
  return res;
108
161
  }
109
162
  /**
110
- * Adds two vectors together element-wise.
111
- * Example: [1,2,3] + [4,5,6] → [5,7,9]
112
- * @param inputs Two vectors to be added
113
- * @returns Number array representing vector
163
+ * Adds two vectors entry by entry.
164
+ *
165
+ * Example: [1,2,3] + [4,5,6] -> [5,7,9]
166
+ * @param inputs - The two vectors to add
167
+ * @returns The vector of sums
114
168
  * @group sum
115
169
  * @shortname add
116
170
  * @drawable false
171
+ * @example
172
+ * ```typescript
173
+ * const sum = bitbybit.vector.add({ first: [1, 2, 3], second: [4, 5, 6] });
174
+ * ```
117
175
  */
118
176
  add(inputs) {
119
177
  const res = [];
@@ -123,25 +181,37 @@ export class Vector {
123
181
  return res;
124
182
  }
125
183
  /**
126
- * Checks if the boolean array contains only true values, returns false if there's a single false.
127
- * Example: [true, true, true] → true, [true, false, true] → false
128
- * @param inputs Vectors to be checked
129
- * @returns Boolean indicating if vector contains only true values
184
+ * Tells whether every value in a list of booleans is true.
185
+ *
186
+ * Example: [true, true, true] -> true, [true, false, true] -> false
187
+ * @param inputs - The booleans to check
188
+ * @returns True when no entry is false
130
189
  * @group sum
131
190
  * @shortname all
132
191
  * @drawable false
192
+ * @example
193
+ * ```typescript
194
+ * const allTrue = bitbybit.vector.all({ vector: [true, true, false] });
195
+ * ```
133
196
  */
134
197
  all(inputs) {
135
198
  return inputs.vector.every(v => v);
136
199
  }
137
200
  /**
138
- * Computes the cross product of two 3D vectors (perpendicular vector to both inputs).
139
- * Example: [1,0,0] × [0,1,0] → [0,0,1] (right-hand rule)
140
- * @param inputs Two vectors to be crossed
201
+ * Computes the cross product of two 3D vectors: a vector at right angles to both.
202
+ *
203
+ * Its direction follows the right-hand rule and its length is the area of the parallelogram the
204
+ * two vectors span.
205
+ * Example: [1,0,0] x [0,1,0] -> [0,0,1]
206
+ * @param inputs - The two 3D vectors
207
+ * @returns The vector perpendicular to both
141
208
  * @group base
142
209
  * @shortname cross
143
- * @returns Crossed vector
144
210
  * @drawable false
211
+ * @example
212
+ * ```typescript
213
+ * const normal = bitbybit.vector.cross({ first: [1, 0, 0], second: [0, 1, 0] });
214
+ * ```
145
215
  */
146
216
  cross(inputs) {
147
217
  const res = [];
@@ -151,13 +221,19 @@ export class Vector {
151
221
  return res;
152
222
  }
153
223
  /**
154
- * Calculates squared distance between two vectors (faster than distance, avoids sqrt).
155
- * Example: [0,0,0] to [3,4,0] → 25 (distance 5 squared)
156
- * @param inputs Two vectors
157
- * @returns Number representing squared distance between two vectors
224
+ * Computes the squared distance between two vectors, which avoids the square root when only
225
+ * comparing distances.
226
+ *
227
+ * Example: [0,0,0] to [3,4,0] -> 25
228
+ * @param inputs - The two vectors
229
+ * @returns The squared distance
158
230
  * @group distance
159
231
  * @shortname dist squared
160
232
  * @drawable false
233
+ * @example
234
+ * ```typescript
235
+ * const d2 = bitbybit.vector.distSquared({ first: [0, 0, 0], second: [3, 4, 0] });
236
+ * ```
161
237
  */
162
238
  distSquared(inputs) {
163
239
  let res = 0;
@@ -167,25 +243,35 @@ export class Vector {
167
243
  return res;
168
244
  }
169
245
  /**
170
- * Calculates the Euclidean distance between two vectors.
171
- * Example: [0,0,0] to [3,4,0] → 5, [1,1] to [4,5] → 5
172
- * @param inputs Two vectors
173
- * @returns Number representing distance between two vectors
246
+ * Computes the straight-line distance between two vectors.
247
+ *
248
+ * Example: [0,0,0] to [3,4,0] -> 5
249
+ * @param inputs - The two vectors
250
+ * @returns The distance in model units
174
251
  * @group distance
175
252
  * @shortname dist
176
253
  * @drawable false
254
+ * @example
255
+ * ```typescript
256
+ * const distance = bitbybit.vector.dist({ first: [0, 0, 0], second: [3, 4, 0] });
257
+ * ```
177
258
  */
178
259
  dist(inputs) {
179
260
  return Math.sqrt(this.distSquared(inputs));
180
261
  }
181
262
  /**
182
- * Divides each element of the vector by a scalar value.
183
- * Example: [10,20,30] ÷ 2 → [5,10,15]
184
- * @param inputs Contains vector and a scalar
185
- * @returns Vector that is a result of division by a scalar
263
+ * Divides every entry of a vector by one number.
264
+ *
265
+ * Example: [10,20,30] / 2 -> [5,10,15]
266
+ * @param inputs - The vector and the number to divide by
267
+ * @returns The divided vector
186
268
  * @group base
187
269
  * @shortname div
188
270
  * @drawable false
271
+ * @example
272
+ * ```typescript
273
+ * const half = bitbybit.vector.div({ vector: [10, 20, 30], scalar: 2 });
274
+ * ```
189
275
  */
190
276
  div(inputs) {
191
277
  const res = [];
@@ -195,25 +281,37 @@ export class Vector {
195
281
  return res;
196
282
  }
197
283
  /**
198
- * Computes the domain (range) between minimum and maximum values of the vector.
199
- * Example: [1,3,5,9] → 8 (difference between last and first: 9-1)
200
- * @param inputs Vector information
201
- * @returns Number representing distance between two vectors
284
+ * Subtracts the first value of a vector from its last, which for a sorted list is its range.
285
+ *
286
+ * Example: [1,3,5,9] -> 8
287
+ * @param inputs - The vector
288
+ * @returns Last value minus first value
202
289
  * @group base
203
290
  * @shortname domain
204
291
  * @drawable false
292
+ * @example
293
+ * ```typescript
294
+ * const span = bitbybit.vector.domain({ vector: [1, 3, 5, 9] });
295
+ * ```
205
296
  */
206
297
  domain(inputs) {
207
298
  return inputs.vector[inputs.vector.length - 1] - inputs.vector[0];
208
299
  }
209
300
  /**
210
- * Calculates the dot product between two vectors (measures similarity/projection).
211
- * Example: [1,2,3] • [4,5,6] → 32 (1×4 + 2×5 + 3×6), perpendicular vectors → 0
212
- * @param inputs Two vectors
213
- * @returns Number representing dot product of the vector
301
+ * Computes the dot product of two vectors: the sum of the products of matching entries.
302
+ *
303
+ * It is 0 for vectors at right angles and, for unit vectors, the cosine of the angle between
304
+ * them.
305
+ * Example: [1,2,3] and [4,5,6] -> 32
306
+ * @param inputs - The two vectors
307
+ * @returns The dot product
214
308
  * @group base
215
309
  * @shortname dot
216
310
  * @drawable false
311
+ * @example
312
+ * ```typescript
313
+ * const projection = bitbybit.vector.dot({ first: [1, 2, 3], second: [4, 5, 6] });
314
+ * ```
217
315
  */
218
316
  dot(inputs) {
219
317
  let res = 0;
@@ -223,38 +321,53 @@ export class Vector {
223
321
  return res;
224
322
  }
225
323
  /**
226
- * Checks if each element in the vector is finite and returns a boolean array.
227
- * Example: [1, 2, Infinity, 3] → [true, true, false, true]
228
- * @param inputs Vector with possibly infinite values
229
- * @returns Vector array that contains boolean values for each number in the input
230
- * vector that identifies if value is finite (true) or infinite (false)
324
+ * Marks which entries of a vector are finite numbers.
325
+ *
326
+ * Example: [1, 2, Infinity, 3] -> [true, true, false, true]
327
+ * @param inputs - The vector to check
328
+ * @returns One boolean per entry, true when it is finite
231
329
  * @group validate
232
330
  * @shortname finite
233
331
  * @drawable false
332
+ * @example
333
+ * ```typescript
334
+ * const flags = bitbybit.vector.finite({ vector: [1, Infinity, 3] });
335
+ * ```
234
336
  */
235
337
  finite(inputs) {
236
338
  return inputs.vector.map(v => isFinite(v));
237
339
  }
238
340
  /**
239
- * Checks if the vector has zero length (all elements are zero).
240
- * Example: [0,0,0] → true, [0,0,0.001] → false
241
- * @param inputs Vector to be checked
242
- * @returns Boolean that identifies if vector is zero length
341
+ * Tells whether a vector has no length, that is, every entry is exactly 0.
342
+ *
343
+ * Example: [0,0,0] -> true, [0,0,0.001] -> false
344
+ * @param inputs - The vector to check
345
+ * @returns True when the length is 0
243
346
  * @group validate
244
347
  * @shortname isZero
245
348
  * @drawable false
349
+ * @example
350
+ * ```typescript
351
+ * const zero = bitbybit.vector.isZero({ vector: [0, 0, 0] });
352
+ * ```
246
353
  */
247
354
  isZero(inputs) {
248
355
  return this.norm({ vector: inputs.vector }) === 0;
249
356
  }
250
357
  /**
251
- * Finds an interpolated vector between two vectors using a fraction (linear interpolation).
252
- * Example: [0,0,0] to [10,10,10] at 0.5 → [5,5,5], fraction=0 → first, fraction=1 → second
253
- * @param inputs Information for finding vector between two vectors using a fraction
254
- * @returns Vector that is in between two vectors
358
+ * Blends two vectors linearly by a fraction.
359
+ *
360
+ * `fraction` is the share of `first`: 1 gives `first`, 0 gives `second`, 0.5 the midpoint.
361
+ * Example: [0,0,0] and [10,10,10] at 0.5 -> [5,5,5]
362
+ * @param inputs - The two vectors and the fraction of the first
363
+ * @returns The blended vector
255
364
  * @group distance
256
365
  * @shortname lerp
257
366
  * @drawable false
367
+ * @example
368
+ * ```typescript
369
+ * const mid = bitbybit.vector.lerp({ first: [0, 0, 0], second: [10, 10, 10], fraction: 0.5 });
370
+ * ```
258
371
  */
259
372
  lerp(inputs) {
260
373
  return this.add({
@@ -263,37 +376,52 @@ export class Vector {
263
376
  });
264
377
  }
265
378
  /**
266
- * Finds the maximum (largest) value in the vector.
267
- * Example: [3, 7, 2, 9, 1] → 9
268
- * @param inputs Vector to be checked
269
- * @returns Largest number in the vector
379
+ * Finds the largest value in a vector.
380
+ *
381
+ * Example: [3, 7, 2, 9, 1] -> 9
382
+ * @param inputs - The vector
383
+ * @returns The largest entry
270
384
  * @group extract
271
385
  * @shortname max
272
386
  * @drawable false
387
+ * @example
388
+ * ```typescript
389
+ * const largest = bitbybit.vector.max({ vector: [3, 7, 2, 9, 1] });
390
+ * ```
273
391
  */
274
392
  max(inputs) {
275
393
  return Math.max(...inputs.vector);
276
394
  }
277
395
  /**
278
- * Finds the minimum (smallest) value in the vector.
279
- * Example: [3, 7, 2, 9, 1] → 1
280
- * @param inputs Vector to be checked
281
- * @returns Lowest number in the vector
396
+ * Finds the smallest value in a vector.
397
+ *
398
+ * Example: [3, 7, 2, 9, 1] -> 1
399
+ * @param inputs - The vector
400
+ * @returns The smallest entry
282
401
  * @group extract
283
402
  * @shortname min
284
403
  * @drawable false
404
+ * @example
405
+ * ```typescript
406
+ * const smallest = bitbybit.vector.min({ vector: [3, 7, 2, 9, 1] });
407
+ * ```
285
408
  */
286
409
  min(inputs) {
287
410
  return Math.min(...inputs.vector);
288
411
  }
289
412
  /**
290
- * Multiplies each element of the vector by a scalar value.
291
- * Example: [2,3,4] × 5 → [10,15,20]
292
- * @param inputs Vector with a scalar
293
- * @returns Vector that results from multiplication
413
+ * Multiplies every entry of a vector by one number.
414
+ *
415
+ * Example: [2,3,4] x 5 -> [10,15,20]
416
+ * @param inputs - The vector and the number to multiply by
417
+ * @returns The scaled vector
294
418
  * @group base
295
419
  * @shortname mul
296
420
  * @drawable false
421
+ * @example
422
+ * ```typescript
423
+ * const scaled = bitbybit.vector.mul({ vector: [2, 3, 4], scalar: 5 });
424
+ * ```
297
425
  */
298
426
  mul(inputs) {
299
427
  const res = [];
@@ -303,13 +431,18 @@ export class Vector {
303
431
  return res;
304
432
  }
305
433
  /**
306
- * Negates the vector (flips the sign of each element).
307
- * Example: [5,-3,2] → [-5,3,-2]
308
- * @param inputs Vector to negate
309
- * @returns Negative vector
434
+ * Flips the sign of every entry, so the vector points the opposite way.
435
+ *
436
+ * Example: [5,-3,2] -> [-5,3,-2]
437
+ * @param inputs - The vector to flip
438
+ * @returns The negated vector
310
439
  * @group base
311
440
  * @shortname neg
312
441
  * @drawable false
442
+ * @example
443
+ * ```typescript
444
+ * const opposite = bitbybit.vector.neg({ vector: [5, -3, 2] });
445
+ * ```
313
446
  */
314
447
  neg(inputs) {
315
448
  const res = [];
@@ -319,38 +452,55 @@ export class Vector {
319
452
  return res;
320
453
  }
321
454
  /**
322
- * Computes the squared norm (squared magnitude/length) of the vector.
323
- * Example: [3,4,0] → 25 (length 5 squared)
324
- * @param inputs Vector for squared norm
325
- * @returns Number that is squared norm
455
+ * Computes the squared length of a vector, which avoids the square root when only comparing
456
+ * lengths.
457
+ *
458
+ * Example: [3,4,0] -> 25
459
+ * @param inputs - The vector
460
+ * @returns The squared length
326
461
  * @group base
327
462
  * @shortname norm squared
328
463
  * @drawable false
464
+ * @example
465
+ * ```typescript
466
+ * const n2 = bitbybit.vector.normSquared({ vector: [3, 4, 0] });
467
+ * ```
329
468
  */
330
469
  normSquared(inputs) {
331
470
  return this.dot({ first: inputs.vector, second: inputs.vector });
332
471
  }
333
472
  /**
334
- * Calculates the norm (magnitude/length) of the vector.
335
- * Example: [3,4,0] → 5, [1,0,0] → 1
336
- * @param inputs Vector to compute the norm
337
- * @returns Number that is norm of the vector
473
+ * Computes the length of a vector.
474
+ *
475
+ * Example: [3,4,0] -> 5, [1,0,0] -> 1
476
+ * @param inputs - The vector
477
+ * @returns The length in model units
338
478
  * @group base
339
479
  * @shortname norm
340
480
  * @drawable false
481
+ * @example
482
+ * ```typescript
483
+ * const len = bitbybit.vector.norm({ vector: [3, 4, 0] });
484
+ * ```
341
485
  */
342
486
  norm(inputs) {
343
487
  const norm2 = this.normSquared(inputs);
344
488
  return norm2 !== 0.0 ? Math.sqrt(norm2) : norm2;
345
489
  }
346
490
  /**
347
- * Normalizes the vector into a unit vector that has a length of 1 (maintains direction, scales magnitude to 1).
348
- * Example: [3,4,0] → [0.6,0.8,0], [10,0,0] → [1,0,0]
349
- * @param inputs Vector to normalize
350
- * @returns Unit vector that has length of 1
491
+ * Scales a 3D vector to length 1 while keeping its direction.
492
+ *
493
+ * A vector shorter than 1e-8 has no direction to keep, so the result is undefined.
494
+ * Example: [3,4,0] -> [0.6,0.8,0]
495
+ * @param inputs - The 3D vector to normalize
496
+ * @returns The unit vector, or undefined for a zero-length input
351
497
  * @group base
352
498
  * @shortname normalized
353
499
  * @drawable false
500
+ * @example
501
+ * ```typescript
502
+ * const direction = bitbybit.vector.normalized({ vector: [3, 4, 0] });
503
+ * ```
354
504
  */
355
505
  normalized(inputs) {
356
506
  const len = this.length({ vector: inputs.vector });
@@ -360,49 +510,70 @@ export class Vector {
360
510
  return this.div({ scalar: this.norm(inputs), vector: inputs.vector });
361
511
  }
362
512
  /**
363
- * Finds a point on a ray at a given distance from the origin along the direction vector.
364
- * Example: Point [0,0,0] + direction [1,0,0] at distance 5 → [5,0,0]
365
- * @param inputs Provide a point, vector and a distance for finding a point
366
- * @returns Vector representing point on the ray
513
+ * Finds the point at a given distance from a start point along a direction.
514
+ *
515
+ * The direction is used as given, so a direction of length 2 travels twice the distance.
516
+ * Example: start [0,0,0], direction [1,0,0], distance 5 -> [5,0,0]
517
+ * @param inputs - The start point, the direction and the distance
518
+ * @returns The point on the ray
367
519
  * @group base
368
520
  * @shortname on ray
369
521
  * @drawable false
522
+ * @example
523
+ * ```typescript
524
+ * const ahead = bitbybit.vector.onRay({ point: [0, 0, 0], vector: [1, 0, 0], distance: 5 });
525
+ * ```
370
526
  */
371
527
  onRay(inputs) {
372
528
  return this.add({ first: inputs.point, second: this.mul({ vector: inputs.vector, scalar: inputs.distance }) });
373
529
  }
374
530
  /**
375
- * Creates a 3D vector from x, y, z coordinates.
376
- * Example: x=1, y=2, z=3 → [1,2,3]
377
- * @param inputs Vector coordinates
378
- * @returns Create a vector of xyz values
531
+ * Builds a 3D vector from its x, y and z values.
532
+ *
533
+ * Example: x=1, y=2, z=3 -> [1,2,3]
534
+ * @param inputs - The three values
535
+ * @returns The vector `[x, y, z]`
379
536
  * @group create
380
537
  * @shortname vector XYZ
381
538
  * @drawable true
539
+ * @example
540
+ * ```typescript
541
+ * const up = bitbybit.vector.vectorXYZ({ x: 0, y: 1, z: 0 });
542
+ * ```
382
543
  */
383
544
  vectorXYZ(inputs) {
384
545
  return [inputs.x, inputs.y, inputs.z];
385
546
  }
386
547
  /**
387
- * Creates a 2D vector from x, y coordinates.
388
- * Example: x=3, y=4 → [3,4]
389
- * @param inputs Vector coordinates
390
- * @returns Create a vector of xy values
548
+ * Builds a 2D vector from its x and y values.
549
+ *
550
+ * Example: x=3, y=4 -> [3,4]
551
+ * @param inputs - The two values
552
+ * @returns The vector `[x, y]`
391
553
  * @group create
392
554
  * @shortname vector XY
393
555
  * @drawable true
556
+ * @example
557
+ * ```typescript
558
+ * const right = bitbybit.vector.vectorXY({ x: 1, y: 0 });
559
+ * ```
394
560
  */
395
561
  vectorXY(inputs) {
396
562
  return [inputs.x, inputs.y];
397
563
  }
398
564
  /**
399
- * Creates a vector of integers from 0 to max (exclusive).
400
- * Example: max=5 → [0,1,2,3,4], max=3 → [0,1,2]
401
- * @param inputs Max value for the range
402
- * @returns Vector containing items from 0 to max
565
+ * Lists the whole numbers from 0 up to, but not including, `max`.
566
+ *
567
+ * Example: max=5 -> [0,1,2,3,4]
568
+ * @param inputs - The end of the range, which is left out
569
+ * @returns The numbers from 0 to max - 1
403
570
  * @group create
404
571
  * @shortname range
405
572
  * @drawable false
573
+ * @example
574
+ * ```typescript
575
+ * const indices = bitbybit.vector.range({ max: 5 });
576
+ * ```
406
577
  */
407
578
  range(inputs) {
408
579
  const res = [];
@@ -412,13 +583,21 @@ export class Vector {
412
583
  return res;
413
584
  }
414
585
  /**
415
- * Computes signed angle between two vectors using a reference vector (determines rotation direction).
416
- * Example: Returns positive or negative angle depending on rotation direction relative to reference
417
- * @param inputs Contains information of two vectors and a reference vector
418
- * @returns Signed angle in degrees
586
+ * Measures the angle from the first vector to the second, turning around a reference direction,
587
+ * in degrees from 0 to 360.
588
+ *
589
+ * The turn is counter-clockwise when the reference vector points toward you: a clockwise turn
590
+ * of 30 degrees reads as 330.
591
+ * Example: [1,0,0] to [0,0,-1] around [0,1,0] -> 90
592
+ * @param inputs - The two vectors and the reference direction to turn around
593
+ * @returns Angle in degrees from 0 to 360
419
594
  * @group angles
420
595
  * @shortname signed angle
421
596
  * @drawable false
597
+ * @example
598
+ * ```typescript
599
+ * const angle = bitbybit.vector.signedAngleBetween({ first: [1, 0, 0], second: [0, 0, -1], reference: [0, 1, 0] });
600
+ * ```
422
601
  */
423
602
  signedAngleBetween(inputs) {
424
603
  const nab = this.cross({ first: inputs.first, second: inputs.second });
@@ -434,13 +613,19 @@ export class Vector {
434
613
  return this.math.radToDeg({ number: res });
435
614
  }
436
615
  /**
437
- * Creates a vector containing numbers from min to max at a given step increment.
438
- * Example: min=0, max=10, step=2 → [0,2,4,6,8,10]
439
- * @param inputs Span information containing min, max and step values
440
- * @returns Vector containing number between min, max and increasing at a given step
616
+ * Lists the numbers from `min` to `max`, stepping by `step`; `max` is included when a step
617
+ * lands on it.
618
+ *
619
+ * Example: min=0, max=10, step=2 -> [0,2,4,6,8,10]
620
+ * @param inputs - The start, the end and the step
621
+ * @returns The numbers in the span
441
622
  * @group create
442
623
  * @shortname span
443
624
  * @drawable false
625
+ * @example
626
+ * ```typescript
627
+ * const values = bitbybit.vector.span({ min: 0, max: 10, step: 2.5 });
628
+ * ```
444
629
  */
445
630
  span(inputs) {
446
631
  const res = [];
@@ -450,13 +635,21 @@ export class Vector {
450
635
  return res;
451
636
  }
452
637
  /**
453
- * Creates a vector with numbers from min to max using an easing function for non-linear distribution.
454
- * Example: min=0, max=100, nrItems=5, ease='easeInQuad' → creates accelerating intervals
455
- * @param inputs Span information containing min, max and ease function
456
- * @returns Vector containing numbers between min, max and increasing in non-linear steps defined by nr of items in the vector and type
638
+ * Lists `nrItems` numbers from `min` to `max` spaced by an easing curve, so they bunch up at
639
+ * one end or both.
640
+ *
641
+ * With `intervals` on, the result holds the gaps between neighbors instead of the values
642
+ * themselves.
643
+ * Example: min=0, max=100, nrItems=5, ease='easeInQuad' -> [0, 6.25, 25, 56.25, 100]
644
+ * @param inputs - The start, the end, the number of items, the easing and whether to return the gaps
645
+ * @returns The eased numbers, or the gaps between them
457
646
  * @group create
458
647
  * @shortname span ease items
459
648
  * @drawable false
649
+ * @example
650
+ * ```typescript
651
+ * const eased = bitbybit.vector.spanEaseItems({ min: 0, max: 100, nrItems: 5, ease: Bit.Inputs.Math.easeEnum.easeInQuad, intervals: false });
652
+ * ```
460
653
  */
461
654
  spanEaseItems(inputs) {
462
655
  const res = [];
@@ -470,13 +663,18 @@ export class Vector {
470
663
  return res;
471
664
  }
472
665
  /**
473
- * Creates a vector with evenly spaced numbers from min to max with a specified number of items.
474
- * Example: min=0, max=10, nrItems=5 → [0, 2.5, 5, 7.5, 10]
475
- * @param inputs Span information containing min, max and step values
476
- * @returns Vector containing number between min, max by giving nr of items
666
+ * Lists `nrItems` evenly spaced numbers from `min` to `max`, both included.
667
+ *
668
+ * Example: min=0, max=10, nrItems=5 -> [0, 2.5, 5, 7.5, 10]
669
+ * @param inputs - The start, the end and the number of items
670
+ * @returns The evenly spaced numbers
477
671
  * @group create
478
672
  * @shortname span linear items
479
673
  * @drawable false
674
+ * @example
675
+ * ```typescript
676
+ * const values = bitbybit.vector.spanLinearItems({ min: 0, max: 10, nrItems: 5 });
677
+ * ```
480
678
  */
481
679
  spanLinearItems(inputs) {
482
680
  const res = [];
@@ -488,13 +686,18 @@ export class Vector {
488
686
  return res;
489
687
  }
490
688
  /**
491
- * Subtracts the second vector from the first element-wise.
492
- * Example: [10,20,30] - [1,2,3] → [9,18,27]
493
- * @param inputs Two vectors
494
- * @returns Vector that result by subtraction two vectors
689
+ * Subtracts the second vector from the first, entry by entry.
690
+ *
691
+ * Example: [10,20,30] - [1,2,3] -> [9,18,27]
692
+ * @param inputs - The vector to subtract from and the vector to subtract
693
+ * @returns The vector of differences
495
694
  * @group base
496
695
  * @shortname sub
497
696
  * @drawable false
697
+ * @example
698
+ * ```typescript
699
+ * const diff = bitbybit.vector.sub({ first: [10, 20, 30], second: [1, 2, 3] });
700
+ * ```
498
701
  */
499
702
  sub(inputs) {
500
703
  const res = [];
@@ -504,50 +707,72 @@ export class Vector {
504
707
  return res;
505
708
  }
506
709
  /**
507
- * Sums all values in the vector and returns a single number.
508
- * Example: [1,2,3,4] → 10, [5,10,15] → 30
509
- * @param inputs Vector to sum
510
- * @returns Number that results by adding up all values in the vector
710
+ * Adds up all values of a vector into one number.
711
+ *
712
+ * Example: [1,2,3,4] -> 10
713
+ * @param inputs - The vector to add up
714
+ * @returns The total
511
715
  * @group base
512
716
  * @shortname sum
513
717
  * @drawable false
718
+ * @example
719
+ * ```typescript
720
+ * const total = bitbybit.vector.sum({ vector: [1, 2, 3, 4] });
721
+ * ```
514
722
  */
515
723
  sum(inputs) {
516
724
  return inputs.vector.reduce((a, b) => a + b, 0);
517
725
  }
518
726
  /**
519
- * Computes the squared length (squared magnitude) of a 3D vector.
520
- * Example: [3,4,0] → 25 (length 5 squared)
521
- * @param inputs Vector to compute the length
522
- * @returns Number that is squared length of the vector
727
+ * Computes the squared length of a 3D vector, which avoids the square root when only comparing
728
+ * lengths.
729
+ *
730
+ * Example: [3,4,0] -> 25
731
+ * @param inputs - The 3D vector
732
+ * @returns The squared length
523
733
  * @group base
524
734
  * @shortname length squared
525
735
  * @drawable false
736
+ * @example
737
+ * ```typescript
738
+ * const l2 = bitbybit.vector.lengthSq({ vector: [3, 4, 0] });
739
+ * ```
526
740
  */
527
741
  lengthSq(inputs) {
528
742
  const v = inputs.vector;
529
743
  return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
530
744
  }
531
745
  /**
532
- * Computes the length (magnitude) of a 3D vector.
533
- * Example: [3,4,0] → 5, [1,0,0] → 1
534
- * @param inputs Vector to compute the length
535
- * @returns Number that is length of the vector
746
+ * Computes the length of a 3D vector.
747
+ *
748
+ * Example: [3,4,0] -> 5
749
+ * @param inputs - The 3D vector
750
+ * @returns The length in model units
536
751
  * @group base
537
752
  * @shortname length
538
753
  * @drawable false
754
+ * @example
755
+ * ```typescript
756
+ * const len = bitbybit.vector.length({ vector: [3, 4, 0] });
757
+ * ```
539
758
  */
540
759
  length(inputs) {
541
760
  return Math.sqrt(this.lengthSq(inputs));
542
761
  }
543
762
  /**
544
- * Converts an array of stringified numbers to actual numbers.
545
- * Example: ['1', '2.5', '3'] → [1, 2.5, 3], ['10', '-5', '0.1'] → [10, -5, 0.1]
546
- * @param inputs Array of stringified numbers
547
- * @returns Array of numbers
763
+ * Turns a list of number strings into numbers.
764
+ *
765
+ * A string that is not a number becomes NaN.
766
+ * Example: ['1', '2.5', '3'] -> [1, 2.5, 3]
767
+ * @param inputs - The strings to parse
768
+ * @returns The numbers
548
769
  * @group create
549
770
  * @shortname parse numbers
550
771
  * @drawable false
772
+ * @example
773
+ * ```typescript
774
+ * const numbers = bitbybit.vector.parseNumbers({ vector: ["1", "2.5", "-3"] });
775
+ * ```
551
776
  */
552
777
  parseNumbers(inputs) {
553
778
  return inputs.vector.map(v => parseFloat(v));