-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
73 lines (68 loc) · 1.64 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Clamps a value between a minimum and maximum value.
*
* @example
* ```ts
* clamp(5, 0, 10); // 5
* clamp(-5, 0, 10); // 0
* clamp(15, 0, 10); // 10
* ```
*/
export const clamp = (value: number, min: number, max: number) =>
Math.max(min, Math.min(max, value));
/**
* Factorial of a number.
*
* @example
* ```ts
* factorial(5); // 120
* ```
*/
export const factorial = (n: number): number =>
n === 0 ? 1 : n * factorial(n - 1);
/**
* Generates a random number between the specified range.
* If no range is specified, the default range is [0, 1].
*/
export const random = (min = 0, max = 1): number =>
Math.random() * (max - min) + min;
/**
* Average of numbers.
*
* @example
* ```ts
* average(1, 2, 3, 4, 5); // 3
* ```
*/
export const average = (...numbers: number[]): number =>
numbers.reduce((acc, val) => acc + val, 0) / numbers.length;
/**
* Bezier curve.
* @example
* ```ts
* bezier(0.1, 0.2, 0.3, 0.4, 0.5); // 0.2375
* ```
* @see https://en.wikipedia.org/wiki/B%C3%A9zier_curve
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/bezierCurveTo
*/
export const bezier = (
t: number,
p0: number,
p1: number,
p2: number,
p3: number,
): number =>
Math.pow(1 - t, 3) * p0 +
3 * Math.pow(1 - t, 2) * t * p1 +
3 * (1 - t) * Math.pow(t, 2) * p2 +
Math.pow(t, 3) * p3;
/**
* FuzzyEquals is a function that compares two numbers and returns true if they are approximately equal.
*
* @example
* ```ts
* fuzzyEquals(0.1 + 0.2, 0.3); // true
* ```
*/
export const fuzzyEquals = (a: number, b: number, epsilon = 0.0001) =>
Math.abs(a - b) < epsilon;