@pie-lib/graphing-utils 1.1.33-next.0 → 1.2.1-beta.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/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [1.1.32](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.30...@pie-lib/graphing-utils@1.1.32) (2023-01-31)
6
+ ## [1.2.1-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.33...@pie-lib/graphing-utils@1.2.1-beta.0) (2025-07-20)
7
7
 
8
8
  **Note:** Version bump only for package @pie-lib/graphing-utils
9
9
 
@@ -11,7 +11,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
11
11
 
12
12
 
13
13
 
14
- ## [1.1.30](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.29...@pie-lib/graphing-utils@1.1.30) (2023-01-31)
14
+ # [1.2.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.33...@pie-lib/graphing-utils@1.2.0-beta.0) (2025-07-20)
15
15
 
16
16
  **Note:** Version bump only for package @pie-lib/graphing-utils
17
17
 
@@ -19,54 +19,50 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
19
19
 
20
20
 
21
21
 
22
- ## [1.1.29](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.28...@pie-lib/graphing-utils@1.1.29) (2023-01-31)
22
+ # [1.3.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.33...@pie-lib/graphing-utils@1.3.0-beta.0) (2025-07-15)
23
23
 
24
24
  **Note:** Version bump only for package @pie-lib/graphing-utils
25
25
 
26
+ # [1.2.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.33...@pie-lib/graphing-utils@1.2.0-beta.0) (2025-07-15)
26
27
 
28
+ **Note:** Version bump only for package @pie-lib/graphing-utils
27
29
 
28
-
29
-
30
- ## [1.1.28](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.27...@pie-lib/graphing-utils@1.1.28) (2023-01-31)
30
+ ## [1.1.33](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.32...@pie-lib/graphing-utils@1.1.33) (2023-06-24)
31
31
 
32
32
  **Note:** Version bump only for package @pie-lib/graphing-utils
33
33
 
34
+ ## [1.1.32](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.30...@pie-lib/graphing-utils@1.1.32) (2023-01-31)
34
35
 
36
+ **Note:** Version bump only for package @pie-lib/graphing-utils
35
37
 
38
+ ## [1.1.30](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.29...@pie-lib/graphing-utils@1.1.30) (2023-01-31)
36
39
 
40
+ **Note:** Version bump only for package @pie-lib/graphing-utils
37
41
 
38
- ## [1.1.27](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.25...@pie-lib/graphing-utils@1.1.27) (2023-01-31)
42
+ ## [1.1.29](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.28...@pie-lib/graphing-utils@1.1.29) (2023-01-31)
39
43
 
40
44
  **Note:** Version bump only for package @pie-lib/graphing-utils
41
45
 
46
+ ## [1.1.28](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.27...@pie-lib/graphing-utils@1.1.28) (2023-01-31)
42
47
 
48
+ **Note:** Version bump only for package @pie-lib/graphing-utils
43
49
 
50
+ ## [1.1.27](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.25...@pie-lib/graphing-utils@1.1.27) (2023-01-31)
44
51
 
52
+ **Note:** Version bump only for package @pie-lib/graphing-utils
45
53
 
46
54
  ## [1.1.25](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.23...@pie-lib/graphing-utils@1.1.25) (2023-01-31)
47
55
 
48
56
  **Note:** Version bump only for package @pie-lib/graphing-utils
49
57
 
50
-
51
-
52
-
53
-
54
58
  ## [1.1.23](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.21...@pie-lib/graphing-utils@1.1.23) (2023-01-31)
55
59
 
56
60
  **Note:** Version bump only for package @pie-lib/graphing-utils
57
61
 
58
-
59
-
60
-
61
-
62
62
  ## [1.1.21](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.20...@pie-lib/graphing-utils@1.1.21) (2022-11-23)
63
63
 
64
64
  **Note:** Version bump only for package @pie-lib/graphing-utils
65
65
 
66
-
67
-
68
-
69
-
70
66
  ## [1.1.20](https://github.com/pie-framework/pie-lib/compare/@pie-lib/graphing-utils@1.1.18...@pie-lib/graphing-utils@1.1.20) (2021-06-25)
71
67
 
72
68
  **Note:** Version bump only for package @pie-lib/graphing-utils
@@ -0,0 +1 @@
1
+ []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/graphing-utils",
3
- "version": "1.1.33-next.0+2f8c6198",
3
+ "version": "1.2.1-beta.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "module": "src/index.js",
@@ -14,6 +14,6 @@
14
14
  "debug": "^4.1.1",
15
15
  "lodash": "^4.17.11"
16
16
  },
17
- "gitHead": "2f8c619845b4aa9a1c862b2ac549a975fe0bd13f",
17
+ "gitHead": "e2aa3ddac60f49bcb8c2562370f496323642f453",
18
18
  "scripts": {}
19
19
  }
@@ -0,0 +1,197 @@
1
+ import { xPoints, sinY, buildDataPoints, pointsToABC, parabola, parabolaFromTwoPoints } from '../index';
2
+
3
+ import _ from 'lodash';
4
+
5
+ const xy = (x, y) => ({ x, y });
6
+
7
+ describe('utils', () => {
8
+ describe('xPoints', () => {
9
+ const assertXPoints = (root, freq, min, max, expected) => {
10
+ it(`root: ${root}, freq: ${freq}, domain: ${min}<->${max} => ${expected}`, () => {
11
+ const result = xPoints(root, freq, min, max);
12
+ expect(result).toEqual(expected);
13
+ });
14
+ };
15
+ assertXPoints(0, 2, -5, 5, [-6, -4, -2, 0, 2, 4, 6]);
16
+ assertXPoints(2, 2, -5, 5, [-6, -4, -2, 0, 2, 4, 6]);
17
+ assertXPoints(3, 2, -5, 5, [-5, -3, -1, 1, 3, 5]);
18
+ assertXPoints(3, 2, 0, 10, [-1, 1, 3, 5, 7, 9, 11]);
19
+ assertXPoints(-2, 2, -10, -1, [-10, -8, -6, -4, -2, 0]);
20
+ });
21
+
22
+ describe('sinY', () => {
23
+ const assertSin = (amp, freq, shift) => (input, expected) => {
24
+ const p = shift ? shift.phase : 0;
25
+ const v = shift ? shift.vertical : 0;
26
+ it(` sin of ${input} (>${p} ^${v}) = ${expected}`, () => {
27
+ const fn = sinY(amp, freq, shift);
28
+ const result = Array.isArray(input) ? input.map((x) => fn(x)) : fn(input);
29
+ expect(result).toEqual(expected);
30
+ });
31
+ };
32
+ const shift = (phase, vertical) => ({ phase, vertical });
33
+ const rng = (start, end, step) => _.range(start, end + step, step);
34
+
35
+ describe('amp: 1, freq: 1', () => {
36
+ assertSin(1, 1, shift(0, 0))(0, 0);
37
+ assertSin(1, 1, shift(0, 0))(0.25, 1);
38
+ assertSin(1, 1, shift(0, 0))(0.5, 0);
39
+ assertSin(1, 1, shift(0, 0))(0.75, -1);
40
+ assertSin(1, 1, shift(0, 0))(rng(0, 1, 0.25), [0, 1, 0, -1, 0]);
41
+ });
42
+
43
+ describe('amp: 1, freq: 4', () => {
44
+ assertSin(1, 4, shift(0, 0))(rng(-4, 4, 1), [0, 1, 0, -1, 0, 1, 0, -1, 0]);
45
+ assertSin(1, 4, shift(0, 0))(rng(-1, 1, 1), [-1, 0, 1]);
46
+ assertSin(1, 4, shift(1, 0))(rng(-1, 1, 1), [0, -1, 0]);
47
+ assertSin(1, 4, shift(-1, 0))(rng(-1, 1, 1), [0, 1, 0]);
48
+ assertSin(1, 4, shift(0, 1))(rng(-1, 1, 1), [0, 1, 2]);
49
+ });
50
+ });
51
+
52
+ describe('buildDataPoints', () => {
53
+ it('generates points', () => {
54
+ const result = buildDataPoints(
55
+ { min: -1, max: 1, step: 1 },
56
+ { min: -1, max: 1, step: 1 },
57
+ { x: 0, y: 0 },
58
+ { x: 0, y: 0 },
59
+ (x) => x,
60
+ );
61
+ expect(result.map((p) => p.x)).toEqual([-2, -1, 0, 1, 2]);
62
+ });
63
+
64
+ const assertParabola = (
65
+ domain,
66
+ range,
67
+ root,
68
+ edge,
69
+ mirror,
70
+ existingPoint1,
71
+ existingPoint2,
72
+ nonExistingPointOutsideRange,
73
+ ) => {
74
+ it('generates points for parabola', () => {
75
+ const result = buildDataPoints(domain, range, root, edge, parabolaFromTwoPoints(root, edge));
76
+
77
+ expect(result).toContainEqual(root);
78
+ expect(result).toContainEqual(edge);
79
+ expect(result).toContainEqual(mirror);
80
+ expect(result).toContainEqual(existingPoint1);
81
+ expect(result).toContainEqual(existingPoint2);
82
+ expect(result).not.toContainEqual(nonExistingPointOutsideRange);
83
+ });
84
+ };
85
+
86
+ // a * x^2 + b * x + c
87
+
88
+ // a = 8, b = 4, c = 2
89
+ assertParabola(
90
+ { min: -1, max: 1, step: 0.25 },
91
+ { min: -1, max: -1, step: 1 },
92
+ { x: -0.25, y: 1.5 },
93
+ { x: 0.25, y: 3.5 },
94
+ { x: -0.75, y: 3.5 },
95
+ { x: -1, y: 6 },
96
+ { x: 1, y: 14 },
97
+ { x: 1.25, y: -19.5 },
98
+ );
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 = 10, b = 0, c = -2
113
+ assertParabola(
114
+ { min: -1, max: 1, step: 0.25 },
115
+ { min: -1, max: -1, step: 1 },
116
+ { x: 0, y: -2 },
117
+ { x: 0.5, y: 0.5 },
118
+ { x: -0.5, y: 0.5 },
119
+ { x: -1, y: 8 },
120
+ { x: 1, y: 8 },
121
+ { x: 3, y: 88 },
122
+ );
123
+
124
+ // a = 10, b = -10, c = 0
125
+ assertParabola(
126
+ { min: -1, max: 1, step: 0.25 },
127
+ { min: -1, max: -1, step: 1 },
128
+ { x: 0.5, y: -2.5 },
129
+ { x: 0, y: 0 },
130
+ { x: 1, y: 0 },
131
+ { x: -1, y: 20 },
132
+ { x: 1.25, y: 3.125 },
133
+ { x: 3, y: 60 },
134
+ );
135
+
136
+ // a = 10, b = -10, c = 0
137
+ assertParabola(
138
+ { min: -1, max: 1, step: 0.5 },
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.5, y: 7.5 },
145
+ { x: 3, y: 60 },
146
+ );
147
+
148
+ // a = -4, b = 4, c = 0
149
+ assertParabola(
150
+ { min: -1, max: 5, step: 0.25 },
151
+ { min: -1, max: -1, step: 1 },
152
+ { x: 0.5, y: 1 },
153
+ { x: 0, y: 0 },
154
+ { x: 1, y: 0 },
155
+ { x: -0.5, y: -3 },
156
+ { x: 1.5, y: -3 },
157
+ { x: 3, y: 60 },
158
+ );
159
+ });
160
+
161
+ describe('pointsToABC', () => {
162
+ const assertPointsToABC = (one, two, three, a, b, c) => {
163
+ it(`${one}, ${two}, ${three} => ${a}, ${b}, ${c}`, () => {
164
+ const result = pointsToABC(one, two, three);
165
+ expect(result.a).toBeCloseTo(a);
166
+ expect(result.b).toBeCloseTo(b);
167
+ expect(result.c).toBeCloseTo(c);
168
+ });
169
+ };
170
+
171
+ assertPointsToABC(xy(0, 0), xy(1, 1), xy(-1, 1), 1, 0, 0);
172
+ assertPointsToABC(xy(0, 0), xy(2, 1), xy(-2, 1), 0.25, 0, 0);
173
+ assertPointsToABC(xy(1, 0), xy(2, 1), xy(0, 1), 1, -2, 1);
174
+ assertPointsToABC(xy(1, 0), xy(4, 1), xy(-2, 1), 0.11111, -0.22222, 0.11111);
175
+ assertPointsToABC(xy(0, 0), xy(1, 2), xy(-1, 2), 2, 0, 0);
176
+ });
177
+
178
+ describe('parabolaFromTwoPoints', () => {
179
+ describe('0,0 -> 1,2', () => {
180
+ const yVal = parabolaFromTwoPoints(xy(0, 0), xy(1, 1));
181
+ it('for x = 0, returns 0', () => expect(yVal(0)).toEqual(0));
182
+ it('for x = 1, returns 1', () => expect(yVal(1)).toEqual(1));
183
+ });
184
+ describe('0,0 -> 1,2', () => {
185
+ const yVal = parabolaFromTwoPoints(xy(0, 0), xy(1, 2));
186
+ it('for x = 0, returns 0', () => expect(yVal(0)).toEqual(0));
187
+ it('for x = 1, returns 2', () => expect(yVal(1)).toEqual(2));
188
+ });
189
+ });
190
+
191
+ describe('parabola', () => {
192
+ it('works', () => {
193
+ const fn = parabola(0.22222, -0.444444, 0.22222);
194
+ expect(fn(0)).toBeCloseTo(0.222222);
195
+ });
196
+ });
197
+ });
package/src/index.js CHANGED
@@ -85,23 +85,143 @@ export const pointsToABC = (one, two, three) => {
85
85
  */
86
86
  export const parabola = (a, b, c) => (x) => a * Math.pow(x, 2) + b * x + c;
87
87
 
88
+ /**
89
+ * Absolute value from two points Root (Vertex) the point where the two rays intersect and edge One point on a ray.
90
+ * @param root 1st point selected on graph
91
+ * @param edge 2nd point selected on graph
92
+ * https://www.desmos.com/calculator/rarxiatpip
93
+ */
94
+ export const absoluteFromTwoPoints = (root, edge) => {
95
+ edge = edge || { ...root };
96
+ const a = pointsToAForAbsolute(root, edge);
97
+ return absolute(a, root.x, root.y);
98
+ };
99
+
100
+ /*
101
+ * Finds value of a in y=a*abs(x-h)+k function
102
+ * @param one 1st point
103
+ * @param two 2nd point
104
+ * */
105
+ export const pointsToAForAbsolute = (one, two) => {
106
+ const y1 = two.y - one.y;
107
+ const x1 = two.x - one.x;
108
+ let a = y1 / x1;
109
+ if (two.x < one.x) {
110
+ a = a * -1;
111
+ }
112
+ return a;
113
+ };
114
+
115
+ /**
116
+ * y=a*abs(x-h)+k
117
+ */
118
+ export const absolute = (a, h, k) => (x) => a * Math.abs(x - h) + k;
119
+
120
+ /**
121
+ * Exponential from two points on exponential graph.
122
+ * @param root 1st point selected on graph
123
+ * @param edge 2nd point selected on graph
124
+ * https://www.desmos.com/calculator/3fisjexbvp
125
+ */
126
+ export const exponentialFromTwoPoints = (root, edge) => {
127
+ edge = edge || { ...root };
128
+ const { a, b } = pointsToABForExponential(root, edge);
129
+ return exponential(a, b);
130
+ };
131
+
132
+ /*
133
+ * Finds value of a and b in y=a*(b)^x function
134
+ * @param one 1st point
135
+ * @param two 2nd point
136
+ * */
137
+ export const pointsToABForExponential = (one, two) => {
138
+ const p = one.y / two.y;
139
+ const r = 1 / (one.x - two.x);
140
+ const b = Math.pow(p, r);
141
+ const a = one.y / Math.pow(b, one.x);
142
+ return { a, b };
143
+ };
144
+
145
+ /**
146
+ * y=a*(b)^x
147
+ */
148
+ export const exponential = (a, b) => (x) => a * Math.pow(b, x);
149
+
88
150
  /**
89
151
  * Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max
90
- * @param {*} min
91
- * @param {*} max
152
+ * @param {*} domain
153
+ * @param {*} range
92
154
  * @param {*} root
93
155
  * @param {*} edge
94
- * @param {*} interval
95
156
  * @param {*} yFn
157
+ * @param {*} excludeOutsidePoints
96
158
  */
97
- export const buildDataPoints = (min, max, root, edge, interval, yFn) => {
98
- log('[buildDataPoints] min:', min, 'max:', max, 'root:', root);
159
+ export const buildDataPoints = (domain, range, root, edge, yFn, excludeOutsidePoints = false) => {
160
+ log('[buildDataPoints] domain:', domain, 'range:', range, 'root:', root, 'edge:', edge);
161
+
162
+ domain = { min: 0, max: 0, step: 1, ...domain };
163
+ range = { min: 0, max: 0, step: 1, ...range };
99
164
  edge = edge ? edge : { ...root };
165
+
100
166
  const minX = Math.min(root.x, edge.x);
101
- const maxX = Math.max(root.x, edge.x);
102
- const leftSpace = min - minX;
103
- const rightSpace = max - maxX;
104
- const xs = xPoints(minX, interval, min - rightSpace, max - leftSpace);
105
- log('[buildDataPoints]:xs:', xs);
106
- return xs.map((v) => new Point(v, yFn(v)));
167
+ const xPts = xPoints(minX, domain.step, domain.min - domain.step, domain.max + domain.step);
168
+
169
+ log('[buildDataPoints]:xPts:', xPts);
170
+
171
+ let startIndex = -1;
172
+ let endIndex = -1;
173
+
174
+ // generate points based on the yFn
175
+ const points = xPts.map((v) => new Point(v, yFn(v)));
176
+
177
+ if (!excludeOutsidePoints) {
178
+ return points;
179
+ }
180
+
181
+ // exclude the points with y outside range min and max
182
+ const filteredPoints = points.reduce((acc, val, index) => {
183
+ if (val.y >= range.min - range.step && val.y <= range.max + range.step) {
184
+ if (startIndex === -1) {
185
+ startIndex = index;
186
+ }
187
+
188
+ if (endIndex < index) {
189
+ endIndex = index;
190
+ }
191
+
192
+ return [...acc, val];
193
+ }
194
+
195
+ return acc;
196
+ }, []);
197
+
198
+ // add the first value outside range min and max smaller than range.min if exists
199
+ if (startIndex - 1 >= 0) {
200
+ filteredPoints.unshift(points[startIndex - 1]);
201
+ }
202
+
203
+ // add the first value outside range min and max greater than range.min if exists
204
+ if (endIndex + 1 <= points.length - 1) {
205
+ filteredPoints.push(points[endIndex + 1]);
206
+ }
207
+
208
+ return points;
209
+ };
210
+
211
+ export default {
212
+ FREQ_DIVIDER,
213
+ getAmplitudeAndFreq,
214
+ xPoints,
215
+ sinY,
216
+ parabolaFromTwoPoints,
217
+ parabolaFromThreePoints,
218
+ pointsToABC,
219
+ parabola,
220
+ absoluteFromTwoPoints,
221
+ pointsToAForAbsolute,
222
+ absolute,
223
+ exponentialFromTwoPoints,
224
+ pointsToABForExponential,
225
+ exponential,
226
+ buildDataPoints,
107
227
  };
package/README.md DELETED
@@ -1,13 +0,0 @@
1
- # graphing-utils
2
-
3
- `graphing-utils` is a small collection of tools and helper functions that facilitate testing for components of the PIE framework.
4
-
5
- ## Usage
6
-
7
- ### Install
8
-
9
- npm install --save @pie-lib/graphing-utils
10
-
11
- ### Import
12
-
13
- import utils from '@pie-lib/graphing-utils';
package/lib/index.js DELETED
@@ -1,183 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.xPoints = exports.sinY = exports.pointsToABC = exports.parabolaFromTwoPoints = exports.parabolaFromThreePoints = exports.parabola = exports.getAmplitudeAndFreq = exports.buildDataPoints = exports.FREQ_DIVIDER = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _debug = _interopRequireDefault(require("debug"));
13
-
14
- var _pointGeometry = _interopRequireDefault(require("@mapbox/point-geometry"));
15
-
16
- var _lodash = _interopRequireDefault(require("lodash"));
17
-
18
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
19
-
20
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
21
-
22
- var log = (0, _debug["default"])('pie-lib:graphing:tools:utils');
23
- var FREQ_DIVIDER = 16;
24
- exports.FREQ_DIVIDER = FREQ_DIVIDER;
25
-
26
- var getAmplitudeAndFreq = function getAmplitudeAndFreq(root, edge) {
27
- if (!edge) {
28
- return {
29
- freq: 0,
30
- amplitude: 0
31
- };
32
- }
33
-
34
- if (root.x == edge.x) {
35
- return {
36
- freq: 0,
37
- amplitude: 0
38
- };
39
- }
40
-
41
- var r = new _pointGeometry["default"](root.x, root.y);
42
- var e = new _pointGeometry["default"](edge.x, edge.y);
43
- var d = e.sub(r); // edge point describes 1/4 of the freq
44
-
45
- return {
46
- freq: d.x * 4,
47
- amplitude: d.y
48
- };
49
- };
50
- /**
51
- * build the x points with root as the anchor.
52
- */
53
-
54
-
55
- exports.getAmplitudeAndFreq = getAmplitudeAndFreq;
56
-
57
- var xPoints = function xPoints(root, freq, min, max) {
58
- freq = Math.abs(freq);
59
- log('[xPoints] root:', root, 'freq:', freq, ' min:', min, ' max:', max);
60
-
61
- var leftpoints = _lodash["default"].rangeRight(root, min - freq, freq * -1);
62
-
63
- var rightpoints = _lodash["default"].range(root + freq, max + freq, freq);
64
-
65
- return _lodash["default"].concat(leftpoints, rightpoints);
66
- };
67
- /**
68
- * Calculate y for x value using amp, freq and shift
69
- * https://www.desmos.com/calculator/f1psfoiiv6
70
- */
71
-
72
-
73
- exports.xPoints = xPoints;
74
-
75
- var sinY = function sinY(amplitude, freq, shift) {
76
- return function (x) {
77
- shift = _objectSpread({
78
- phase: 0,
79
- vertical: 0
80
- }, shift);
81
- var TWO_PI = Math.PI * 2;
82
- var num = TWO_PI * (x - shift.phase);
83
- var frac = num / freq;
84
- var sin = Math.sin(frac);
85
- var sinFloat = parseFloat(sin).toFixed(10);
86
- return amplitude * sinFloat + shift.vertical;
87
- };
88
- };
89
- /**
90
- * You need three points for the quadratic eq, so we mirror the edge point
91
- * https://www.desmos.com/calculator/xhz1xqgpca
92
- */
93
-
94
-
95
- exports.sinY = sinY;
96
-
97
- var parabolaFromTwoPoints = function parabolaFromTwoPoints(root, edge) {
98
- edge = edge || _objectSpread({}, root);
99
- var mirrorEdge = {
100
- x: root.x - (edge.x - root.x),
101
- y: edge.y
102
- };
103
- return parabolaFromThreePoints(root, edge, mirrorEdge);
104
- };
105
-
106
- exports.parabolaFromTwoPoints = parabolaFromTwoPoints;
107
-
108
- var parabolaFromThreePoints = function parabolaFromThreePoints(one, two, three) {
109
- log(one, two, three);
110
-
111
- var _pointsToABC = pointsToABC(one, two, three),
112
- a = _pointsToABC.a,
113
- b = _pointsToABC.b,
114
- c = _pointsToABC.c;
115
-
116
- log(a, b, c);
117
- return parabola(a, b, c);
118
- };
119
- /** derive a,b,c from the 3 points */
120
-
121
-
122
- exports.parabolaFromThreePoints = parabolaFromThreePoints;
123
-
124
- var pointsToABC = function pointsToABC(one, two, three) {
125
- var a1 = Math.pow(one.x, 2) * -1 + Math.pow(two.x, 2);
126
- var b1 = one.x * -1 + two.x;
127
- var d1 = one.y * -1 + two.y;
128
- var a2 = Math.pow(two.x, 2) * -1 + Math.pow(three.x, 2);
129
- var b2 = two.x * -1 + three.x;
130
- var d2 = two.y * -1 + three.y;
131
- var bMult = b2 / b1 * -1;
132
- var a3 = bMult * a1 + a2;
133
- var d3 = bMult * d1 + d2;
134
- var a = d3 / a3;
135
- var b = (d1 - a1 * a) / b1;
136
- var c = one.y - a * Math.pow(one.x, 2) - b * one.x;
137
- return {
138
- a: a,
139
- b: b,
140
- c: c
141
- };
142
- };
143
- /**
144
- * y=ax^2+bx+c
145
- */
146
-
147
-
148
- exports.pointsToABC = pointsToABC;
149
-
150
- var parabola = function parabola(a, b, c) {
151
- return function (x) {
152
- return a * Math.pow(x, 2) + b * x + c;
153
- };
154
- };
155
- /**
156
- * Generate a set of data points, add spacing before min and after max if there is space between minx + min and maxX and max
157
- * @param {*} min
158
- * @param {*} max
159
- * @param {*} root
160
- * @param {*} edge
161
- * @param {*} interval
162
- * @param {*} yFn
163
- */
164
-
165
-
166
- exports.parabola = parabola;
167
-
168
- var buildDataPoints = function buildDataPoints(min, max, root, edge, interval, yFn) {
169
- log('[buildDataPoints] min:', min, 'max:', max, 'root:', root);
170
- edge = edge ? edge : _objectSpread({}, root);
171
- var minX = Math.min(root.x, edge.x);
172
- var maxX = Math.max(root.x, edge.x);
173
- var leftSpace = min - minX;
174
- var rightSpace = max - maxX;
175
- var xs = xPoints(minX, interval, min - rightSpace, max - leftSpace);
176
- log('[buildDataPoints]:xs:', xs);
177
- return xs.map(function (v) {
178
- return new _pointGeometry["default"](v, yFn(v));
179
- });
180
- };
181
-
182
- exports.buildDataPoints = buildDataPoints;
183
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.js"],"names":["log","FREQ_DIVIDER","getAmplitudeAndFreq","root","edge","freq","amplitude","x","r","Point","y","e","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","a","b","c","parabola","a1","pow","b1","d1","a2","b2","d2","bMult","a3","d3","buildDataPoints","interval","yFn","minX","maxX","leftSpace","rightSpace","xs","map","v"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AACA;;;;;;AACA,IAAMA,GAAG,GAAG,uBAAM,8BAAN,CAAZ;AAEO,IAAMC,YAAY,GAAG,EAArB;;;AAEA,IAAMC,mBAAmB,GAAG,SAAtBA,mBAAsB,CAACC,IAAD,EAAOC,IAAP,EAAgB;AACjD,MAAI,CAACA,IAAL,EAAW;AACT,WAAO;AAAEC,MAAAA,IAAI,EAAE,CAAR;AAAWC,MAAAA,SAAS,EAAE;AAAtB,KAAP;AACD;;AAED,MAAIH,IAAI,CAACI,CAAL,IAAUH,IAAI,CAACG,CAAnB,EAAsB;AACpB,WAAO;AAAEF,MAAAA,IAAI,EAAE,CAAR;AAAWC,MAAAA,SAAS,EAA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,CAACG,CAAtB,CAAb;AACA,MAAMwD,SAAS,GAAGhD,GAAG,GAAG8C,IAAxB;AACA,MAAMG,UAAU,GAAGhD,GAAG,GAAG8C,IAAzB;AACA,MAAMG,EAAE,GAAGnD,OAAO,CAAC+C,IAAD,EAAOF,QAAP,EAAiB5C,GAAG,GAAGiD,UAAvB,EAAmChD,GAAG,GAAG+C,SAAzC,CAAlB;AACA/D,EAAAA,GAAG,CAAC,uBAAD,EAA0BiE,EAA1B,CAAH;AACA,SAAOA,EAAE,CAACC,GAAH,CAAO,UAACC,CAAD;AAAA,WAAO,IAAI1D,yBAAJ,CAAU0D,CAAV,EAAaP,GAAG,CAACO,CAAD,CAAhB,CAAP;AAAA,GAAP,CAAP;AACD,CAVM","sourcesContent":["import debug from 'debug';\nimport Point from '@mapbox/point-geometry';\nimport _ from 'lodash';\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 * 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 {*} min\n * @param {*} max\n * @param {*} root\n * @param {*} edge\n * @param {*} interval\n * @param {*} yFn\n */\nexport const buildDataPoints = (min, max, root, edge, interval, yFn) => {\n log('[buildDataPoints] min:', min, 'max:', max, 'root:', root);\n edge = edge ? edge : { ...root };\n const minX = Math.min(root.x, edge.x);\n const maxX = Math.max(root.x, edge.x);\n const leftSpace = min - minX;\n const rightSpace = max - maxX;\n const xs = xPoints(minX, interval, min - rightSpace, max - leftSpace);\n log('[buildDataPoints]:xs:', xs);\n return xs.map((v) => new Point(v, yFn(v)));\n};\n"],"file":"index.js"}