@pie-lib/graphing-utils 3.0.2 → 3.0.3-next.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.js +90 -0
- package/package.json +27 -12
- package/CHANGELOG.json +0 -1
- package/CHANGELOG.md +0 -384
- package/LICENSE.md +0 -5
- package/lib/index.js +0 -275
- package/lib/index.js.map +0 -1
- package/src/__tests__/utils.test.js +0 -299
- package/src/index.js +0 -228
package/src/index.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import { concat, range, rangeRight } from 'lodash-es';
|
|
3
|
-
import Point from '@mapbox/point-geometry';
|
|
4
|
-
|
|
5
|
-
const log = debug('pie-lib:graphing:tools:utils');
|
|
6
|
-
|
|
7
|
-
export const FREQ_DIVIDER = 16;
|
|
8
|
-
|
|
9
|
-
export const getAmplitudeAndFreq = (root, edge) => {
|
|
10
|
-
if (!edge) {
|
|
11
|
-
return { freq: 0, amplitude: 0 };
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (root.x == edge.x) {
|
|
15
|
-
return { freq: 0, amplitude: 0 };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const r = new Point(root.x, root.y);
|
|
19
|
-
const e = new Point(edge.x, edge.y);
|
|
20
|
-
const d = e.sub(r);
|
|
21
|
-
// edge point describes 1/4 of the freq
|
|
22
|
-
return { freq: d.x * 4, amplitude: d.y };
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* build the x points with root as the anchor.
|
|
27
|
-
*/
|
|
28
|
-
export const xPoints = (root, freq, min, max) => {
|
|
29
|
-
freq = Math.abs(freq);
|
|
30
|
-
log('[xPoints] root:', root, 'freq:', freq, ' min:', min, ' max:', max);
|
|
31
|
-
const leftpoints = rangeRight(root, min - freq, freq * -1);
|
|
32
|
-
const rightpoints = range(root + freq, max + freq, freq);
|
|
33
|
-
|
|
34
|
-
return concat(leftpoints, rightpoints);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Calculate y for x value using amp, freq and shift
|
|
39
|
-
* https://www.desmos.com/calculator/f1psfoiiv6
|
|
40
|
-
*/
|
|
41
|
-
export const sinY = (amplitude, freq, shift) => (x) => {
|
|
42
|
-
shift = { phase: 0, vertical: 0, ...shift };
|
|
43
|
-
const TWO_PI = Math.PI * 2;
|
|
44
|
-
const num = TWO_PI * (x - shift.phase);
|
|
45
|
-
const frac = num / freq;
|
|
46
|
-
const sin = Math.sin(frac);
|
|
47
|
-
const sinFloat = parseFloat(sin).toFixed(10);
|
|
48
|
-
return amplitude * sinFloat + shift.vertical;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* You need three points for the quadratic eq, so we mirror the edge point
|
|
53
|
-
* https://www.desmos.com/calculator/xhz1xqgpca
|
|
54
|
-
*/
|
|
55
|
-
export const parabolaFromTwoPoints = (root, edge) => {
|
|
56
|
-
edge = edge || { ...root };
|
|
57
|
-
const mirrorEdge = { x: root.x - (edge.x - root.x), y: edge.y };
|
|
58
|
-
return parabolaFromThreePoints(root, edge, mirrorEdge);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export const parabolaFromThreePoints = (one, two, three) => {
|
|
62
|
-
log(one, two, three);
|
|
63
|
-
const { a, b, c } = pointsToABC(one, two, three);
|
|
64
|
-
log(a, b, c);
|
|
65
|
-
return parabola(a, b, c);
|
|
66
|
-
};
|
|
67
|
-
/** derive a,b,c from the 3 points */
|
|
68
|
-
export const pointsToABC = (one, two, three) => {
|
|
69
|
-
const a1 = Math.pow(one.x, 2) * -1 + Math.pow(two.x, 2);
|
|
70
|
-
const b1 = one.x * -1 + two.x;
|
|
71
|
-
const d1 = one.y * -1 + two.y;
|
|
72
|
-
const a2 = Math.pow(two.x, 2) * -1 + Math.pow(three.x, 2);
|
|
73
|
-
const b2 = two.x * -1 + three.x;
|
|
74
|
-
const d2 = two.y * -1 + three.y;
|
|
75
|
-
const bMult = (b2 / b1) * -1;
|
|
76
|
-
const a3 = bMult * a1 + a2;
|
|
77
|
-
const d3 = bMult * d1 + d2;
|
|
78
|
-
const a = d3 / a3;
|
|
79
|
-
const b = (d1 - a1 * a) / b1;
|
|
80
|
-
const c = one.y - a * Math.pow(one.x, 2) - b * one.x;
|
|
81
|
-
return { a, b, c };
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* y=ax^2+bx+c
|
|
86
|
-
*/
|
|
87
|
-
export const parabola = (a, b, c) => (x) => a * Math.pow(x, 2) + b * x + c;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Absolute value from two points Root (Vertex) the point where the two rays intersect and edge One point on a ray.
|
|
91
|
-
* @param root 1st point selected on graph
|
|
92
|
-
* @param edge 2nd point selected on graph
|
|
93
|
-
* https://www.desmos.com/calculator/rarxiatpip
|
|
94
|
-
*/
|
|
95
|
-
export const absoluteFromTwoPoints = (root, edge) => {
|
|
96
|
-
edge = edge || { ...root };
|
|
97
|
-
const a = pointsToAForAbsolute(root, edge);
|
|
98
|
-
return absolute(a, root.x, root.y);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/*
|
|
102
|
-
* Finds value of a in y=a*abs(x-h)+k function
|
|
103
|
-
* @param one 1st point
|
|
104
|
-
* @param two 2nd point
|
|
105
|
-
* */
|
|
106
|
-
export const pointsToAForAbsolute = (one, two) => {
|
|
107
|
-
const y1 = two.y - one.y;
|
|
108
|
-
const x1 = two.x - one.x;
|
|
109
|
-
let a = y1 / x1;
|
|
110
|
-
if (two.x < one.x) {
|
|
111
|
-
a = a * -1;
|
|
112
|
-
}
|
|
113
|
-
return a;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* y=a*abs(x-h)+k
|
|
118
|
-
*/
|
|
119
|
-
export const absolute = (a, h, k) => (x) => a * Math.abs(x - h) + k;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Exponential from two points on exponential graph.
|
|
123
|
-
* @param root 1st point selected on graph
|
|
124
|
-
* @param edge 2nd point selected on graph
|
|
125
|
-
* https://www.desmos.com/calculator/3fisjexbvp
|
|
126
|
-
*/
|
|
127
|
-
export const exponentialFromTwoPoints = (root, edge) => {
|
|
128
|
-
edge = edge || { ...root };
|
|
129
|
-
const { a, b } = pointsToABForExponential(root, edge);
|
|
130
|
-
return exponential(a, b);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
/*
|
|
134
|
-
* Finds value of a and b in y=a*(b)^x function
|
|
135
|
-
* @param one 1st point
|
|
136
|
-
* @param two 2nd point
|
|
137
|
-
* */
|
|
138
|
-
export const pointsToABForExponential = (one, two) => {
|
|
139
|
-
const p = one.y / two.y;
|
|
140
|
-
const r = 1 / (one.x - two.x);
|
|
141
|
-
const b = Math.pow(p, r);
|
|
142
|
-
const a = one.y / Math.pow(b, one.x);
|
|
143
|
-
return { a, b };
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* y=a*(b)^x
|
|
148
|
-
*/
|
|
149
|
-
export const exponential = (a, b) => (x) => a * Math.pow(b, x);
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max
|
|
153
|
-
* @param {*} domain
|
|
154
|
-
* @param {*} range
|
|
155
|
-
* @param {*} root
|
|
156
|
-
* @param {*} edge
|
|
157
|
-
* @param {*} yFn
|
|
158
|
-
* @param {*} excludeOutsidePoints
|
|
159
|
-
*/
|
|
160
|
-
export const buildDataPoints = (domain, range, root, edge, yFn, excludeOutsidePoints = false) => {
|
|
161
|
-
log('[buildDataPoints] domain:', domain, 'range:', range, 'root:', root, 'edge:', edge);
|
|
162
|
-
|
|
163
|
-
domain = { min: 0, max: 0, step: 1, ...domain };
|
|
164
|
-
range = { min: 0, max: 0, step: 1, ...range };
|
|
165
|
-
edge = edge ? edge : { ...root };
|
|
166
|
-
|
|
167
|
-
const minX = Math.min(root.x, edge.x);
|
|
168
|
-
const xPts = xPoints(minX, domain.step, domain.min - domain.step, domain.max + domain.step);
|
|
169
|
-
|
|
170
|
-
log('[buildDataPoints]:xPts:', xPts);
|
|
171
|
-
|
|
172
|
-
let startIndex = -1;
|
|
173
|
-
let endIndex = -1;
|
|
174
|
-
|
|
175
|
-
// generate points based on the yFn
|
|
176
|
-
const points = xPts.map((v) => new Point(v, yFn(v)));
|
|
177
|
-
|
|
178
|
-
if (!excludeOutsidePoints) {
|
|
179
|
-
return points;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// exclude the points with y outside range min and max
|
|
183
|
-
const filteredPoints = points.reduce((acc, val, index) => {
|
|
184
|
-
if (val.y >= range.min - range.step && val.y <= range.max + range.step) {
|
|
185
|
-
if (startIndex === -1) {
|
|
186
|
-
startIndex = index;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (endIndex < index) {
|
|
190
|
-
endIndex = index;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return [...acc, val];
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return acc;
|
|
197
|
-
}, []);
|
|
198
|
-
|
|
199
|
-
// add the first value outside range min and max smaller than range.min if exists
|
|
200
|
-
if (startIndex - 1 >= 0) {
|
|
201
|
-
filteredPoints.unshift(points[startIndex - 1]);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// add the first value outside range min and max greater than range.min if exists
|
|
205
|
-
if (endIndex + 1 <= points.length - 1) {
|
|
206
|
-
filteredPoints.push(points[endIndex + 1]);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return points;
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
export default {
|
|
213
|
-
FREQ_DIVIDER,
|
|
214
|
-
getAmplitudeAndFreq,
|
|
215
|
-
xPoints,
|
|
216
|
-
sinY,
|
|
217
|
-
parabolaFromTwoPoints,
|
|
218
|
-
parabolaFromThreePoints,
|
|
219
|
-
pointsToABC,
|
|
220
|
-
parabola,
|
|
221
|
-
absoluteFromTwoPoints,
|
|
222
|
-
pointsToAForAbsolute,
|
|
223
|
-
absolute,
|
|
224
|
-
exponentialFromTwoPoints,
|
|
225
|
-
pointsToABForExponential,
|
|
226
|
-
exponential,
|
|
227
|
-
buildDataPoints,
|
|
228
|
-
};
|