toosoon-utils 4.1.9 → 4.2.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 (62) hide show
  1. package/README.md +97 -885
  2. package/lib/constants.d.ts +0 -1
  3. package/lib/constants.js +3 -1
  4. package/lib/extras/color-scale/ColorScale.d.ts +2 -2
  5. package/lib/extras/color-scale/ColorScale.js +2 -2
  6. package/lib/extras/curves/CatmullRomCurve.d.ts +20 -5
  7. package/lib/extras/curves/CatmullRomCurve.js +23 -5
  8. package/lib/extras/curves/CatmullRomCurve3.d.ts +101 -0
  9. package/lib/extras/curves/CatmullRomCurve3.js +122 -0
  10. package/lib/extras/curves/CubicBezierCurve.d.ts +20 -5
  11. package/lib/extras/curves/CubicBezierCurve.js +23 -5
  12. package/lib/extras/curves/CubicBezierCurve3.d.ts +101 -0
  13. package/lib/extras/curves/CubicBezierCurve3.js +122 -0
  14. package/lib/extras/curves/Curve.d.ts +23 -24
  15. package/lib/extras/curves/Curve.js +19 -26
  16. package/lib/extras/curves/EllipseCurve.d.ts +21 -5
  17. package/lib/extras/curves/EllipseCurve.js +55 -6
  18. package/lib/extras/curves/LineCurve.d.ts +34 -10
  19. package/lib/extras/curves/LineCurve.js +35 -13
  20. package/lib/extras/curves/LineCurve3.d.ts +87 -0
  21. package/lib/extras/curves/LineCurve3.js +108 -0
  22. package/lib/extras/curves/PolylineCurve.d.ts +9 -8
  23. package/lib/extras/curves/PolylineCurve.js +6 -6
  24. package/lib/extras/curves/PolylineCurve3.d.ts +28 -0
  25. package/lib/extras/curves/PolylineCurve3.js +39 -0
  26. package/lib/extras/curves/QuadraticBezierCurve.d.ts +19 -5
  27. package/lib/extras/curves/QuadraticBezierCurve.js +22 -5
  28. package/lib/extras/curves/QuadraticBezierCurve3.d.ts +84 -0
  29. package/lib/extras/curves/QuadraticBezierCurve3.js +102 -0
  30. package/lib/extras/curves/SplineCurve.d.ts +12 -8
  31. package/lib/extras/curves/SplineCurve.js +9 -6
  32. package/lib/extras/curves/SplineCurve3.d.ts +28 -0
  33. package/lib/extras/curves/SplineCurve3.js +41 -0
  34. package/lib/extras/curves/index.d.ts +6 -0
  35. package/lib/extras/curves/index.js +6 -0
  36. package/lib/extras/geometry/Matrix2.d.ts +1 -0
  37. package/lib/extras/geometry/Matrix2.js +230 -0
  38. package/lib/extras/geometry/Matrix4.d.ts +1 -0
  39. package/lib/extras/geometry/Matrix4.js +632 -0
  40. package/lib/extras/geometry/Vector.d.ts +42 -0
  41. package/lib/extras/geometry/Vector.js +1 -0
  42. package/lib/extras/geometry/Vector2.d.ts +480 -0
  43. package/lib/extras/geometry/Vector2.js +709 -0
  44. package/lib/extras/geometry/Vector3.d.ts +486 -0
  45. package/lib/extras/geometry/Vector3.js +765 -0
  46. package/lib/extras/geometry/index.d.ts +3 -0
  47. package/lib/extras/geometry/index.js +2 -0
  48. package/lib/extras/paths/Path.d.ts +24 -18
  49. package/lib/extras/paths/Path.js +48 -35
  50. package/lib/extras/paths/PathContext.d.ts +97 -67
  51. package/lib/extras/paths/PathContext.js +326 -183
  52. package/lib/extras/paths/PathSVG.d.ts +43 -31
  53. package/lib/extras/paths/PathSVG.js +69 -56
  54. package/lib/geometry.d.ts +0 -135
  55. package/lib/geometry.js +1 -219
  56. package/lib/maths.d.ts +54 -22
  57. package/lib/maths.js +77 -27
  58. package/lib/random.d.ts +12 -16
  59. package/lib/random.js +19 -27
  60. package/lib/tsconfig.tsbuildinfo +1 -1
  61. package/lib/types.d.ts +43 -1
  62. package/package.json +2 -1
package/lib/geometry.js CHANGED
@@ -1,5 +1,4 @@
1
- import { EPSILON, PI, TWO_PI } from './constants';
2
- import { lerp } from './maths';
1
+ import { PI } from './constants';
3
2
  /**
4
3
  * Convert a radians value into degrees
5
4
  *
@@ -55,30 +54,6 @@ export function distance(x1, y1, x2, y2) {
55
54
  const dy = y1 - y2;
56
55
  return Math.sqrt(dx * dx + dy * dy);
57
56
  }
58
- /**
59
- * Calculate the dot product of two vectors
60
- *
61
- * @param {number} x1 X-axis coordinate of the first vector
62
- * @param {number} y1 Y-axis coordinate of the first vector
63
- * @param {number} x2 X-axis coordinate of the second vector
64
- * @param {number} y2 Y-axis coordinate of the second vector
65
- * @returns {number} Computed dot product
66
- */
67
- export function dot(x1, y1, x2, y2) {
68
- return x1 * x2 + y1 * y2;
69
- }
70
- /**
71
- * Calculate the cross product of two vectors
72
- *
73
- * @param {number} x1 X-axis coordinate of the first vector
74
- * @param {number} y1 Y-axis coordinate of the first vector
75
- * @param {number} x2 X-axis coordinate of the second vector
76
- * @param {number} y2 Y-axis coordinate of the second vector
77
- * @returns {number} Computed cross product
78
- */
79
- export function cross(x1, y1, x2, y2) {
80
- return x1 * x2 - y1 * y2;
81
- }
82
57
  /**
83
58
  * Calculate the length of the diagonal of a rectangle
84
59
  *
@@ -89,199 +64,6 @@ export function cross(x1, y1, x2, y2) {
89
64
  export function diagonal(width, height) {
90
65
  return Math.sqrt(width * width + height * height);
91
66
  }
92
- /**
93
- * Convert radians to a 3D point on the surface of a unit sphere
94
- *
95
- * @param {number} radius Radius of the sphere
96
- * @param {number} phi Polar angle from the y (up) axis : [0, PI]
97
- * @param {number} theta Equator angle around the y (up) axis : [0, 2*PI]
98
- * @param {Point3} target Target 3D point
99
- * @returns {Point3}
100
- */
101
- export function radToSphere(radius, phi, theta, target = [0, 0, 0]) {
102
- target[0] = radius * Math.sin(phi) * Math.sin(theta);
103
- target[1] = radius * Math.cos(phi);
104
- target[2] = radius * Math.sin(phi) * Math.cos(theta);
105
- return target;
106
- }
107
- // *********************
108
- // Curves
109
- // *********************
110
- /**
111
- * Interpolate a point on a line
112
- *
113
- * @param {number} t Normalized time value to interpolate
114
- * @param {number} x1 X-axis coordinate of the start point
115
- * @param {number} y1 Y-axis coordinate of the start point
116
- * @param {number} x2 X-axis coordinate of the end point
117
- * @param {number} y2 Y-axis coordinate of the end point
118
- * @returns {Point} Interpolated coordinates on the line
119
- */
120
- export function line(t, x1, y1, x2, y2) {
121
- const x = lerp(t, x1, x2);
122
- const y = lerp(t, y1, y2);
123
- return [x, y];
124
- }
125
- /**
126
- * Interpolate a point on a Quadratic Bézier curve
127
- *
128
- * @param {number} t Normalized time value to interpolate
129
- * @param {number} x1 X-axis coordinate of the start point
130
- * @param {number} y1 Y-axis coordinate of the start point
131
- * @param {number} cpx X-axis coordinate of the control point
132
- * @param {number} cpy Y-axis coordinate of the control point
133
- * @param {number} x2 X-axis coordinate of the end point
134
- * @param {number} y2 Y-axis coordinate of the end point
135
- * @returns {Point} Interpolated coordinates on the curve
136
- */
137
- export function quadraticBezier(t, x1, y1, cpx, cpy, x2, y2) {
138
- const t2 = t * t;
139
- const k = 1 - t;
140
- const k2 = k * k;
141
- const x = k2 * x1 + 2 * k * t * cpx + t2 * x2;
142
- const y = k2 * y1 + 2 * k * t * cpy + t2 * y2;
143
- return [x, y];
144
- }
145
- /**
146
- * Interpolate a point on a Cubic Bézier curve
147
- *
148
- * @param {number} t Normalized time value to interpolate
149
- * @param {number} x1 X-axis coordinate of the start point
150
- * @param {number} y1 Y-axis coordinate of the start point
151
- * @param {number} cp1x X-axis coordinate of the first control point
152
- * @param {number} cp1y Y-axis coordinate of the first control point
153
- * @param {number} cp2x X-axis coordinate of the second control point
154
- * @param {number} cp2y Y-axis coordinate of the second control point
155
- * @param {number} x2 X-axis coordinate of the end point
156
- * @param {number} y2 Y-axis coordinate of the end point
157
- * @returns {Point} Interpolated coordinates on the curve
158
- */
159
- export function cubicBezier(t, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2) {
160
- const t2 = t * t;
161
- const t3 = t2 * t;
162
- const k = 1 - t;
163
- const k2 = k * k;
164
- const k3 = k2 * k;
165
- const x = k3 * x1 + 3 * k2 * t * cp1x + 3 * k * t2 * cp2x + t3 * x2;
166
- const y = k3 * y1 + 3 * k2 * t * cp1y + 3 * k * t2 * cp2y + t3 * y2;
167
- return [x, y];
168
- }
169
- /**
170
- * Interpolate a point on a Catmull-Rom spline
171
- *
172
- * @param {number} t Normalized time value to interpolate
173
- * @param {number} x1 X-axis coordinate of the start point
174
- * @param {number} y1 Y-axis coordinate of the start point
175
- * @param {number} cp1x X-axis coordinate of the first control point
176
- * @param {number} cp1y Y-axis coordinate of the first control point
177
- * @param {number} cp2x X-axis coordinate of the second control point
178
- * @param {number} cp2y Y-axis coordinate of the second control point
179
- * @param {number} x2 X-axis coordinate of the end point
180
- * @param {number} y2 Y-axis coordinate of the end point
181
- * @returns {Point} Interpolated coordinates on the spline
182
- */
183
- export function catmullRom(t, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2) {
184
- const t2 = t * t;
185
- const t3 = t2 * t;
186
- const vx1 = (cp2x - x1) * 0.5;
187
- const vy1 = (cp2y - y1) * 0.5;
188
- const vx2 = (x2 - cp1x) * 0.5;
189
- const vy2 = (y2 - cp1y) * 0.5;
190
- const x = (2 * cp1x - 2 * cp2x + vx1 + vx2) * t3 + (-3 * cp1x + 3 * cp2x - 2 * vx1 - vx2) * t2 + vx1 * t + cp1x;
191
- const y = (2 * cp1y - 2 * cp2y + vy1 + vy2) * t3 + (-3 * cp1y + 3 * cp2y - 2 * vy1 - vy2) * t2 + vy1 * t + cp1y;
192
- return [x, y];
193
- }
194
- /**
195
- * Interpolate a point on an elliptical arc
196
- *
197
- * @param {number} t Normalized time value to interpolate
198
- * @param {number} cx X-axis coordinate of the center of the ellipse
199
- * @param {number} cy Y-axis coordinate of the center of the ellipse
200
- * @param {number} rx X-radius of the ellipse
201
- * @param {number} ry Y-radius of the ellipse
202
- * @param {number} [rotation=0] Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis
203
- * @param {number} [startAngle=0] Start angle of the arc (in radians)
204
- * @param {number} [endAngle=2*PI] End angle of the arc (in radians)
205
- * @param {boolean} [counterclockwise=false] Flag indicating the direction of the arc
206
- * @returns {Point} Interpolated coordinates on the arc
207
- */
208
- export function ellipse(t, cx, cy, rx, ry, rotation = 0, startAngle = 0, endAngle = TWO_PI, counterclockwise = false) {
209
- let deltaAngle = endAngle - startAngle;
210
- const isEmpty = Math.abs(deltaAngle) <= EPSILON;
211
- while (deltaAngle < 0)
212
- deltaAngle += TWO_PI;
213
- while (deltaAngle > TWO_PI)
214
- deltaAngle -= TWO_PI;
215
- if (deltaAngle <= EPSILON) {
216
- if (isEmpty) {
217
- deltaAngle = 0;
218
- }
219
- else {
220
- deltaAngle = TWO_PI;
221
- }
222
- }
223
- if (counterclockwise === true && !isEmpty) {
224
- if (deltaAngle === TWO_PI) {
225
- deltaAngle = -TWO_PI;
226
- }
227
- else {
228
- deltaAngle = deltaAngle - TWO_PI;
229
- }
230
- }
231
- const angle = startAngle + t * deltaAngle;
232
- let x = cx + rx * Math.cos(angle);
233
- let y = cy + ry * Math.sin(angle);
234
- if (rotation !== 0) {
235
- const cos = Math.cos(rotation);
236
- const sin = Math.sin(rotation);
237
- const deltaX = x - cx;
238
- const deltaY = y - cy;
239
- x = deltaX * cos - deltaY * sin + cx;
240
- y = deltaX * sin + deltaY * cos + cy;
241
- }
242
- return [x, y];
243
- }
244
- /**
245
- * Interpolate a point on a circular arc
246
- *
247
- * @param {number} t Normalized time value to interpolate
248
- * @param {number} cx X-axis coordinate of the center of the circle
249
- * @param {number} cy Y-axis coordinate of the center of the circle
250
- * @param {number} radius Radius of the circle
251
- * @param {number} [startAngle] Start angle of the arc (in radians)
252
- * @param {number} [endAngle] End angle of the arc (in radians)
253
- * @param {boolean} [counterclockwise=false] Flag indicating the direction of the arc
254
- * @returns {Point} Interpolated coordinates on the arc
255
- */
256
- export function arc(t, cx, cy, radius, startAngle, endAngle, counterclockwise) {
257
- return ellipse(t, cx, cy, radius, radius, 0, startAngle, endAngle, counterclockwise);
258
- }
259
- /**
260
- * Check if two points are coincident
261
- *
262
- * @param {number} x1 X-axis coordinate of the first point
263
- * @param {number} y1 Y-axis coordinate of the first point
264
- * @param {number} x2 X-axis coordinate of the second point
265
- * @param {number} y2 Y-axis coordinate of the second point
266
- * @returns {boolean} True if the two are coincident, false otherwise
267
- */
268
- export function isCoincident(x1, y1, x2, y2) {
269
- return distance(x1, y1, x2, y2) <= EPSILON;
270
- }
271
- /**
272
- * Check if three points are collinear (aligned on the same line)
273
- *
274
- * @param {number} x1 X-axis coordinate of the first point
275
- * @param {number} y1 Y-axis coordinate of the first point
276
- * @param {number} x2 X-axis coordinate of the second point
277
- * @param {number} y2 Y-axis coordinate of the second point
278
- * @param {number} x3 X-axis coordinate of the third point
279
- * @param {number} y3 Y-axis coordinate of the third point
280
- * @returns {boolean} True if the three points are collinear, false otherwise
281
- */
282
- export function isCollinear(x1, y1, x2, y2, x3, y3) {
283
- return Math.abs((x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2)) <= EPSILON;
284
- }
285
67
  /**
286
68
  * Make a target fit a container
287
69
  *
package/lib/maths.d.ts CHANGED
@@ -44,7 +44,15 @@ export declare function sign(value: number): number;
44
44
  */
45
45
  export declare function clamp(value: number, min?: number, max?: number): number;
46
46
  /**
47
- * Linear interpolation between two values (lerping)
47
+ * Round a number up to a nearest multiple
48
+ *
49
+ * @param {number} value Value to round
50
+ * @param {number} [multiple=1] Multiple to round to
51
+ * @returns {number} Closest multiple
52
+ */
53
+ export declare function snap(value: number, multiple?: number): number;
54
+ /**
55
+ * Interpolate a value between two values using Linear interpolation (lerping)
48
56
  *
49
57
  * @param {number} t Normalized time value to interpolate
50
58
  * @param {number} min Minimum value
@@ -53,7 +61,27 @@ export declare function clamp(value: number, min?: number, max?: number): number
53
61
  */
54
62
  export declare function lerp(t: number, min: number, max: number): number;
55
63
  /**
56
- * Triangular interpolation between two values
64
+ * Normalize a value between two bounds
65
+ *
66
+ * @param {number} value Value to normalize
67
+ * @param {number} min Minimum boundary
68
+ * @param {number} max Maximum boundary
69
+ * @returns {number} Normalized value
70
+ */
71
+ export declare function normalize(value: number, min: number, max: number): number;
72
+ /**
73
+ * Re-map a number from one range to another
74
+ *
75
+ * @param {number} value Value to re-map
76
+ * @param {number} currentMin Lower bound of the value's current range
77
+ * @param {number} currentMax Upper bound of the value's current range
78
+ * @param {number} targetMin Lower bound of the value's target range
79
+ * @param {number} targetMax Upper bound of the value's target range
80
+ * @returns {number} Re-mapped value
81
+ */
82
+ export declare function map(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number;
83
+ /**
84
+ * Interpolate a value between two values using Triangular interpolation
57
85
  *
58
86
  * @param {number} t Normalized time value to interpolate
59
87
  * @param {number} min Minimum value
@@ -63,7 +91,7 @@ export declare function lerp(t: number, min: number, max: number): number;
63
91
  */
64
92
  export declare function triLerp(t: number, min: number, max: number, target: number): number;
65
93
  /**
66
- * Exponential interpolation between two values
94
+ * Interpolate a value using Exponential interpolation
67
95
  *
68
96
  * @param {number} t Normalized time value to interpolate
69
97
  * @param {number} currentMin Lower bound of the value's current range
@@ -74,33 +102,37 @@ export declare function triLerp(t: number, min: number, max: number, target: num
74
102
  */
75
103
  export declare function expLerp(t: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number;
76
104
  /**
77
- * Normalize a value between two bounds
105
+ * Interpolate a value using Quadratic Bézier interpolation
78
106
  *
79
- * @param {number} value Value to normalize
80
- * @param {number} min Minimum boundary
81
- * @param {number} max Maximum boundary
82
- * @returns {number} Normalized value
107
+ * @param {number} t Normalized time value to interpolate
108
+ * @param {number} p1 Start point
109
+ * @param {number} cp Control point
110
+ * @param {number} p2 End point
111
+ * @returns {number} Interpolated value
83
112
  */
84
- export declare function normalize(value: number, min: number, max: number): number;
113
+ export declare function quadraticBezier(t: number, p1: number, cp: number, p2: number): number;
85
114
  /**
86
- * Re-map a number from one range to another
115
+ * Interpolate a value using Cubic Bézier interpolation
87
116
  *
88
- * @param {number} value Value to re-map
89
- * @param {number} currentMin Lower bound of the value's current range
90
- * @param {number} currentMax Upper bound of the value's current range
91
- * @param {number} targetMin Lower bound of the value's target range
92
- * @param {number} targetMax Upper bound of the value's target range
93
- * @returns {number} Re-mapped value
117
+ * @param {number} t Normalized time value to interpolate
118
+ * @param {number} p1 Start point
119
+ * @param {number} cp1 First control point
120
+ * @param {number} cp2 Second control point
121
+ * @param {number} p2 End point
122
+ * @returns {number} Interpolated value
94
123
  */
95
- export declare function map(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number;
124
+ export declare function cubicBezier(t: number, p1: number, cp1: number, cp2: number, p2: number): number;
96
125
  /**
97
- * Round a number up to a nearest multiple
126
+ * Interpolate a value using Catmull-Rom interpolation
98
127
  *
99
- * @param {number} value Value to round
100
- * @param {number} [multiple=1] Multiple to round to
101
- * @returns {number} Closest multiple
128
+ * @param {number} t Normalized time value to interpolate
129
+ * @param {number} p1 Start point
130
+ * @param {number} cp1 First control point
131
+ * @param {number} cp2 Second control point
132
+ * @param {number} p2 End point
133
+ * @returns {number} Interpolated value
102
134
  */
103
- export declare function snap(value: number, multiple?: number): number;
135
+ export declare function catmullRom(t: number, p1: number, cp1: number, cp2: number, p2: number): number;
104
136
  /**
105
137
  * Modulo absolute a value based on a length
106
138
  *
package/lib/maths.js CHANGED
@@ -60,7 +60,19 @@ export function clamp(value, min = 0, max = 1) {
60
60
  return Math.min(max, Math.max(min, value));
61
61
  }
62
62
  /**
63
- * Linear interpolation between two values (lerping)
63
+ * Round a number up to a nearest multiple
64
+ *
65
+ * @param {number} value Value to round
66
+ * @param {number} [multiple=1] Multiple to round to
67
+ * @returns {number} Closest multiple
68
+ */
69
+ export function snap(value, multiple = 1) {
70
+ if (multiple === 0)
71
+ return value;
72
+ return Math.round(value / multiple) * multiple;
73
+ }
74
+ /**
75
+ * Interpolate a value between two values using Linear interpolation (lerping)
64
76
  *
65
77
  * @param {number} t Normalized time value to interpolate
66
78
  * @param {number} min Minimum value
@@ -71,7 +83,31 @@ export function lerp(t, min, max) {
71
83
  return min + (max - min) * t;
72
84
  }
73
85
  /**
74
- * Triangular interpolation between two values
86
+ * Normalize a value between two bounds
87
+ *
88
+ * @param {number} value Value to normalize
89
+ * @param {number} min Minimum boundary
90
+ * @param {number} max Maximum boundary
91
+ * @returns {number} Normalized value
92
+ */
93
+ export function normalize(value, min, max) {
94
+ return (value - min) / (max - min);
95
+ }
96
+ /**
97
+ * Re-map a number from one range to another
98
+ *
99
+ * @param {number} value Value to re-map
100
+ * @param {number} currentMin Lower bound of the value's current range
101
+ * @param {number} currentMax Upper bound of the value's current range
102
+ * @param {number} targetMin Lower bound of the value's target range
103
+ * @param {number} targetMax Upper bound of the value's target range
104
+ * @returns {number} Re-mapped value
105
+ */
106
+ export function map(value, currentMin, currentMax, targetMin, targetMax) {
107
+ return ((value - currentMin) / (currentMax - currentMin)) * (targetMax - targetMin) + targetMin;
108
+ }
109
+ /**
110
+ * Interpolate a value between two values using Triangular interpolation
75
111
  *
76
112
  * @param {number} t Normalized time value to interpolate
77
113
  * @param {number} min Minimum value
@@ -84,7 +120,7 @@ export function triLerp(t, min, max, target) {
84
120
  return t <= x ? min - (min - target) * (t / x) : target - (target - max) * ((t - x) / (1 - x));
85
121
  }
86
122
  /**
87
- * Exponential interpolation between two values
123
+ * Interpolate a value using Exponential interpolation
88
124
  *
89
125
  * @param {number} t Normalized time value to interpolate
90
126
  * @param {number} currentMin Lower bound of the value's current range
@@ -98,40 +134,54 @@ export function expLerp(t, currentMin, currentMax, targetMin, targetMax) {
98
134
  Math.pow(targetMax / targetMin, (clamp(t, currentMin, currentMax) - currentMin) / (currentMax - currentMin)));
99
135
  }
100
136
  /**
101
- * Normalize a value between two bounds
137
+ * Interpolate a value using Quadratic Bézier interpolation
102
138
  *
103
- * @param {number} value Value to normalize
104
- * @param {number} min Minimum boundary
105
- * @param {number} max Maximum boundary
106
- * @returns {number} Normalized value
139
+ * @param {number} t Normalized time value to interpolate
140
+ * @param {number} p1 Start point
141
+ * @param {number} cp Control point
142
+ * @param {number} p2 End point
143
+ * @returns {number} Interpolated value
107
144
  */
108
- export function normalize(value, min, max) {
109
- return (value - min) / (max - min);
145
+ export function quadraticBezier(t, p1, cp, p2) {
146
+ const t2 = t * t;
147
+ const k = 1 - t;
148
+ const k2 = k * k;
149
+ return k2 * p1 + 2 * k * t * cp + t2 * p2;
110
150
  }
111
151
  /**
112
- * Re-map a number from one range to another
152
+ * Interpolate a value using Cubic Bézier interpolation
113
153
  *
114
- * @param {number} value Value to re-map
115
- * @param {number} currentMin Lower bound of the value's current range
116
- * @param {number} currentMax Upper bound of the value's current range
117
- * @param {number} targetMin Lower bound of the value's target range
118
- * @param {number} targetMax Upper bound of the value's target range
119
- * @returns {number} Re-mapped value
154
+ * @param {number} t Normalized time value to interpolate
155
+ * @param {number} p1 Start point
156
+ * @param {number} cp1 First control point
157
+ * @param {number} cp2 Second control point
158
+ * @param {number} p2 End point
159
+ * @returns {number} Interpolated value
120
160
  */
121
- export function map(value, currentMin, currentMax, targetMin, targetMax) {
122
- return ((value - currentMin) / (currentMax - currentMin)) * (targetMax - targetMin) + targetMin;
161
+ export function cubicBezier(t, p1, cp1, cp2, p2) {
162
+ const t2 = t * t;
163
+ const t3 = t2 * t;
164
+ const k = 1 - t;
165
+ const k2 = k * k;
166
+ const k3 = k2 * k;
167
+ return k3 * p1 + 3 * k2 * t * cp1 + 3 * k * t2 * cp2 + t3 * p2;
123
168
  }
124
169
  /**
125
- * Round a number up to a nearest multiple
170
+ * Interpolate a value using Catmull-Rom interpolation
126
171
  *
127
- * @param {number} value Value to round
128
- * @param {number} [multiple=1] Multiple to round to
129
- * @returns {number} Closest multiple
172
+ * @param {number} t Normalized time value to interpolate
173
+ * @param {number} p1 Start point
174
+ * @param {number} cp1 First control point
175
+ * @param {number} cp2 Second control point
176
+ * @param {number} p2 End point
177
+ * @returns {number} Interpolated value
130
178
  */
131
- export function snap(value, multiple = 1) {
132
- if (multiple === 0)
133
- return value;
134
- return Math.round(value / multiple) * multiple;
179
+ export function catmullRom(t, p1, cp1, cp2, p2) {
180
+ const t2 = t * t;
181
+ const t3 = t2 * t;
182
+ const v1 = (cp2 - p1) * 0.5;
183
+ const v2 = (p2 - cp1) * 0.5;
184
+ return (2 * cp1 - 2 * cp2 + v1 + v2) * t3 + (-3 * cp1 + 3 * cp2 - 2 * v1 - v2) * t2 + v1 * t + cp1;
135
185
  }
136
186
  /**
137
187
  * Modulo absolute a value based on a length
package/lib/random.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Point, Point3 } from './types';
1
+ import type { Point2, Point3 } from './types';
2
2
  /**
3
3
  * Generate a random boolean (true or false)
4
4
  *
@@ -66,34 +66,30 @@ export declare function randomIndex(weights: number[]): number;
66
66
  */
67
67
  export declare function randomGaussian(mean?: number, spread?: number): number;
68
68
  /**
69
- * Produce a random 2D point around the perimiter of a unit circle
69
+ * Produce a random 2D point around the perimiter of a circle
70
70
  *
71
71
  * @param {number} [radius=1] Radius of the circle
72
- * @param {Point} [target] Target point
73
- * @returns {Point} Random 2D point on circle
72
+ * @returns {Point} Random 2D point on the circle
74
73
  */
75
- export declare function onCircle(radius?: number, target?: Point): Point;
74
+ export declare function onCircle(radius?: number): Point2;
76
75
  /**
77
- * Produce a random 2D point inside a unit circle
76
+ * Produce a random 2D point inside a circle
78
77
  *
79
78
  * @param {number} [radius=1] Radius of the circle
80
- * @param {Point} [target] Target vector
81
- * @returns {Point} Random 2D point inside circle
79
+ * @returns {Point} Random 2D point inside the circle
82
80
  */
83
- export declare function insideCircle(radius?: number, target?: Point): Point;
81
+ export declare function insideCircle(radius?: number): Point2;
84
82
  /**
85
- * Produce a random 3D point on the surface of a unit sphere
83
+ * Produce a random 3D point on the surface of a sphere
86
84
  *
87
85
  * @param {number} [radius=1] Radius of the sphere
88
- * @param {Point3} [target] Target vector
89
- * @returns {Point3} Random 3D point on sphere
86
+ * @returns {Point3} Random 3D point on the sphere
90
87
  */
91
- export declare function onSphere(radius?: number, target?: Point3): Point3;
88
+ export declare function onSphere(radius?: number): Point3;
92
89
  /**
93
90
  * Produce a random 3D point inside a unit sphere
94
91
  *
95
92
  * @param {number} [radius=1] Radius of the sphere
96
- * @param {Point3} [target] Target vector
97
- * @returns {Point3} Random 3D point inside sphere
93
+ * @returns {Point3} Random 3D point inside the sphere
98
94
  */
99
- export declare function insideSphere(radius?: number, target?: Point3): Point3;
95
+ export declare function insideSphere(radius?: number): Point3;
package/lib/random.js CHANGED
@@ -1,4 +1,4 @@
1
- import { radToSphere } from './geometry';
1
+ import { Vector2, Vector3 } from './extras/geometry';
2
2
  /**
3
3
  * Generate a random boolean (true or false)
4
4
  *
@@ -112,51 +112,43 @@ export function randomGaussian(mean = 0, spread = 1) {
112
112
  // Geometry
113
113
  // *********************
114
114
  /**
115
- * Produce a random 2D point around the perimiter of a unit circle
115
+ * Produce a random 2D point around the perimiter of a circle
116
116
  *
117
117
  * @param {number} [radius=1] Radius of the circle
118
- * @param {Point} [target] Target point
119
- * @returns {Point} Random 2D point on circle
118
+ * @returns {Point} Random 2D point on the circle
120
119
  */
121
- export function onCircle(radius = 1, target = [0, 0]) {
122
- const angle = Math.random() * 2.0 * Math.PI;
123
- target[0] = radius * Math.cos(angle);
124
- target[1] = radius * Math.sin(angle);
125
- return target;
120
+ export function onCircle(radius = 1) {
121
+ const angle = randomFloat(0, Math.PI * 2);
122
+ return Vector2.fromCircularCoords(angle, radius);
126
123
  }
127
124
  /**
128
- * Produce a random 2D point inside a unit circle
125
+ * Produce a random 2D point inside a circle
129
126
  *
130
127
  * @param {number} [radius=1] Radius of the circle
131
- * @param {Point} [target] Target vector
132
- * @returns {Point} Random 2D point inside circle
128
+ * @returns {Point} Random 2D point inside the circle
133
129
  */
134
- export function insideCircle(radius = 1, target = [0, 0]) {
130
+ export function insideCircle(radius = 1) {
135
131
  radius *= Math.random();
136
- return onCircle(radius, target);
132
+ return onCircle(radius);
137
133
  }
138
134
  /**
139
- * Produce a random 3D point on the surface of a unit sphere
135
+ * Produce a random 3D point on the surface of a sphere
140
136
  *
141
137
  * @param {number} [radius=1] Radius of the sphere
142
- * @param {Point3} [target] Target vector
143
- * @returns {Point3} Random 3D point on sphere
138
+ * @returns {Point3} Random 3D point on the sphere
144
139
  */
145
- export function onSphere(radius = 1, target = [0, 0, 0]) {
146
- const u = Math.random() * Math.PI * 2;
147
- const v = Math.random() * 2 - 1;
148
- const phi = u;
149
- const theta = Math.acos(v);
150
- return radToSphere(radius, phi, theta, target);
140
+ export function onSphere(radius = 1) {
141
+ const phi = randomFloat(0, Math.PI);
142
+ const theta = randomFloat(0, 2 * Math.PI);
143
+ return Vector3.fromSphericalCoords(phi, theta, radius);
151
144
  }
152
145
  /**
153
146
  * Produce a random 3D point inside a unit sphere
154
147
  *
155
148
  * @param {number} [radius=1] Radius of the sphere
156
- * @param {Point3} [target] Target vector
157
- * @returns {Point3} Random 3D point inside sphere
149
+ * @returns {Point3} Random 3D point inside the sphere
158
150
  */
159
- export function insideSphere(radius = 1, target = [0, 0, 0]) {
151
+ export function insideSphere(radius = 1) {
160
152
  radius *= Math.random();
161
- return onSphere(radius, target);
153
+ return onSphere(radius);
162
154
  }