@pie-lib/graphing-utils 3.0.2-next.1 → 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/lib/index.js DELETED
@@ -1,275 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.xPoints = exports.sinY = exports.pointsToAForAbsolute = exports.pointsToABForExponential = exports.pointsToABC = exports.parabolaFromTwoPoints = exports.parabolaFromThreePoints = exports.parabola = exports.getAmplitudeAndFreq = exports.exponentialFromTwoPoints = exports.exponential = exports["default"] = exports.buildDataPoints = exports.absoluteFromTwoPoints = exports.absolute = exports.FREQ_DIVIDER = void 0;
8
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
- var _debug = _interopRequireDefault(require("debug"));
11
- var _lodashEs = require("lodash-es");
12
- var _pointGeometry = _interopRequireDefault(require("@mapbox/point-geometry"));
13
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
14
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
15
- var log = (0, _debug["default"])('pie-lib:graphing:tools:utils');
16
- var FREQ_DIVIDER = exports.FREQ_DIVIDER = 16;
17
- var getAmplitudeAndFreq = exports.getAmplitudeAndFreq = function getAmplitudeAndFreq(root, edge) {
18
- if (!edge) {
19
- return {
20
- freq: 0,
21
- amplitude: 0
22
- };
23
- }
24
- if (root.x == edge.x) {
25
- return {
26
- freq: 0,
27
- amplitude: 0
28
- };
29
- }
30
- var r = new _pointGeometry["default"](root.x, root.y);
31
- var e = new _pointGeometry["default"](edge.x, edge.y);
32
- var d = e.sub(r);
33
- // edge point describes 1/4 of the freq
34
- return {
35
- freq: d.x * 4,
36
- amplitude: d.y
37
- };
38
- };
39
-
40
- /**
41
- * build the x points with root as the anchor.
42
- */
43
- var xPoints = exports.xPoints = function xPoints(root, freq, min, max) {
44
- freq = Math.abs(freq);
45
- log('[xPoints] root:', root, 'freq:', freq, ' min:', min, ' max:', max);
46
- var leftpoints = (0, _lodashEs.rangeRight)(root, min - freq, freq * -1);
47
- var rightpoints = (0, _lodashEs.range)(root + freq, max + freq, freq);
48
- return (0, _lodashEs.concat)(leftpoints, rightpoints);
49
- };
50
-
51
- /**
52
- * Calculate y for x value using amp, freq and shift
53
- * https://www.desmos.com/calculator/f1psfoiiv6
54
- */
55
- var sinY = exports.sinY = function sinY(amplitude, freq, shift) {
56
- return function (x) {
57
- shift = _objectSpread({
58
- phase: 0,
59
- vertical: 0
60
- }, shift);
61
- var TWO_PI = Math.PI * 2;
62
- var num = TWO_PI * (x - shift.phase);
63
- var frac = num / freq;
64
- var sin = Math.sin(frac);
65
- var sinFloat = parseFloat(sin).toFixed(10);
66
- return amplitude * sinFloat + shift.vertical;
67
- };
68
- };
69
-
70
- /**
71
- * You need three points for the quadratic eq, so we mirror the edge point
72
- * https://www.desmos.com/calculator/xhz1xqgpca
73
- */
74
- var parabolaFromTwoPoints = exports.parabolaFromTwoPoints = function parabolaFromTwoPoints(root, edge) {
75
- edge = edge || _objectSpread({}, root);
76
- var mirrorEdge = {
77
- x: root.x - (edge.x - root.x),
78
- y: edge.y
79
- };
80
- return parabolaFromThreePoints(root, edge, mirrorEdge);
81
- };
82
- var parabolaFromThreePoints = exports.parabolaFromThreePoints = function parabolaFromThreePoints(one, two, three) {
83
- log(one, two, three);
84
- var _pointsToABC = pointsToABC(one, two, three),
85
- a = _pointsToABC.a,
86
- b = _pointsToABC.b,
87
- c = _pointsToABC.c;
88
- log(a, b, c);
89
- return parabola(a, b, c);
90
- };
91
- /** derive a,b,c from the 3 points */
92
- var pointsToABC = exports.pointsToABC = function pointsToABC(one, two, three) {
93
- var a1 = Math.pow(one.x, 2) * -1 + Math.pow(two.x, 2);
94
- var b1 = one.x * -1 + two.x;
95
- var d1 = one.y * -1 + two.y;
96
- var a2 = Math.pow(two.x, 2) * -1 + Math.pow(three.x, 2);
97
- var b2 = two.x * -1 + three.x;
98
- var d2 = two.y * -1 + three.y;
99
- var bMult = b2 / b1 * -1;
100
- var a3 = bMult * a1 + a2;
101
- var d3 = bMult * d1 + d2;
102
- var a = d3 / a3;
103
- var b = (d1 - a1 * a) / b1;
104
- var c = one.y - a * Math.pow(one.x, 2) - b * one.x;
105
- return {
106
- a: a,
107
- b: b,
108
- c: c
109
- };
110
- };
111
-
112
- /**
113
- * y=ax^2+bx+c
114
- */
115
- var parabola = exports.parabola = function parabola(a, b, c) {
116
- return function (x) {
117
- return a * Math.pow(x, 2) + b * x + c;
118
- };
119
- };
120
-
121
- /**
122
- * Absolute value from two points Root (Vertex) the point where the two rays intersect and edge One point on a ray.
123
- * @param root 1st point selected on graph
124
- * @param edge 2nd point selected on graph
125
- * https://www.desmos.com/calculator/rarxiatpip
126
- */
127
- var absoluteFromTwoPoints = exports.absoluteFromTwoPoints = function absoluteFromTwoPoints(root, edge) {
128
- edge = edge || _objectSpread({}, root);
129
- var a = pointsToAForAbsolute(root, edge);
130
- return absolute(a, root.x, root.y);
131
- };
132
-
133
- /*
134
- * Finds value of a in y=a*abs(x-h)+k function
135
- * @param one 1st point
136
- * @param two 2nd point
137
- * */
138
- var pointsToAForAbsolute = exports.pointsToAForAbsolute = function pointsToAForAbsolute(one, two) {
139
- var y1 = two.y - one.y;
140
- var x1 = two.x - one.x;
141
- var a = y1 / x1;
142
- if (two.x < one.x) {
143
- a = a * -1;
144
- }
145
- return a;
146
- };
147
-
148
- /**
149
- * y=a*abs(x-h)+k
150
- */
151
- var absolute = exports.absolute = function absolute(a, h, k) {
152
- return function (x) {
153
- return a * Math.abs(x - h) + k;
154
- };
155
- };
156
-
157
- /**
158
- * Exponential from two points on exponential graph.
159
- * @param root 1st point selected on graph
160
- * @param edge 2nd point selected on graph
161
- * https://www.desmos.com/calculator/3fisjexbvp
162
- */
163
- var exponentialFromTwoPoints = exports.exponentialFromTwoPoints = function exponentialFromTwoPoints(root, edge) {
164
- edge = edge || _objectSpread({}, root);
165
- var _pointsToABForExponen = pointsToABForExponential(root, edge),
166
- a = _pointsToABForExponen.a,
167
- b = _pointsToABForExponen.b;
168
- return exponential(a, b);
169
- };
170
-
171
- /*
172
- * Finds value of a and b in y=a*(b)^x function
173
- * @param one 1st point
174
- * @param two 2nd point
175
- * */
176
- var pointsToABForExponential = exports.pointsToABForExponential = function pointsToABForExponential(one, two) {
177
- var p = one.y / two.y;
178
- var r = 1 / (one.x - two.x);
179
- var b = Math.pow(p, r);
180
- var a = one.y / Math.pow(b, one.x);
181
- return {
182
- a: a,
183
- b: b
184
- };
185
- };
186
-
187
- /**
188
- * y=a*(b)^x
189
- */
190
- var exponential = exports.exponential = function exponential(a, b) {
191
- return function (x) {
192
- return a * Math.pow(b, x);
193
- };
194
- };
195
-
196
- /**
197
- * Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max
198
- * @param {*} domain
199
- * @param {*} range
200
- * @param {*} root
201
- * @param {*} edge
202
- * @param {*} yFn
203
- * @param {*} excludeOutsidePoints
204
- */
205
- var buildDataPoints = exports.buildDataPoints = function buildDataPoints(domain, range, root, edge, yFn) {
206
- var excludeOutsidePoints = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
207
- log('[buildDataPoints] domain:', domain, 'range:', range, 'root:', root, 'edge:', edge);
208
- domain = _objectSpread({
209
- min: 0,
210
- max: 0,
211
- step: 1
212
- }, domain);
213
- range = _objectSpread({
214
- min: 0,
215
- max: 0,
216
- step: 1
217
- }, range);
218
- edge = edge ? edge : _objectSpread({}, root);
219
- var minX = Math.min(root.x, edge.x);
220
- var xPts = xPoints(minX, domain.step, domain.min - domain.step, domain.max + domain.step);
221
- log('[buildDataPoints]:xPts:', xPts);
222
- var startIndex = -1;
223
- var endIndex = -1;
224
-
225
- // generate points based on the yFn
226
- var points = xPts.map(function (v) {
227
- return new _pointGeometry["default"](v, yFn(v));
228
- });
229
- if (!excludeOutsidePoints) {
230
- return points;
231
- }
232
-
233
- // exclude the points with y outside range min and max
234
- var filteredPoints = points.reduce(function (acc, val, index) {
235
- if (val.y >= range.min - range.step && val.y <= range.max + range.step) {
236
- if (startIndex === -1) {
237
- startIndex = index;
238
- }
239
- if (endIndex < index) {
240
- endIndex = index;
241
- }
242
- return [].concat((0, _toConsumableArray2["default"])(acc), [val]);
243
- }
244
- return acc;
245
- }, []);
246
-
247
- // add the first value outside range min and max smaller than range.min if exists
248
- if (startIndex - 1 >= 0) {
249
- filteredPoints.unshift(points[startIndex - 1]);
250
- }
251
-
252
- // add the first value outside range min and max greater than range.min if exists
253
- if (endIndex + 1 <= points.length - 1) {
254
- filteredPoints.push(points[endIndex + 1]);
255
- }
256
- return points;
257
- };
258
- var _default = exports["default"] = {
259
- FREQ_DIVIDER: FREQ_DIVIDER,
260
- getAmplitudeAndFreq: getAmplitudeAndFreq,
261
- xPoints: xPoints,
262
- sinY: sinY,
263
- parabolaFromTwoPoints: parabolaFromTwoPoints,
264
- parabolaFromThreePoints: parabolaFromThreePoints,
265
- pointsToABC: pointsToABC,
266
- parabola: parabola,
267
- absoluteFromTwoPoints: absoluteFromTwoPoints,
268
- pointsToAForAbsolute: pointsToAForAbsolute,
269
- absolute: absolute,
270
- exponentialFromTwoPoints: exponentialFromTwoPoints,
271
- pointsToABForExponential: pointsToABForExponential,
272
- exponential: exponential,
273
- buildDataPoints: buildDataPoints
274
- };
275
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","names":["_debug","_interopRequireDefault","require","_lodashEs","_pointGeometry","ownKeys","e","r","t","Object","keys","getOwnPropertySymbols","o","filter","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","arguments","length","forEach","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","log","debug","FREQ_DIVIDER","exports","getAmplitudeAndFreq","root","edge","freq","amplitude","x","Point","y","d","sub","xPoints","min","max","Math","abs","leftpoints","rangeRight","rightpoints","range","concat","sinY","shift","phase","vertical","TWO_PI","PI","num","frac","sin","sinFloat","parseFloat","toFixed","parabolaFromTwoPoints","mirrorEdge","parabolaFromThreePoints","one","two","three","_pointsToABC","pointsToABC","a","b","c","parabola","a1","pow","b1","d1","a2","b2","d2","bMult","a3","d3","absoluteFromTwoPoints","pointsToAForAbsolute","absolute","y1","x1","h","k","exponentialFromTwoPoints","_pointsToABForExponen","pointsToABForExponential","exponential","p","buildDataPoints","domain","yFn","excludeOutsidePoints","undefined","step","minX","xPts","startIndex","endIndex","points","map","v","filteredPoints","reduce","acc","val","index","_toConsumableArray2","unshift","_default"],"sources":["../src/index.js"],"sourcesContent":["import debug from 'debug';\nimport { concat, range, rangeRight } from 'lodash-es';\nimport Point from '@mapbox/point-geometry';\n\nconst log = debug('pie-lib:graphing:tools:utils');\n\nexport const FREQ_DIVIDER = 16;\n\nexport const getAmplitudeAndFreq = (root, edge) => {\n if (!edge) {\n return { freq: 0, amplitude: 0 };\n }\n\n if (root.x == edge.x) {\n return { freq: 0, amplitude: 0 };\n }\n\n const r = new Point(root.x, root.y);\n const e = new Point(edge.x, edge.y);\n const d = e.sub(r);\n // edge point describes 1/4 of the freq\n return { freq: d.x * 4, amplitude: d.y };\n};\n\n/**\n * build the x points with root as the anchor.\n */\nexport const xPoints = (root, freq, min, max) => {\n freq = Math.abs(freq);\n log('[xPoints] root:', root, 'freq:', freq, ' min:', min, ' max:', max);\n const leftpoints = rangeRight(root, min - freq, freq * -1);\n const rightpoints = range(root + freq, max + freq, freq);\n\n return concat(leftpoints, rightpoints);\n};\n\n/**\n * Calculate y for x value using amp, freq and shift\n * https://www.desmos.com/calculator/f1psfoiiv6\n */\nexport const sinY = (amplitude, freq, shift) => (x) => {\n shift = { phase: 0, vertical: 0, ...shift };\n const TWO_PI = Math.PI * 2;\n const num = TWO_PI * (x - shift.phase);\n const frac = num / freq;\n const sin = Math.sin(frac);\n const sinFloat = parseFloat(sin).toFixed(10);\n return amplitude * sinFloat + shift.vertical;\n};\n\n/**\n * You need three points for the quadratic eq, so we mirror the edge point\n * https://www.desmos.com/calculator/xhz1xqgpca\n */\nexport const parabolaFromTwoPoints = (root, edge) => {\n edge = edge || { ...root };\n const mirrorEdge = { x: root.x - (edge.x - root.x), y: edge.y };\n return parabolaFromThreePoints(root, edge, mirrorEdge);\n};\n\nexport const parabolaFromThreePoints = (one, two, three) => {\n log(one, two, three);\n const { a, b, c } = pointsToABC(one, two, three);\n log(a, b, c);\n return parabola(a, b, c);\n};\n/** derive a,b,c from the 3 points */\nexport const pointsToABC = (one, two, three) => {\n const a1 = Math.pow(one.x, 2) * -1 + Math.pow(two.x, 2);\n const b1 = one.x * -1 + two.x;\n const d1 = one.y * -1 + two.y;\n const a2 = Math.pow(two.x, 2) * -1 + Math.pow(three.x, 2);\n const b2 = two.x * -1 + three.x;\n const d2 = two.y * -1 + three.y;\n const bMult = (b2 / b1) * -1;\n const a3 = bMult * a1 + a2;\n const d3 = bMult * d1 + d2;\n const a = d3 / a3;\n const b = (d1 - a1 * a) / b1;\n const c = one.y - a * Math.pow(one.x, 2) - b * one.x;\n return { a, b, c };\n};\n\n/**\n * y=ax^2+bx+c\n */\nexport const parabola = (a, b, c) => (x) => a * Math.pow(x, 2) + b * x + c;\n\n/**\n * Absolute value from two points Root (Vertex) the point where the two rays intersect and edge One point on a ray.\n * @param root 1st point selected on graph\n * @param edge 2nd point selected on graph\n * https://www.desmos.com/calculator/rarxiatpip\n */\nexport const absoluteFromTwoPoints = (root, edge) => {\n edge = edge || { ...root };\n const a = pointsToAForAbsolute(root, edge);\n return absolute(a, root.x, root.y);\n};\n\n/*\n * Finds value of a in y=a*abs(x-h)+k function\n * @param one 1st point\n * @param two 2nd point\n * */\nexport const pointsToAForAbsolute = (one, two) => {\n const y1 = two.y - one.y;\n const x1 = two.x - one.x;\n let a = y1 / x1;\n if (two.x < one.x) {\n a = a * -1;\n }\n return a;\n};\n\n/**\n * y=a*abs(x-h)+k\n */\nexport const absolute = (a, h, k) => (x) => a * Math.abs(x - h) + k;\n\n/**\n * Exponential from two points on exponential graph.\n * @param root 1st point selected on graph\n * @param edge 2nd point selected on graph\n * https://www.desmos.com/calculator/3fisjexbvp\n */\nexport const exponentialFromTwoPoints = (root, edge) => {\n edge = edge || { ...root };\n const { a, b } = pointsToABForExponential(root, edge);\n return exponential(a, b);\n};\n\n/*\n * Finds value of a and b in y=a*(b)^x function\n * @param one 1st point\n * @param two 2nd point\n * */\nexport const pointsToABForExponential = (one, two) => {\n const p = one.y / two.y;\n const r = 1 / (one.x - two.x);\n const b = Math.pow(p, r);\n const a = one.y / Math.pow(b, one.x);\n return { a, b };\n};\n\n/**\n * y=a*(b)^x\n */\nexport const exponential = (a, b) => (x) => a * Math.pow(b, x);\n\n/**\n * Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max\n * @param {*} domain\n * @param {*} range\n * @param {*} root\n * @param {*} edge\n * @param {*} yFn\n * @param {*} excludeOutsidePoints\n */\nexport const buildDataPoints = (domain, range, root, edge, yFn, excludeOutsidePoints = false) => {\n log('[buildDataPoints] domain:', domain, 'range:', range, 'root:', root, 'edge:', edge);\n\n domain = { min: 0, max: 0, step: 1, ...domain };\n range = { min: 0, max: 0, step: 1, ...range };\n edge = edge ? edge : { ...root };\n\n const minX = Math.min(root.x, edge.x);\n const xPts = xPoints(minX, domain.step, domain.min - domain.step, domain.max + domain.step);\n\n log('[buildDataPoints]:xPts:', xPts);\n\n let startIndex = -1;\n let endIndex = -1;\n\n // generate points based on the yFn\n const points = xPts.map((v) => new Point(v, yFn(v)));\n\n if (!excludeOutsidePoints) {\n return points;\n }\n\n // exclude the points with y outside range min and max\n const filteredPoints = points.reduce((acc, val, index) => {\n if (val.y >= range.min - range.step && val.y <= range.max + range.step) {\n if (startIndex === -1) {\n startIndex = index;\n }\n\n if (endIndex < index) {\n endIndex = index;\n }\n\n return [...acc, val];\n }\n\n return acc;\n }, []);\n\n // add the first value outside range min and max smaller than range.min if exists\n if (startIndex - 1 >= 0) {\n filteredPoints.unshift(points[startIndex - 1]);\n }\n\n // add the first value outside range min and max greater than range.min if exists\n if (endIndex + 1 <= points.length - 1) {\n filteredPoints.push(points[endIndex + 1]);\n }\n\n return points;\n};\n\nexport default {\n FREQ_DIVIDER,\n getAmplitudeAndFreq,\n xPoints,\n sinY,\n parabolaFromTwoPoints,\n parabolaFromThreePoints,\n pointsToABC,\n parabola,\n absoluteFromTwoPoints,\n pointsToAForAbsolute,\n absolute,\n exponentialFromTwoPoints,\n pointsToABForExponential,\n exponential,\n buildDataPoints,\n};\n"],"mappings":";;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,cAAA,GAAAH,sBAAA,CAAAC,OAAA;AAA2C,SAAAG,QAAAC,CAAA,EAAAC,CAAA,QAAAC,CAAA,GAAAC,MAAA,CAAAC,IAAA,CAAAJ,CAAA,OAAAG,MAAA,CAAAE,qBAAA,QAAAC,CAAA,GAAAH,MAAA,CAAAE,qBAAA,CAAAL,CAAA,GAAAC,CAAA,KAAAK,CAAA,GAAAA,CAAA,CAAAC,MAAA,WAAAN,CAAA,WAAAE,MAAA,CAAAK,wBAAA,CAAAR,CAAA,EAAAC,CAAA,EAAAQ,UAAA,OAAAP,CAAA,CAAAQ,IAAA,C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AG,CAAC,IAAI,CAAC,EAAE;IACvBK,cAAc,CAACM,OAAO,CAACT,MAAM,CAACF,UAAU,GAAG,CAAC,CAAC,CAAC;EAChD;;EAEA;EACA,IAAIC,QAAQ,GAAG,CAAC,IAAIC,MAAM,CAACtF,MAAM,GAAG,CAAC,EAAE;IACrCyF,cAAc,CAAC7F,IAAI,CAAC0F,MAAM,CAACD,QAAQ,GAAG,CAAC,CAAC,CAAC;EAC3C;EAEA,OAAOC,MAAM;AACf,CAAC;AAAC,IAAAU,QAAA,GAAAvF,OAAA,cAEa;EACbD,YAAY,EAAZA,YAAY;EACZE,mBAAmB,EAAnBA,mBAAmB;EACnBU,OAAO,EAAPA,OAAO;EACPU,IAAI,EAAJA,IAAI;EACJY,qBAAqB,EAArBA,qBAAqB;EACrBE,uBAAuB,EAAvBA,uBAAuB;EACvBK,WAAW,EAAXA,WAAW;EACXI,QAAQ,EAARA,QAAQ;EACRW,qBAAqB,EAArBA,qBAAqB;EACrBC,oBAAoB,EAApBA,oBAAoB;EACpBC,QAAQ,EAARA,QAAQ;EACRK,wBAAwB,EAAxBA,wBAAwB;EACxBE,wBAAwB,EAAxBA,wBAAwB;EACxBC,WAAW,EAAXA,WAAW;EACXE,eAAe,EAAfA;AACF,CAAC","ignoreList":[]}
@@ -1,299 +0,0 @@
1
- import {
2
- absolute,
3
- absoluteFromTwoPoints,
4
- buildDataPoints,
5
- exponential,
6
- exponentialFromTwoPoints,
7
- getAmplitudeAndFreq,
8
- parabola,
9
- parabolaFromThreePoints,
10
- parabolaFromTwoPoints,
11
- pointsToABC,
12
- pointsToABForExponential,
13
- pointsToAForAbsolute,
14
- sinY,
15
- xPoints,
16
- } from '../index';
17
-
18
- import { range } from 'lodash-es';
19
-
20
- const xy = (x, y) => ({ x, y });
21
-
22
- describe('utils', () => {
23
- describe('getAmplitudeAndFreq', () => {
24
- it('calculates amplitude and frequency from root and edge points', () => {
25
- const root = { x: 0, y: 0 };
26
- const edge = { x: 1, y: 2 };
27
- const result = getAmplitudeAndFreq(root, edge);
28
-
29
- expect(result.freq).toBe(4);
30
- expect(result.amplitude).toBe(2);
31
- });
32
- });
33
-
34
- describe('sinY', () => {
35
- const assertSin = (amp, freq, shift) => (input, expected) => {
36
- const p = shift ? shift.phase : 0;
37
- const v = shift ? shift.vertical : 0;
38
- it(` sin of ${input} (>${p} ^${v}) = ${expected}`, () => {
39
- const fn = sinY(amp, freq, shift);
40
- const result = Array.isArray(input) ? input.map((x) => fn(x)) : fn(input);
41
- expect(result).toEqual(expected);
42
- });
43
- };
44
- const shift = (phase, vertical) => ({ phase, vertical });
45
- const rng = (start, end, step) => range(start, end + step, step);
46
-
47
- describe('amp: 1, freq: 1', () => {
48
- assertSin(1, 1, shift(0, 0))(0, 0);
49
- assertSin(1, 1, shift(0, 0))(0.25, 1);
50
- assertSin(1, 1, shift(0, 0))(0.5, 0);
51
- assertSin(1, 1, shift(0, 0))(0.75, -1);
52
- assertSin(1, 1, shift(0, 0))(rng(0, 1, 0.25), [0, 1, 0, -1, 0]);
53
- });
54
-
55
- describe('amp: 1, freq: 4', () => {
56
- assertSin(1, 4, shift(0, 0))(rng(-4, 4, 1), [0, 1, 0, -1, 0, 1, 0, -1, 0]);
57
- assertSin(1, 4, shift(0, 0))(rng(-1, 1, 1), [-1, 0, 1]);
58
- assertSin(1, 4, shift(1, 0))(rng(-1, 1, 1), [0, -1, 0]);
59
- assertSin(1, 4, shift(-1, 0))(rng(-1, 1, 1), [0, 1, 0]);
60
- assertSin(1, 4, shift(0, 1))(rng(-1, 1, 1), [0, 1, 2]);
61
- });
62
- });
63
-
64
- describe('buildDataPoints', () => {
65
- it('generates points', () => {
66
- const result = buildDataPoints(
67
- { min: -1, max: 1, step: 1 },
68
- { min: -1, max: 1, step: 1 },
69
- { x: 0, y: 0 },
70
- { x: 0, y: 0 },
71
- (x) => x,
72
- );
73
- expect(result.map((p) => p.x)).toEqual([-2, -1, 0, 1, 2]);
74
- });
75
-
76
- const assertParabola = (
77
- domain,
78
- range,
79
- root,
80
- edge,
81
- mirror,
82
- existingPoint1,
83
- existingPoint2,
84
- nonExistingPointOutsideRange,
85
- ) => {
86
- it('generates points for parabola', () => {
87
- const result = buildDataPoints(domain, range, root, edge, parabolaFromTwoPoints(root, edge));
88
-
89
- expect(result).toContainEqual(root);
90
- expect(result).toContainEqual(edge);
91
- expect(result).toContainEqual(mirror);
92
- expect(result).toContainEqual(existingPoint1);
93
- expect(result).toContainEqual(existingPoint2);
94
- expect(result).not.toContainEqual(nonExistingPointOutsideRange);
95
- });
96
- };
97
-
98
- // a * x^2 + b * x + c
99
-
100
- // a = 8, b = 4, c = 2
101
- assertParabola(
102
- { min: -1, max: 1, step: 0.25 },
103
- { min: -1, max: -1, step: 1 },
104
- { x: -0.25, y: 1.5 },
105
- { x: 0.25, y: 3.5 },
106
- { x: -0.75, y: 3.5 },
107
- { x: -1, y: 6 },
108
- { x: 1, y: 14 },
109
- { x: 1.25, y: -19.5 },
110
- );
111
-
112
- // a = -8, b = -4, c = -2
113
- assertParabola(
114
- { min: -1, max: 1, step: 0.25 },
115
- { min: -1, max: -1, step: 1 },
116
- { x: -0.25, y: -1.5 },
117
- { x: 0.25, y: -3.5 },
118
- { x: -0.75, y: -3.5 },
119
- { x: -1, y: -6 },
120
- { x: 1, y: -14 },
121
- { x: 1.25, y: 19.5 },
122
- );
123
-
124
- // a = 10, b = 0, c = -2
125
- assertParabola(
126
- { min: -1, max: 1, step: 0.25 },
127
- { min: -1, max: -1, step: 1 },
128
- { x: 0, y: -2 },
129
- { x: 0.5, y: 0.5 },
130
- { x: -0.5, y: 0.5 },
131
- { x: -1, y: 8 },
132
- { x: 1, y: 8 },
133
- { x: 3, y: 88 },
134
- );
135
-
136
- // a = 10, b = -10, c = 0
137
- assertParabola(
138
- { min: -1, max: 1, step: 0.25 },
139
- { min: -1, max: -1, step: 1 },
140
- { x: 0.5, y: -2.5 },
141
- { x: 0, y: 0 },
142
- { x: 1, y: 0 },
143
- { x: -1, y: 20 },
144
- { x: 1.25, y: 3.125 },
145
- { x: 3, y: 60 },
146
- );
147
-
148
- // a = 10, b = -10, c = 0
149
- assertParabola(
150
- { min: -1, max: 1, step: 0.5 },
151
- { min: -1, max: -1, step: 1 },
152
- { x: 0.5, y: -2.5 },
153
- { x: 0, y: 0 },
154
- { x: 1, y: 0 },
155
- { x: -1, y: 20 },
156
- { x: 1.5, y: 7.5 },
157
- { x: 3, y: 60 },
158
- );
159
-
160
- // a = -4, b = 4, c = 0
161
- assertParabola(
162
- { min: -1, max: 5, step: 0.25 },
163
- { min: -1, max: -1, step: 1 },
164
- { x: 0.5, y: 1 },
165
- { x: 0, y: 0 },
166
- { x: 1, y: 0 },
167
- { x: -0.5, y: -3 },
168
- { x: 1.5, y: -3 },
169
- { x: 3, y: 60 },
170
- );
171
- });
172
-
173
- describe('pointsToABC', () => {
174
- const assertPointsToABC = (one, two, three, a, b, c) => {
175
- it(`${one}, ${two}, ${three} => ${a}, ${b}, ${c}`, () => {
176
- const result = pointsToABC(one, two, three);
177
- expect(result.a).toBeCloseTo(a);
178
- expect(result.b).toBeCloseTo(b);
179
- expect(result.c).toBeCloseTo(c);
180
- });
181
- };
182
-
183
- assertPointsToABC(xy(0, 0), xy(1, 1), xy(-1, 1), 1, 0, 0);
184
- assertPointsToABC(xy(0, 0), xy(2, 1), xy(-2, 1), 0.25, 0, 0);
185
- assertPointsToABC(xy(1, 0), xy(2, 1), xy(0, 1), 1, -2, 1);
186
- assertPointsToABC(xy(1, 0), xy(4, 1), xy(-2, 1), 0.11111, -0.22222, 0.11111);
187
- assertPointsToABC(xy(0, 0), xy(1, 2), xy(-1, 2), 2, 0, 0);
188
- });
189
-
190
- describe('parabolaFromTwoPoints', () => {
191
- describe('0,0 -> 1,2', () => {
192
- const yVal = parabolaFromTwoPoints(xy(0, 0), xy(1, 1));
193
- it('for x = 0, returns 0', () => expect(yVal(0)).toEqual(0));
194
- it('for x = 1, returns 1', () => expect(yVal(1)).toEqual(1));
195
- });
196
- describe('0,0 -> 1,2', () => {
197
- const yVal = parabolaFromTwoPoints(xy(0, 0), xy(1, 2));
198
- it('for x = 0, returns 0', () => expect(yVal(0)).toEqual(0));
199
- it('for x = 1, returns 2', () => expect(yVal(1)).toEqual(2));
200
- });
201
- });
202
-
203
- describe('parabolaFromThreePoints', () => {
204
- it('generates parabola function from three points', () => {
205
- const one = xy(0, 0);
206
- const two = xy(1, 1);
207
- const three = xy(-1, 1);
208
- const fn = parabolaFromThreePoints(one, two, three);
209
-
210
- expect(fn(0)).toBeCloseTo(0);
211
- expect(fn(1)).toBeCloseTo(1);
212
- expect(fn(-1)).toBeCloseTo(1);
213
- });
214
- });
215
-
216
- describe('parabola', () => {
217
- it('works', () => {
218
- const fn = parabola(0.22222, -0.444444, 0.22222);
219
- expect(fn(0)).toBeCloseTo(0.222222);
220
- });
221
- });
222
-
223
- describe('pointsToAForAbsolute', () => {
224
- it('calculates a coefficient for absolute value function', () => {
225
- const one = xy(0, 0);
226
- const two = xy(2, 4);
227
- const result = pointsToAForAbsolute(one, two);
228
-
229
- expect(result).toBe(2);
230
- });
231
- });
232
-
233
- describe('absolute', () => {
234
- it('generates absolute value function y=a*abs(x-h)+k', () => {
235
- const fn = absolute(2, 1, 3);
236
-
237
- expect(fn(1)).toBe(3); // vertex
238
- expect(fn(2)).toBe(5); // right side
239
- expect(fn(0)).toBe(5); // left side
240
- });
241
- });
242
-
243
- describe('absoluteFromTwoPoints', () => {
244
- it('generates absolute value function from two points', () => {
245
- const root = xy(0, 0);
246
- const edge = xy(1, 2);
247
- const fn = absoluteFromTwoPoints(root, edge);
248
-
249
- expect(fn(0)).toBe(0);
250
- expect(fn(1)).toBe(2);
251
- });
252
- });
253
-
254
- describe('pointsToABForExponential', () => {
255
- it('calculates a and b coefficients for exponential function', () => {
256
- const one = xy(0, 2);
257
- const two = xy(1, 4);
258
- const result = pointsToABForExponential(one, two);
259
-
260
- expect(result.a).toBeCloseTo(2);
261
- expect(result.b).toBeCloseTo(2);
262
- });
263
- });
264
-
265
- describe('exponential', () => {
266
- it('generates exponential function y=a*(b)^x', () => {
267
- const fn = exponential(2, 3);
268
-
269
- expect(fn(0)).toBe(2);
270
- expect(fn(1)).toBe(6);
271
- expect(fn(2)).toBe(18);
272
- });
273
- });
274
-
275
- describe('exponentialFromTwoPoints', () => {
276
- it('generates exponential function from two points', () => {
277
- const root = xy(0, 2);
278
- const edge = xy(1, 6);
279
- const fn = exponentialFromTwoPoints(root, edge);
280
-
281
- expect(fn(0)).toBeCloseTo(2);
282
- expect(fn(1)).toBeCloseTo(6);
283
- });
284
- });
285
-
286
- describe('xPoints', () => {
287
- const assertXPoints = (root, freq, min, max, expected) => {
288
- it(`root: ${root}, freq: ${freq}, domain: ${min}<->${max} => ${expected}`, () => {
289
- const result = xPoints(root, freq, min, max);
290
- expect(result).toEqual(expected);
291
- });
292
- };
293
- assertXPoints(0, 2, -5, 5, [-6, -4, -2, 0, 2, 4, 6]);
294
- assertXPoints(2, 2, -5, 5, [-6, -4, -2, 0, 2, 4, 6]);
295
- assertXPoints(3, 2, -5, 5, [-5, -3, -1, 1, 3, 5]);
296
- assertXPoints(3, 2, 0, 10, [-1, 1, 3, 5, 7, 9, 11]);
297
- assertXPoints(-2, 2, -10, -1, [-10, -8, -6, -4, -2, 0]);
298
- });
299
- });