toosoon-utils 4.0.6 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +716 -88
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +5 -3
- package/lib/extras/_wip/pool.d.ts +56 -0
- package/lib/extras/_wip/pool.js +67 -0
- package/lib/{classes/color-scale.d.ts → extras/color-scale/ColorScale.d.ts} +1 -1
- package/lib/{classes/color-scale.js → extras/color-scale/ColorScale.js} +2 -2
- package/lib/extras/curves/ArcCurve.d.ts +19 -0
- package/lib/extras/curves/ArcCurve.js +21 -0
- package/lib/extras/curves/CatmulRomCurve.d.ts +62 -0
- package/lib/extras/curves/CatmulRomCurve.js +75 -0
- package/lib/extras/curves/CubicBezierCurve.d.ts +62 -0
- package/lib/extras/curves/CubicBezierCurve.js +75 -0
- package/lib/extras/curves/Curve.d.ts +95 -0
- package/lib/extras/curves/Curve.js +174 -0
- package/lib/extras/curves/EllipseCurve.d.ts +63 -0
- package/lib/extras/curves/EllipseCurve.js +76 -0
- package/lib/extras/curves/LineCurve.d.ts +51 -0
- package/lib/extras/curves/LineCurve.js +71 -0
- package/lib/extras/curves/Path.d.ts +164 -0
- package/lib/extras/curves/Path.js +367 -0
- package/lib/extras/curves/PathCurve.d.ts +69 -0
- package/lib/extras/curves/PathCurve.js +148 -0
- package/lib/extras/curves/PathSVG.d.ts +41 -0
- package/lib/extras/curves/PathSVG.js +135 -0
- package/lib/extras/curves/PolylineCurve.d.ts +27 -0
- package/lib/extras/curves/PolylineCurve.js +39 -0
- package/lib/extras/curves/QuadraticBezierCurve.d.ts +52 -0
- package/lib/extras/curves/QuadraticBezierCurve.js +63 -0
- package/lib/extras/curves/SplineCurve.d.ts +24 -0
- package/lib/extras/curves/SplineCurve.js +38 -0
- package/lib/extras/curves/index.d.ts +12 -0
- package/lib/extras/curves/index.js +12 -0
- package/lib/{classes/frame-rate.js → extras/frame-rate/FrameRate.js} +1 -1
- package/lib/geometry.d.ts +87 -68
- package/lib/geometry.js +144 -140
- package/lib/prng.js +2 -1
- package/lib/random.d.ts +13 -13
- package/lib/random.js +14 -14
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +3 -10
- package/package.json +4 -3
- /package/lib/{classes/_pool.d.ts → extras/_/pool.d.ts} +0 -0
- /package/lib/{classes/_pool.js → extras/_/pool.js} +0 -0
- /package/lib/{classes/frame-rate.d.ts → extras/frame-rate/FrameRate.d.ts} +0 -0
package/lib/geometry.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { PI } from './constants';
|
|
1
|
+
import { EPSILON, PI, TWO_PI } from './constants';
|
|
2
|
+
import { lerp } from './maths';
|
|
2
3
|
/**
|
|
3
4
|
* Convert a radians value into degrees
|
|
4
5
|
*
|
|
@@ -32,8 +33,8 @@ export function angle(x1, y1, x2, y2) {
|
|
|
32
33
|
/**
|
|
33
34
|
* Find the closest angle between to angles
|
|
34
35
|
*
|
|
35
|
-
* @param {number} source Source angle in radians
|
|
36
|
-
* @param {number} target Target angle in radians
|
|
36
|
+
* @param {number} source Source angle (in radians)
|
|
37
|
+
* @param {number} target Target angle (in radians)
|
|
37
38
|
* @returns {number} Closest angle
|
|
38
39
|
*/
|
|
39
40
|
export function closestAngle(source, target) {
|
|
@@ -70,63 +71,35 @@ export function diagonal(width, height) {
|
|
|
70
71
|
* @param {number} radius Radius of the sphere
|
|
71
72
|
* @param {number} phi Polar angle from the y (up) axis : [0, PI]
|
|
72
73
|
* @param {number} theta Equator angle around the y (up) axis : [0, 2*PI]
|
|
73
|
-
* @param {
|
|
74
|
-
* @returns {
|
|
74
|
+
* @param {Point3} target Target 3D point
|
|
75
|
+
* @returns {Point3}
|
|
75
76
|
*/
|
|
76
|
-
export function radToSphere(radius, phi, theta, target =
|
|
77
|
-
target
|
|
78
|
-
target
|
|
79
|
-
target
|
|
77
|
+
export function radToSphere(radius, phi, theta, target = [0, 0, 0]) {
|
|
78
|
+
target[0] = radius * Math.sin(phi) * Math.sin(theta);
|
|
79
|
+
target[1] = radius * Math.cos(phi);
|
|
80
|
+
target[2] = radius * Math.sin(phi) * Math.cos(theta);
|
|
80
81
|
return target;
|
|
81
82
|
}
|
|
82
83
|
// *********************
|
|
83
84
|
// Curves
|
|
84
85
|
// *********************
|
|
85
86
|
/**
|
|
86
|
-
* Interpolate a point on
|
|
87
|
+
* Interpolate a point on a line
|
|
87
88
|
*
|
|
88
|
-
* @param {number} t
|
|
89
|
-
* @param {number} x1
|
|
90
|
-
* @param {number} y1
|
|
91
|
-
* @param {number}
|
|
92
|
-
* @param {number}
|
|
93
|
-
* @
|
|
94
|
-
* @param {number} cpy2 Y-axis coordinate of the second control point
|
|
95
|
-
* @param {number} x2 X-axis coordinate of the end point
|
|
96
|
-
* @param {number} y2 Y-axis coordinate of the end point
|
|
97
|
-
* @returns {[number, number]} Interpolated coordinates on the curve
|
|
89
|
+
* @param {number} t Normalized time value to interpolate
|
|
90
|
+
* @param {number} x1 X-axis coordinate of the start point
|
|
91
|
+
* @param {number} y1 Y-axis coordinate of the start point
|
|
92
|
+
* @param {number} x2 X-axis coordinate of the end point
|
|
93
|
+
* @param {number} y2 Y-axis coordinate of the end point
|
|
94
|
+
* @returns {Point} Interpolated coordinates on the line
|
|
98
95
|
*/
|
|
99
|
-
export function
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const T = 1 - t;
|
|
103
|
-
const T2 = T * T;
|
|
104
|
-
const T3 = T2 * T;
|
|
105
|
-
const x = T3 * x1 + 3 * T2 * t * cpx1 + 3 * T * t2 * cpx2 + t3 * x2;
|
|
106
|
-
const y = T3 * y1 + 3 * T2 * t * cpy1 + 3 * T * t2 * cpy2 + t3 * y2;
|
|
96
|
+
export function line(t, x1, y1, x2, y2) {
|
|
97
|
+
const x = lerp(t, x1, x2);
|
|
98
|
+
const y = lerp(t, y1, y2);
|
|
107
99
|
return [x, y];
|
|
108
100
|
}
|
|
109
101
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* @param {number} x1 X-axis coordinate of the start point
|
|
113
|
-
* @param {number} y1 Y-axis coordinate of the start point
|
|
114
|
-
* @param {number} cpx1 X-axis coordinate of the first control point
|
|
115
|
-
* @param {number} cpy1 Y-axis coordinate of the first control point
|
|
116
|
-
* @param {number} cpx2 X-axis coordinate of the second control point
|
|
117
|
-
* @param {number} cpy2 Y-axis coordinate of the second control point
|
|
118
|
-
* @param {number} x2 X-axis coordinate of the end point
|
|
119
|
-
* @param {number} y2 Y-axis coordinate of the end point
|
|
120
|
-
* @returns {number} Computed curvature
|
|
121
|
-
*/
|
|
122
|
-
export function computeCubicBezierCurvature(x1, y1, cpx1, cpy1, cpx2, cpy2, x2, y2) {
|
|
123
|
-
const d1 = Math.hypot(cpx1 - x1, cpy1 - y1);
|
|
124
|
-
const d2 = Math.hypot(cpx2 - x2, cpy2 - y2);
|
|
125
|
-
const d3 = Math.hypot(x2 - x1, y2 - y1);
|
|
126
|
-
return (d1 + d2) / d3;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Interpolate a point on an elliptical arc Quadratic Bézier curve
|
|
102
|
+
* Interpolate a point on a Quadratic Bézier curve
|
|
130
103
|
*
|
|
131
104
|
* @param {number} t Normalized time value to interpolate
|
|
132
105
|
* @param {number} x1 X-axis coordinate of the start point
|
|
@@ -135,124 +108,155 @@ export function computeCubicBezierCurvature(x1, y1, cpx1, cpy1, cpx2, cpy2, x2,
|
|
|
135
108
|
* @param {number} cpy Y-axis coordinate of the control point
|
|
136
109
|
* @param {number} x2 X-axis coordinate of the end point
|
|
137
110
|
* @param {number} y2 Y-axis coordinate of the end point
|
|
138
|
-
* @returns {
|
|
111
|
+
* @returns {Point} Interpolated coordinates on the curve
|
|
139
112
|
*/
|
|
140
113
|
export function quadraticBezier(t, x1, y1, cpx, cpy, x2, y2) {
|
|
141
114
|
const t2 = t * t;
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
const x =
|
|
145
|
-
const y =
|
|
115
|
+
const k = 1 - t;
|
|
116
|
+
const k2 = k * k;
|
|
117
|
+
const x = k2 * x1 + 2 * k * t * cpx + t2 * x2;
|
|
118
|
+
const y = k2 * y1 + 2 * k * t * cpy + t2 * y2;
|
|
119
|
+
return [x, y];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Interpolate a point on a Cubic Bézier curve
|
|
123
|
+
*
|
|
124
|
+
* @param {number} t Normalized time value to interpolate
|
|
125
|
+
* @param {number} x1 X-axis coordinate of the start point
|
|
126
|
+
* @param {number} y1 Y-axis coordinate of the start point
|
|
127
|
+
* @param {number} cp1x X-axis coordinate of the first control point
|
|
128
|
+
* @param {number} cp1y Y-axis coordinate of the first control point
|
|
129
|
+
* @param {number} cp2x X-axis coordinate of the second control point
|
|
130
|
+
* @param {number} cp2y Y-axis coordinate of the second control point
|
|
131
|
+
* @param {number} x2 X-axis coordinate of the end point
|
|
132
|
+
* @param {number} y2 Y-axis coordinate of the end point
|
|
133
|
+
* @returns {Point} Interpolated coordinates on the curve
|
|
134
|
+
*/
|
|
135
|
+
export function cubicBezier(t, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2) {
|
|
136
|
+
const t2 = t * t;
|
|
137
|
+
const t3 = t2 * t;
|
|
138
|
+
const k = 1 - t;
|
|
139
|
+
const k2 = k * k;
|
|
140
|
+
const k3 = k2 * k;
|
|
141
|
+
const x = k3 * x1 + 3 * k2 * t * cp1x + 3 * k * t2 * cp2x + t3 * x2;
|
|
142
|
+
const y = k3 * y1 + 3 * k2 * t * cp1y + 3 * k * t2 * cp2y + t3 * y2;
|
|
146
143
|
return [x, y];
|
|
147
144
|
}
|
|
148
145
|
/**
|
|
149
|
-
*
|
|
146
|
+
* Interpolate a point on a Catmull-Rom spline
|
|
150
147
|
*
|
|
148
|
+
* @param {number} t Normalized time value to interpolate
|
|
151
149
|
* @param {number} x1 X-axis coordinate of the start point
|
|
152
150
|
* @param {number} y1 Y-axis coordinate of the start point
|
|
153
|
-
* @param {number}
|
|
154
|
-
* @param {number}
|
|
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
155
|
* @param {number} x2 X-axis coordinate of the end point
|
|
156
156
|
* @param {number} y2 Y-axis coordinate of the end point
|
|
157
|
-
* @returns {
|
|
157
|
+
* @returns {Point} Interpolated coordinates on the spline
|
|
158
158
|
*/
|
|
159
|
-
export function
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
|
|
159
|
+
export function catmullRom(t, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2) {
|
|
160
|
+
const t2 = t * t;
|
|
161
|
+
const t3 = t2 * t;
|
|
162
|
+
const vx1 = (cp2x - x1) * 0.5;
|
|
163
|
+
const vy1 = (cp2y - y1) * 0.5;
|
|
164
|
+
const vx2 = (x2 - cp1x) * 0.5;
|
|
165
|
+
const vy2 = (y2 - cp1y) * 0.5;
|
|
166
|
+
const x = (2 * cp1x - 2 * cp2x + vx1 + vx2) * t3 + (-3 * cp1x + 3 * cp2x - 2 * vx1 - vx2) * t2 + vx1 * t + cp1x;
|
|
167
|
+
const y = (2 * cp1y - 2 * cp2y + vy1 + vy2) * t3 + (-3 * cp1y + 3 * cp2y - 2 * vy1 - vy2) * t2 + vy1 * t + cp1y;
|
|
168
|
+
return [x, y];
|
|
164
169
|
}
|
|
165
170
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @param {number}
|
|
169
|
-
* @param {number}
|
|
170
|
-
* @param {number}
|
|
171
|
-
* @param {number}
|
|
172
|
-
* @param {number}
|
|
173
|
-
* @param {
|
|
174
|
-
* @param {
|
|
175
|
-
* @
|
|
171
|
+
* Interpolate a point on an elliptical arc
|
|
172
|
+
*
|
|
173
|
+
* @param {number} t Normalized time value to interpolate
|
|
174
|
+
* @param {number} cx X-axis coordinate of the center of the ellipse
|
|
175
|
+
* @param {number} cy Y-axis coordinate of the center of the ellipse
|
|
176
|
+
* @param {number} rx X-radius of the ellipse
|
|
177
|
+
* @param {number} ry Y-radius of the ellipse
|
|
178
|
+
* @param {number} [rotation=0] Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis
|
|
179
|
+
* @param {number} [startAngle=0] Start angle of the arc (in radians)
|
|
180
|
+
* @param {number} [endAngle=2*PI] End angle of the arc (in radians)
|
|
181
|
+
* @param {boolean} [counterclockwise=false] Flag indicating the direction of the arc
|
|
182
|
+
* @returns {Point} Interpolated coordinates on the arc
|
|
176
183
|
*/
|
|
177
|
-
function
|
|
178
|
-
const centerX = (x1 - x2) / 2;
|
|
179
|
-
const centerY = (y1 - y2) / 2;
|
|
180
|
-
const cosAngle = Math.cos(angle);
|
|
181
|
-
const sinAngle = Math.sin(angle);
|
|
182
|
-
const x1p = cosAngle * centerX + sinAngle * centerY;
|
|
183
|
-
const y1p = -sinAngle * centerX + cosAngle * centerY;
|
|
184
|
-
rx = Math.abs(rx);
|
|
185
|
-
ry = Math.abs(ry);
|
|
186
|
-
const lambda = x1p ** 2 / rx ** 2 + y1p ** 2 / ry ** 2;
|
|
187
|
-
if (lambda > 1) {
|
|
188
|
-
rx *= Math.sqrt(lambda);
|
|
189
|
-
ry *= Math.sqrt(lambda);
|
|
190
|
-
}
|
|
191
|
-
const sign = largeArc !== clockwise ? 1 : -1;
|
|
192
|
-
const factor = sign *
|
|
193
|
-
Math.sqrt(Math.max(0, (rx ** 2 * ry ** 2 - rx ** 2 * y1p ** 2 - ry ** 2 * x1p ** 2) / (rx ** 2 * y1p ** 2 + ry ** 2 * x1p ** 2)));
|
|
194
|
-
const cxp = (factor * rx * y1p) / ry;
|
|
195
|
-
const cyp = (-factor * ry * x1p) / rx;
|
|
196
|
-
const cx = cosAngle * cxp - sinAngle * cyp + (x1 + x2) / 2;
|
|
197
|
-
const cy = sinAngle * cxp + cosAngle * cyp + (y1 + y2) / 2;
|
|
198
|
-
const startAngle = Math.atan2((y1p - cyp) / ry, (x1p - cxp) / rx);
|
|
199
|
-
const endAngle = Math.atan2((-y1p - cyp) / ry, (-x1p - cxp) / rx);
|
|
184
|
+
export function ellipse(t, cx, cy, rx, ry, rotation = 0, startAngle = 0, endAngle = TWO_PI, counterclockwise = false) {
|
|
200
185
|
let deltaAngle = endAngle - startAngle;
|
|
201
|
-
|
|
202
|
-
|
|
186
|
+
const isEmpty = Math.abs(deltaAngle) <= EPSILON;
|
|
187
|
+
while (deltaAngle < 0)
|
|
188
|
+
deltaAngle += TWO_PI;
|
|
189
|
+
while (deltaAngle > TWO_PI)
|
|
190
|
+
deltaAngle -= TWO_PI;
|
|
191
|
+
if (deltaAngle <= EPSILON) {
|
|
192
|
+
if (isEmpty) {
|
|
193
|
+
deltaAngle = 0;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
deltaAngle = TWO_PI;
|
|
197
|
+
}
|
|
203
198
|
}
|
|
204
|
-
|
|
205
|
-
deltaAngle
|
|
199
|
+
if (counterclockwise === false && !isEmpty) {
|
|
200
|
+
if (deltaAngle === TWO_PI) {
|
|
201
|
+
deltaAngle = -TWO_PI;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
deltaAngle = deltaAngle - TWO_PI;
|
|
205
|
+
}
|
|
206
206
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
207
|
+
const angle = startAngle + t * deltaAngle;
|
|
208
|
+
let x = cx + rx * Math.cos(angle);
|
|
209
|
+
let y = cy + ry * Math.sin(angle);
|
|
210
|
+
if (rotation !== 0) {
|
|
211
|
+
const cos = Math.cos(rotation);
|
|
212
|
+
const sin = Math.sin(rotation);
|
|
213
|
+
const deltaX = x - cx;
|
|
214
|
+
const deltaY = y - cy;
|
|
215
|
+
x = deltaX * cos - deltaY * sin + cx;
|
|
216
|
+
y = deltaX * sin + deltaY * cos + cy;
|
|
217
|
+
}
|
|
218
|
+
return [x, y];
|
|
215
219
|
}
|
|
216
220
|
/**
|
|
217
|
-
* Interpolate a point on
|
|
221
|
+
* Interpolate a point on a circular arc
|
|
218
222
|
*
|
|
219
|
-
* @param {number} t
|
|
220
|
-
* @param {number}
|
|
221
|
-
* @param {number}
|
|
222
|
-
* @param {number}
|
|
223
|
-
* @param {number}
|
|
224
|
-
* @param {number}
|
|
225
|
-
* @param {
|
|
226
|
-
* @
|
|
227
|
-
* @param {boolean} [largeArc] Flag indicating whether to draw the larger of the two possible arcs
|
|
228
|
-
* @param {boolean} [clockwise] Flag indicating the direction of the arc
|
|
229
|
-
* @returns {[number, number]} Interpolated coordinates on the arc
|
|
223
|
+
* @param {number} t Normalized time value to interpolate
|
|
224
|
+
* @param {number} cx X-axis coordinate of the center of the circle
|
|
225
|
+
* @param {number} cy Y-axis coordinate of the center of the circle
|
|
226
|
+
* @param {number} radius Radius of the circle
|
|
227
|
+
* @param {number} [startAngle] Start angle of the arc (in radians)
|
|
228
|
+
* @param {number} [endAngle] End angle of the arc (in radians)
|
|
229
|
+
* @param {boolean} [counterclockwise=false] Flag indicating the direction of the arc
|
|
230
|
+
* @returns {Point} Interpolated coordinates on the arc
|
|
230
231
|
*/
|
|
231
|
-
export function arc(t,
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
232
|
+
export function arc(t, cx, cy, radius, startAngle, endAngle, counterclockwise) {
|
|
233
|
+
return ellipse(t, cx, cy, radius, radius, 0, startAngle, endAngle, counterclockwise);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Check if two points are coincident
|
|
237
|
+
*
|
|
238
|
+
* @param {number} x1 X-axis coordinate of the first point
|
|
239
|
+
* @param {number} y1 Y-axis coordinate of the first point
|
|
240
|
+
* @param {number} x2 X-axis coordinate of the second point
|
|
241
|
+
* @param {number} y2 Y-axis coordinate of the second point
|
|
242
|
+
* @returns {boolean} True if the two are coincident, false otherwise
|
|
243
|
+
*/
|
|
244
|
+
export function isCoincident(x1, y1, x2, y2) {
|
|
245
|
+
return distance(x1, y1, x2, y2) <= EPSILON;
|
|
238
246
|
}
|
|
239
247
|
/**
|
|
240
|
-
*
|
|
248
|
+
* Check if three points are collinear (aligned on the same line)
|
|
241
249
|
*
|
|
242
|
-
* @param {number} x1
|
|
243
|
-
* @param {number} y1
|
|
244
|
-
* @param {number} x2
|
|
245
|
-
* @param {number} y2
|
|
246
|
-
* @param {number}
|
|
247
|
-
* @param {number}
|
|
248
|
-
* @
|
|
249
|
-
* @param {boolean} [largeArc] Flag indicating whether to draw the larger of the two possible arcs
|
|
250
|
-
* @param {boolean} [clockwise] Flag indicating the direction of the arc
|
|
251
|
-
* @returns Computed curvature
|
|
250
|
+
* @param {number} x1 X-axis coordinate of the first point
|
|
251
|
+
* @param {number} y1 Y-axis coordinate of the first point
|
|
252
|
+
* @param {number} x2 X-axis coordinate of the second point
|
|
253
|
+
* @param {number} y2 Y-axis coordinate of the second point
|
|
254
|
+
* @param {number} x3 X-axis coordinate of the third point
|
|
255
|
+
* @param {number} y3 Y-axis coordinate of the third point
|
|
256
|
+
* @returns {boolean} True if the three points are collinear, false otherwise
|
|
252
257
|
*/
|
|
253
|
-
export function
|
|
254
|
-
|
|
255
|
-
return deltaAngle / PI;
|
|
258
|
+
export function isCollinear(x1, y1, x2, y2, x3, y3) {
|
|
259
|
+
return Math.abs((x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2)) <= EPSILON;
|
|
256
260
|
}
|
|
257
261
|
/**
|
|
258
262
|
* Make a target fit a container
|
package/lib/prng.js
CHANGED
|
@@ -216,8 +216,9 @@ export function randomIndex(prng, weights) {
|
|
|
216
216
|
for (let weight of weights) {
|
|
217
217
|
totalWeight += weight;
|
|
218
218
|
}
|
|
219
|
-
if (totalWeight <= 0)
|
|
219
|
+
if (totalWeight <= 0) {
|
|
220
220
|
console.warn('PRNG randomIndex()', 'Weights must sum to > 0', totalWeight);
|
|
221
|
+
}
|
|
221
222
|
let weight = random(prng) * totalWeight;
|
|
222
223
|
for (let i = 0; i < weights.length; i++) {
|
|
223
224
|
if (weight < weights[i])
|
package/lib/random.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Point, Point3 } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Generate a random boolean (true or false)
|
|
4
4
|
*
|
|
@@ -69,31 +69,31 @@ export declare function randomGaussian(mean?: number, spread?: number): number;
|
|
|
69
69
|
* Produce a random 2D point around the perimiter of a unit circle
|
|
70
70
|
*
|
|
71
71
|
* @param {number} [radius=1] Radius of the circle
|
|
72
|
-
* @param {
|
|
73
|
-
* @returns {
|
|
72
|
+
* @param {Point} [target] Target point
|
|
73
|
+
* @returns {Point} Random 2D point on circle
|
|
74
74
|
*/
|
|
75
|
-
export declare function onCircle(radius?: number, target?:
|
|
75
|
+
export declare function onCircle(radius?: number, target?: Point): Point;
|
|
76
76
|
/**
|
|
77
77
|
* Produce a random 2D point inside a unit circle
|
|
78
78
|
*
|
|
79
79
|
* @param {number} [radius=1] Radius of the circle
|
|
80
|
-
* @param {
|
|
81
|
-
* @returns {
|
|
80
|
+
* @param {Point} [target] Target vector
|
|
81
|
+
* @returns {Point} Random 2D point inside circle
|
|
82
82
|
*/
|
|
83
|
-
export declare function insideCircle(radius?: number, target?:
|
|
83
|
+
export declare function insideCircle(radius?: number, target?: Point): Point;
|
|
84
84
|
/**
|
|
85
85
|
* Produce a random 3D point on the surface of a unit sphere
|
|
86
86
|
*
|
|
87
87
|
* @param {number} [radius=1] Radius of the sphere
|
|
88
|
-
* @param {
|
|
89
|
-
* @returns {
|
|
88
|
+
* @param {Point3} [target] Target vector
|
|
89
|
+
* @returns {Point3} Random 3D point on sphere
|
|
90
90
|
*/
|
|
91
|
-
export declare function onSphere(radius?: number, target?:
|
|
91
|
+
export declare function onSphere(radius?: number, target?: Point3): Point3;
|
|
92
92
|
/**
|
|
93
93
|
* Produce a random 3D point inside a unit sphere
|
|
94
94
|
*
|
|
95
95
|
* @param {number} [radius=1] Radius of the sphere
|
|
96
|
-
* @param {
|
|
97
|
-
* @returns {
|
|
96
|
+
* @param {Point3} [target] Target vector
|
|
97
|
+
* @returns {Point3} Random 3D point inside sphere
|
|
98
98
|
*/
|
|
99
|
-
export declare function insideSphere(radius?: number, target?:
|
|
99
|
+
export declare function insideSphere(radius?: number, target?: Point3): Point3;
|
package/lib/random.js
CHANGED
|
@@ -114,23 +114,23 @@ export function randomGaussian(mean = 0, spread = 1) {
|
|
|
114
114
|
* Produce a random 2D point around the perimiter of a unit circle
|
|
115
115
|
*
|
|
116
116
|
* @param {number} [radius=1] Radius of the circle
|
|
117
|
-
* @param {
|
|
118
|
-
* @returns {
|
|
117
|
+
* @param {Point} [target] Target point
|
|
118
|
+
* @returns {Point} Random 2D point on circle
|
|
119
119
|
*/
|
|
120
|
-
export function onCircle(radius = 1, target =
|
|
120
|
+
export function onCircle(radius = 1, target = [0, 0]) {
|
|
121
121
|
const angle = Math.random() * 2.0 * Math.PI;
|
|
122
|
-
target
|
|
123
|
-
target
|
|
122
|
+
target[0] = radius * Math.cos(angle);
|
|
123
|
+
target[1] = radius * Math.sin(angle);
|
|
124
124
|
return target;
|
|
125
125
|
}
|
|
126
126
|
/**
|
|
127
127
|
* Produce a random 2D point inside a unit circle
|
|
128
128
|
*
|
|
129
129
|
* @param {number} [radius=1] Radius of the circle
|
|
130
|
-
* @param {
|
|
131
|
-
* @returns {
|
|
130
|
+
* @param {Point} [target] Target vector
|
|
131
|
+
* @returns {Point} Random 2D point inside circle
|
|
132
132
|
*/
|
|
133
|
-
export function insideCircle(radius = 1, target =
|
|
133
|
+
export function insideCircle(radius = 1, target = [0, 0]) {
|
|
134
134
|
radius *= Math.random();
|
|
135
135
|
return onCircle(radius, target);
|
|
136
136
|
}
|
|
@@ -138,10 +138,10 @@ export function insideCircle(radius = 1, target = { x: 0, y: 0 }) {
|
|
|
138
138
|
* Produce a random 3D point on the surface of a unit sphere
|
|
139
139
|
*
|
|
140
140
|
* @param {number} [radius=1] Radius of the sphere
|
|
141
|
-
* @param {
|
|
142
|
-
* @returns {
|
|
141
|
+
* @param {Point3} [target] Target vector
|
|
142
|
+
* @returns {Point3} Random 3D point on sphere
|
|
143
143
|
*/
|
|
144
|
-
export function onSphere(radius = 1, target =
|
|
144
|
+
export function onSphere(radius = 1, target = [0, 0, 0]) {
|
|
145
145
|
const u = Math.random() * Math.PI * 2;
|
|
146
146
|
const v = Math.random() * 2 - 1;
|
|
147
147
|
const phi = u;
|
|
@@ -152,10 +152,10 @@ export function onSphere(radius = 1, target = { x: 0, y: 0, z: 0 }) {
|
|
|
152
152
|
* Produce a random 3D point inside a unit sphere
|
|
153
153
|
*
|
|
154
154
|
* @param {number} [radius=1] Radius of the sphere
|
|
155
|
-
* @param {
|
|
156
|
-
* @returns {
|
|
155
|
+
* @param {Point3} [target] Target vector
|
|
156
|
+
* @returns {Point3} Random 3D point inside sphere
|
|
157
157
|
*/
|
|
158
|
-
export function insideSphere(radius = 1, target =
|
|
158
|
+
export function insideSphere(radius = 1, target = [0, 0, 0]) {
|
|
159
159
|
radius *= Math.random();
|
|
160
160
|
return onSphere(radius, target);
|
|
161
161
|
}
|