@pie-lib/graphing-utils 0.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/dist/index.d.ts +104 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +90 -0
- package/package.json +29 -0
- package/src/index.ts +238 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @synced-from pie-lib/packages/graphing-utils/src/index.js
|
|
3
|
+
* @auto-generated
|
|
4
|
+
*
|
|
5
|
+
* This file is automatically synced from pie-elements and converted to TypeScript.
|
|
6
|
+
* Manual edits will be overwritten on next sync.
|
|
7
|
+
* To make changes, edit the upstream JavaScript file and run sync again.
|
|
8
|
+
*/
|
|
9
|
+
export declare const FREQ_DIVIDER = 16;
|
|
10
|
+
export declare const getAmplitudeAndFreq: (root: any, edge: any) => {
|
|
11
|
+
freq: number;
|
|
12
|
+
amplitude: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* build the x points with root as the anchor.
|
|
16
|
+
*/
|
|
17
|
+
export declare const xPoints: (root: any, freq: any, min: any, max: any) => any;
|
|
18
|
+
/**
|
|
19
|
+
* Calculate y for x value using amp, freq and shift
|
|
20
|
+
* https://www.desmos.com/calculator/f1psfoiiv6
|
|
21
|
+
*/
|
|
22
|
+
export declare const sinY: (amplitude: any, freq: any, shift: any) => (x: any) => any;
|
|
23
|
+
/**
|
|
24
|
+
* You need three points for the quadratic eq, so we mirror the edge point
|
|
25
|
+
* https://www.desmos.com/calculator/xhz1xqgpca
|
|
26
|
+
*/
|
|
27
|
+
export declare const parabolaFromTwoPoints: (root: any, edge: any) => (x: any) => any;
|
|
28
|
+
export declare const parabolaFromThreePoints: (one: any, two: any, three: any) => (x: any) => any;
|
|
29
|
+
/** derive a,b,c from the 3 points */
|
|
30
|
+
export declare const pointsToABC: (one: any, two: any, three: any) => {
|
|
31
|
+
a: number;
|
|
32
|
+
b: number;
|
|
33
|
+
c: number;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* y=ax^2+bx+c
|
|
37
|
+
*/
|
|
38
|
+
export declare const parabola: (a: any, b: any, c: any) => (x: any) => any;
|
|
39
|
+
/**
|
|
40
|
+
* Absolute value from two points Root (Vertex) the point where the two rays intersect and edge One point on a ray.
|
|
41
|
+
* @param root 1st point selected on graph
|
|
42
|
+
* @param edge 2nd point selected on graph
|
|
43
|
+
* https://www.desmos.com/calculator/rarxiatpip
|
|
44
|
+
*/
|
|
45
|
+
export declare const absoluteFromTwoPoints: (root: any, edge: any) => (x: any) => any;
|
|
46
|
+
export declare const pointsToAForAbsolute: (one: any, two: any) => number;
|
|
47
|
+
/**
|
|
48
|
+
* y=a*abs(x-h)+k
|
|
49
|
+
*/
|
|
50
|
+
export declare const absolute: (a: any, h: any, k: any) => (x: any) => any;
|
|
51
|
+
/**
|
|
52
|
+
* Exponential from two points on exponential graph.
|
|
53
|
+
* @param root 1st point selected on graph
|
|
54
|
+
* @param edge 2nd point selected on graph
|
|
55
|
+
* https://www.desmos.com/calculator/3fisjexbvp
|
|
56
|
+
*/
|
|
57
|
+
export declare const exponentialFromTwoPoints: (root: any, edge: any) => (x: any) => number;
|
|
58
|
+
export declare const pointsToABForExponential: (one: any, two: any) => {
|
|
59
|
+
a: number;
|
|
60
|
+
b: number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* y=a*(b)^x
|
|
64
|
+
*/
|
|
65
|
+
export declare const exponential: (a: any, b: any) => (x: any) => number;
|
|
66
|
+
/**
|
|
67
|
+
* Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max
|
|
68
|
+
* @param {*} domain
|
|
69
|
+
* @param {*} range
|
|
70
|
+
* @param {*} root
|
|
71
|
+
* @param {*} edge
|
|
72
|
+
* @param {*} yFn
|
|
73
|
+
* @param {*} excludeOutsidePoints
|
|
74
|
+
*/
|
|
75
|
+
export declare const buildDataPoints: (domain: any, range: any, root: any, edge: any, yFn: any, excludeOutsidePoints?: boolean) => any;
|
|
76
|
+
declare const _default: {
|
|
77
|
+
FREQ_DIVIDER: number;
|
|
78
|
+
getAmplitudeAndFreq: (root: any, edge: any) => {
|
|
79
|
+
freq: number;
|
|
80
|
+
amplitude: number;
|
|
81
|
+
};
|
|
82
|
+
xPoints: (root: any, freq: any, min: any, max: any) => any;
|
|
83
|
+
sinY: (amplitude: any, freq: any, shift: any) => (x: any) => any;
|
|
84
|
+
parabolaFromTwoPoints: (root: any, edge: any) => (x: any) => any;
|
|
85
|
+
parabolaFromThreePoints: (one: any, two: any, three: any) => (x: any) => any;
|
|
86
|
+
pointsToABC: (one: any, two: any, three: any) => {
|
|
87
|
+
a: number;
|
|
88
|
+
b: number;
|
|
89
|
+
c: number;
|
|
90
|
+
};
|
|
91
|
+
parabola: (a: any, b: any, c: any) => (x: any) => any;
|
|
92
|
+
absoluteFromTwoPoints: (root: any, edge: any) => (x: any) => any;
|
|
93
|
+
pointsToAForAbsolute: (one: any, two: any) => number;
|
|
94
|
+
absolute: (a: any, h: any, k: any) => (x: any) => any;
|
|
95
|
+
exponentialFromTwoPoints: (root: any, edge: any) => (x: any) => number;
|
|
96
|
+
pointsToABForExponential: (one: any, two: any) => {
|
|
97
|
+
a: number;
|
|
98
|
+
b: number;
|
|
99
|
+
};
|
|
100
|
+
exponential: (a: any, b: any) => (x: any) => number;
|
|
101
|
+
buildDataPoints: (domain: any, range: any, root: any, edge: any, yFn: any, excludeOutsidePoints?: boolean) => any;
|
|
102
|
+
};
|
|
103
|
+
export default _default;
|
|
104
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAQH,eAAO,MAAM,YAAY,KAAK,CAAC;AAE/B,eAAO,MAAM,mBAAmB,GAAI,SAAI,EAAE,SAAI;;;CAc7C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,OAAO,GAAI,SAAI,EAAE,SAAI,EAAE,QAAG,EAAE,QAAG,QAO3C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,IAAI,GAAI,cAAS,EAAE,SAAI,EAAE,UAAK,MAAM,MAAC,QAQjD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAAI,SAAI,EAAE,SAAI,oBAI/C,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,QAAG,EAAE,QAAG,EAAE,UAAK,oBAKtD,CAAC;AACF,qCAAqC;AACrC,eAAO,MAAM,WAAW,GAAI,QAAG,EAAE,QAAG,EAAE,UAAK;;;;CAc1C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAC,EAAE,MAAC,EAAE,MAAC,MAAM,MAAC,QAAmC,CAAC;AAE3E;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,SAAI,EAAE,SAAI,oBAI/C,CAAC;AAOF,eAAO,MAAM,oBAAoB,GAAI,QAAG,EAAE,QAAG,WAQ5C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAC,EAAE,MAAC,EAAE,MAAC,MAAM,MAAC,QAA4B,CAAC;AAEpE;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,GAAI,SAAI,EAAE,SAAI,uBAIlD,CAAC;AAOF,eAAO,MAAM,wBAAwB,GAAI,QAAG,EAAE,QAAG;;;CAMhD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,MAAC,EAAE,MAAC,MAAM,MAAC,WAAuB,CAAC;AAE/D;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,GAAI,WAAM,EAAE,UAAK,EAAE,SAAI,EAAE,SAAI,EAAE,QAAG,EAAE,8BAA4B,QAkD3F,CAAC;;;;;;;;sDAzK+C,MAAC;;;;;;;;2CA8CZ,MAAC;;;2CAgCD,MAAC;;;;;;sCA8BD,MAAC;;;AA+DvC,wBAgBE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import e from "debug";
|
|
2
|
+
import { concat as t, range as n, rangeRight as r } from "lodash-es";
|
|
3
|
+
import i from "@mapbox/point-geometry";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
var a = e("pie-lib:graphing:tools:utils"), o = 16, s = (e, t) => {
|
|
6
|
+
if (!t || e.x == t.x) return {
|
|
7
|
+
freq: 0,
|
|
8
|
+
amplitude: 0
|
|
9
|
+
};
|
|
10
|
+
let n = new i(e.x, e.y), r = new i(t.x, t.y).sub(n);
|
|
11
|
+
return {
|
|
12
|
+
freq: r.x * 4,
|
|
13
|
+
amplitude: r.y
|
|
14
|
+
};
|
|
15
|
+
}, c = (e, i, o, s) => (i = Math.abs(i), a("[xPoints] root:", e, "freq:", i, " min:", o, " max:", s), t(r(e, o - i, i * -1), n(e + i, s + i, i))), l = (e, t, n) => (r) => {
|
|
16
|
+
n = {
|
|
17
|
+
phase: 0,
|
|
18
|
+
vertical: 0,
|
|
19
|
+
...n
|
|
20
|
+
};
|
|
21
|
+
let i = Math.PI * 2 * (r - n.phase) / t;
|
|
22
|
+
return e * parseFloat(Math.sin(i)).toFixed(10) + n.vertical;
|
|
23
|
+
}, u = (e, t) => {
|
|
24
|
+
t ||= { ...e };
|
|
25
|
+
let n = {
|
|
26
|
+
x: e.x - (t.x - e.x),
|
|
27
|
+
y: t.y
|
|
28
|
+
};
|
|
29
|
+
return d(e, t, n);
|
|
30
|
+
}, d = (e, t, n) => {
|
|
31
|
+
a(e, t, n);
|
|
32
|
+
let { a: r, b: i, c: o } = f(e, t, n);
|
|
33
|
+
return a(r, i, o), p(r, i, o);
|
|
34
|
+
}, f = (e, t, n) => {
|
|
35
|
+
let r = e.x ** 2 * -1 + t.x ** 2, i = e.x * -1 + t.x, a = e.y * -1 + t.y, o = t.x ** 2 * -1 + n.x ** 2, s = t.x * -1 + n.x, c = t.y * -1 + n.y, l = s / i * -1, u = l * r + o, d = (l * a + c) / u, f = (a - r * d) / i;
|
|
36
|
+
return {
|
|
37
|
+
a: d,
|
|
38
|
+
b: f,
|
|
39
|
+
c: e.y - d * e.x ** 2 - f * e.x
|
|
40
|
+
};
|
|
41
|
+
}, p = (e, t, n) => (r) => e * r ** 2 + t * r + n, m = (e, t) => (t ||= { ...e }, g(h(e, t), e.x, e.y)), h = (e, t) => {
|
|
42
|
+
let n = (t.y - e.y) / (t.x - e.x);
|
|
43
|
+
return t.x < e.x && (n *= -1), n;
|
|
44
|
+
}, g = (e, t, n) => (r) => e * Math.abs(r - t) + n, _ = (e, t) => {
|
|
45
|
+
t ||= { ...e };
|
|
46
|
+
let { a: n, b: r } = v(e, t);
|
|
47
|
+
return y(n, r);
|
|
48
|
+
}, v = (e, t) => {
|
|
49
|
+
let n = (e.y / t.y) ** +(1 / (e.x - t.x));
|
|
50
|
+
return {
|
|
51
|
+
a: e.y / n ** +e.x,
|
|
52
|
+
b: n
|
|
53
|
+
};
|
|
54
|
+
}, y = (e, t) => (n) => e * t ** +n, b = (e, t, n, r, o, s = !1) => {
|
|
55
|
+
a("[buildDataPoints] domain:", e, "range:", t, "root:", n, "edge:", r), e = {
|
|
56
|
+
min: 0,
|
|
57
|
+
max: 0,
|
|
58
|
+
step: 1,
|
|
59
|
+
...e
|
|
60
|
+
}, t = {
|
|
61
|
+
min: 0,
|
|
62
|
+
max: 0,
|
|
63
|
+
step: 1,
|
|
64
|
+
...t
|
|
65
|
+
}, r ||= { ...n };
|
|
66
|
+
let l = c(Math.min(n.x, r.x), e.step, e.min - e.step, e.max + e.step);
|
|
67
|
+
a("[buildDataPoints]:xPts:", l);
|
|
68
|
+
let u = -1, d = -1, f = l.map((e) => new i(e, o(e)));
|
|
69
|
+
if (!s) return f;
|
|
70
|
+
let p = f.reduce((e, n, r) => n.y >= t.min - t.step && n.y <= t.max + t.step ? (u === -1 && (u = r), d < r && (d = r), [...e, n]) : e, []);
|
|
71
|
+
return u - 1 >= 0 && p.unshift(f[u - 1]), d + 1 <= f.length - 1 && p.push(f[d + 1]), f;
|
|
72
|
+
}, x = {
|
|
73
|
+
FREQ_DIVIDER: 16,
|
|
74
|
+
getAmplitudeAndFreq: s,
|
|
75
|
+
xPoints: c,
|
|
76
|
+
sinY: l,
|
|
77
|
+
parabolaFromTwoPoints: u,
|
|
78
|
+
parabolaFromThreePoints: d,
|
|
79
|
+
pointsToABC: f,
|
|
80
|
+
parabola: p,
|
|
81
|
+
absoluteFromTwoPoints: m,
|
|
82
|
+
pointsToAForAbsolute: h,
|
|
83
|
+
absolute: g,
|
|
84
|
+
exponentialFromTwoPoints: _,
|
|
85
|
+
pointsToABForExponential: v,
|
|
86
|
+
exponential: y,
|
|
87
|
+
buildDataPoints: b
|
|
88
|
+
};
|
|
89
|
+
//#endregion
|
|
90
|
+
export { o as FREQ_DIVIDER, g as absolute, m as absoluteFromTwoPoints, b as buildDataPoints, x as default, y as exponential, _ as exponentialFromTwoPoints, s as getAmplitudeAndFreq, p as parabola, d as parabolaFromThreePoints, u as parabolaFromTwoPoints, f as pointsToABC, v as pointsToABForExponential, h as pointsToAForAbsolute, l as sinY, c as xPoints };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pie-lib/graphing-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@mapbox/point-geometry": "^1.1.0",
|
|
7
|
+
"debug": "^4.1.1",
|
|
8
|
+
"lodash-es": "^4.17.23"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "bun x vite build && bun x tsc --emitDeclarationOnly",
|
|
26
|
+
"dev": "bun x vite",
|
|
27
|
+
"test": "bun x vitest run"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* @synced-from pie-lib/packages/graphing-utils/src/index.js
|
|
4
|
+
* @auto-generated
|
|
5
|
+
*
|
|
6
|
+
* This file is automatically synced from pie-elements and converted to TypeScript.
|
|
7
|
+
* Manual edits will be overwritten on next sync.
|
|
8
|
+
* To make changes, edit the upstream JavaScript file and run sync again.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import debug from 'debug';
|
|
12
|
+
import { concat, range, rangeRight } from 'lodash-es';
|
|
13
|
+
import Point from '@mapbox/point-geometry';
|
|
14
|
+
|
|
15
|
+
const log = debug('pie-lib:graphing:tools:utils');
|
|
16
|
+
|
|
17
|
+
export const FREQ_DIVIDER = 16;
|
|
18
|
+
|
|
19
|
+
export const getAmplitudeAndFreq = (root, edge) => {
|
|
20
|
+
if (!edge) {
|
|
21
|
+
return { freq: 0, amplitude: 0 };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (root.x == edge.x) {
|
|
25
|
+
return { freq: 0, amplitude: 0 };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const r = new Point(root.x, root.y);
|
|
29
|
+
const e = new Point(edge.x, edge.y);
|
|
30
|
+
const d = e.sub(r);
|
|
31
|
+
// edge point describes 1/4 of the freq
|
|
32
|
+
return { freq: d.x * 4, amplitude: d.y };
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* build the x points with root as the anchor.
|
|
37
|
+
*/
|
|
38
|
+
export const xPoints = (root, freq, min, max) => {
|
|
39
|
+
freq = Math.abs(freq);
|
|
40
|
+
log('[xPoints] root:', root, 'freq:', freq, ' min:', min, ' max:', max);
|
|
41
|
+
const leftpoints = rangeRight(root, min - freq, freq * -1);
|
|
42
|
+
const rightpoints = range(root + freq, max + freq, freq);
|
|
43
|
+
|
|
44
|
+
return concat(leftpoints, rightpoints);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Calculate y for x value using amp, freq and shift
|
|
49
|
+
* https://www.desmos.com/calculator/f1psfoiiv6
|
|
50
|
+
*/
|
|
51
|
+
export const sinY = (amplitude, freq, shift) => (x) => {
|
|
52
|
+
shift = { phase: 0, vertical: 0, ...shift };
|
|
53
|
+
const TWO_PI = Math.PI * 2;
|
|
54
|
+
const num = TWO_PI * (x - shift.phase);
|
|
55
|
+
const frac = num / freq;
|
|
56
|
+
const sin = Math.sin(frac);
|
|
57
|
+
const sinFloat = parseFloat(sin).toFixed(10);
|
|
58
|
+
return amplitude * sinFloat + shift.vertical;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* You need three points for the quadratic eq, so we mirror the edge point
|
|
63
|
+
* https://www.desmos.com/calculator/xhz1xqgpca
|
|
64
|
+
*/
|
|
65
|
+
export const parabolaFromTwoPoints = (root, edge) => {
|
|
66
|
+
edge = edge || { ...root };
|
|
67
|
+
const mirrorEdge = { x: root.x - (edge.x - root.x), y: edge.y };
|
|
68
|
+
return parabolaFromThreePoints(root, edge, mirrorEdge);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const parabolaFromThreePoints = (one, two, three) => {
|
|
72
|
+
log(one, two, three);
|
|
73
|
+
const { a, b, c } = pointsToABC(one, two, three);
|
|
74
|
+
log(a, b, c);
|
|
75
|
+
return parabola(a, b, c);
|
|
76
|
+
};
|
|
77
|
+
/** derive a,b,c from the 3 points */
|
|
78
|
+
export const pointsToABC = (one, two, three) => {
|
|
79
|
+
const a1 = Math.pow(one.x, 2) * -1 + Math.pow(two.x, 2);
|
|
80
|
+
const b1 = one.x * -1 + two.x;
|
|
81
|
+
const d1 = one.y * -1 + two.y;
|
|
82
|
+
const a2 = Math.pow(two.x, 2) * -1 + Math.pow(three.x, 2);
|
|
83
|
+
const b2 = two.x * -1 + three.x;
|
|
84
|
+
const d2 = two.y * -1 + three.y;
|
|
85
|
+
const bMult = (b2 / b1) * -1;
|
|
86
|
+
const a3 = bMult * a1 + a2;
|
|
87
|
+
const d3 = bMult * d1 + d2;
|
|
88
|
+
const a = d3 / a3;
|
|
89
|
+
const b = (d1 - a1 * a) / b1;
|
|
90
|
+
const c = one.y - a * Math.pow(one.x, 2) - b * one.x;
|
|
91
|
+
return { a, b, c };
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* y=ax^2+bx+c
|
|
96
|
+
*/
|
|
97
|
+
export const parabola = (a, b, c) => (x) => a * Math.pow(x, 2) + b * x + c;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Absolute value from two points Root (Vertex) the point where the two rays intersect and edge One point on a ray.
|
|
101
|
+
* @param root 1st point selected on graph
|
|
102
|
+
* @param edge 2nd point selected on graph
|
|
103
|
+
* https://www.desmos.com/calculator/rarxiatpip
|
|
104
|
+
*/
|
|
105
|
+
export const absoluteFromTwoPoints = (root, edge) => {
|
|
106
|
+
edge = edge || { ...root };
|
|
107
|
+
const a = pointsToAForAbsolute(root, edge);
|
|
108
|
+
return absolute(a, root.x, root.y);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/*
|
|
112
|
+
* Finds value of a in y=a*abs(x-h)+k function
|
|
113
|
+
* @param one 1st point
|
|
114
|
+
* @param two 2nd point
|
|
115
|
+
* */
|
|
116
|
+
export const pointsToAForAbsolute = (one, two) => {
|
|
117
|
+
const y1 = two.y - one.y;
|
|
118
|
+
const x1 = two.x - one.x;
|
|
119
|
+
let a = y1 / x1;
|
|
120
|
+
if (two.x < one.x) {
|
|
121
|
+
a = a * -1;
|
|
122
|
+
}
|
|
123
|
+
return a;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* y=a*abs(x-h)+k
|
|
128
|
+
*/
|
|
129
|
+
export const absolute = (a, h, k) => (x) => a * Math.abs(x - h) + k;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Exponential from two points on exponential graph.
|
|
133
|
+
* @param root 1st point selected on graph
|
|
134
|
+
* @param edge 2nd point selected on graph
|
|
135
|
+
* https://www.desmos.com/calculator/3fisjexbvp
|
|
136
|
+
*/
|
|
137
|
+
export const exponentialFromTwoPoints = (root, edge) => {
|
|
138
|
+
edge = edge || { ...root };
|
|
139
|
+
const { a, b } = pointsToABForExponential(root, edge);
|
|
140
|
+
return exponential(a, b);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/*
|
|
144
|
+
* Finds value of a and b in y=a*(b)^x function
|
|
145
|
+
* @param one 1st point
|
|
146
|
+
* @param two 2nd point
|
|
147
|
+
* */
|
|
148
|
+
export const pointsToABForExponential = (one, two) => {
|
|
149
|
+
const p = one.y / two.y;
|
|
150
|
+
const r = 1 / (one.x - two.x);
|
|
151
|
+
const b = Math.pow(p, r);
|
|
152
|
+
const a = one.y / Math.pow(b, one.x);
|
|
153
|
+
return { a, b };
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* y=a*(b)^x
|
|
158
|
+
*/
|
|
159
|
+
export const exponential = (a, b) => (x) => a * Math.pow(b, x);
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max
|
|
163
|
+
* @param {*} domain
|
|
164
|
+
* @param {*} range
|
|
165
|
+
* @param {*} root
|
|
166
|
+
* @param {*} edge
|
|
167
|
+
* @param {*} yFn
|
|
168
|
+
* @param {*} excludeOutsidePoints
|
|
169
|
+
*/
|
|
170
|
+
export const buildDataPoints = (domain, range, root, edge, yFn, excludeOutsidePoints = false) => {
|
|
171
|
+
log('[buildDataPoints] domain:', domain, 'range:', range, 'root:', root, 'edge:', edge);
|
|
172
|
+
|
|
173
|
+
domain = { min: 0, max: 0, step: 1, ...domain };
|
|
174
|
+
range = { min: 0, max: 0, step: 1, ...range };
|
|
175
|
+
edge = edge ? edge : { ...root };
|
|
176
|
+
|
|
177
|
+
const minX = Math.min(root.x, edge.x);
|
|
178
|
+
const xPts = xPoints(minX, domain.step, domain.min - domain.step, domain.max + domain.step);
|
|
179
|
+
|
|
180
|
+
log('[buildDataPoints]:xPts:', xPts);
|
|
181
|
+
|
|
182
|
+
let startIndex = -1;
|
|
183
|
+
let endIndex = -1;
|
|
184
|
+
|
|
185
|
+
// generate points based on the yFn
|
|
186
|
+
const points = xPts.map((v) => new Point(v, yFn(v)));
|
|
187
|
+
|
|
188
|
+
if (!excludeOutsidePoints) {
|
|
189
|
+
return points;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// exclude the points with y outside range min and max
|
|
193
|
+
const filteredPoints = points.reduce((acc, val, index) => {
|
|
194
|
+
if (val.y >= range.min - range.step && val.y <= range.max + range.step) {
|
|
195
|
+
if (startIndex === -1) {
|
|
196
|
+
startIndex = index;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (endIndex < index) {
|
|
200
|
+
endIndex = index;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return [...acc, val];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return acc;
|
|
207
|
+
}, []);
|
|
208
|
+
|
|
209
|
+
// add the first value outside range min and max smaller than range.min if exists
|
|
210
|
+
if (startIndex - 1 >= 0) {
|
|
211
|
+
filteredPoints.unshift(points[startIndex - 1]);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// add the first value outside range min and max greater than range.min if exists
|
|
215
|
+
if (endIndex + 1 <= points.length - 1) {
|
|
216
|
+
filteredPoints.push(points[endIndex + 1]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return points;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
export default {
|
|
223
|
+
FREQ_DIVIDER,
|
|
224
|
+
getAmplitudeAndFreq,
|
|
225
|
+
xPoints,
|
|
226
|
+
sinY,
|
|
227
|
+
parabolaFromTwoPoints,
|
|
228
|
+
parabolaFromThreePoints,
|
|
229
|
+
pointsToABC,
|
|
230
|
+
parabola,
|
|
231
|
+
absoluteFromTwoPoints,
|
|
232
|
+
pointsToAForAbsolute,
|
|
233
|
+
absolute,
|
|
234
|
+
exponentialFromTwoPoints,
|
|
235
|
+
pointsToABForExponential,
|
|
236
|
+
exponential,
|
|
237
|
+
buildDataPoints,
|
|
238
|
+
};
|