@khanacademy/kmath 0.4.7 → 1.0.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.js +102 -412
- package/dist/index.js.map +1 -1
- package/package.json +14 -7
- package/dist/es/index.js +0 -1122
- package/dist/es/index.js.map +0 -1
package/dist/es/index.js
DELETED
|
@@ -1,1122 +0,0 @@
|
|
|
1
|
-
import { addLibraryVersionToPerseusDebug } from '@khanacademy/perseus-utils';
|
|
2
|
-
import _ from 'underscore';
|
|
3
|
-
import { approximateEqual, approximateDeepEqual } from '@khanacademy/perseus-core';
|
|
4
|
-
import $ from 'jquery';
|
|
5
|
-
|
|
6
|
-
// This file is processed by a Rollup plugin (replace) to inject the production
|
|
7
|
-
const libName = "@khanacademy/kmath";
|
|
8
|
-
const libVersion = "0.4.7";
|
|
9
|
-
addLibraryVersionToPerseusDebug(libName, libVersion);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Number Utils
|
|
13
|
-
* A number is a js-number, e.g. 5.12
|
|
14
|
-
*/
|
|
15
|
-
const DEFAULT_TOLERANCE = 1e-9;
|
|
16
|
-
|
|
17
|
-
// TODO: Should this just be Number.Epsilon
|
|
18
|
-
const EPSILON = Math.pow(2, -42);
|
|
19
|
-
function is$2(x) {
|
|
20
|
-
return _.isNumber(x) && !_.isNaN(x);
|
|
21
|
-
}
|
|
22
|
-
function equal$4(x, y, tolerance) {
|
|
23
|
-
// Checking for undefined makes this function behave nicely
|
|
24
|
-
// with vectors of different lengths that are _.zip'd together
|
|
25
|
-
if (x == null || y == null) {
|
|
26
|
-
return x === y;
|
|
27
|
-
}
|
|
28
|
-
// We check === here so that +/-Infinity comparisons work correctly
|
|
29
|
-
if (x === y) {
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
if (tolerance == null) {
|
|
33
|
-
tolerance = DEFAULT_TOLERANCE;
|
|
34
|
-
}
|
|
35
|
-
return Math.abs(x - y) < tolerance;
|
|
36
|
-
}
|
|
37
|
-
function sign$1(x, tolerance) /* Should be: 0 | 1 | -1 */{
|
|
38
|
-
return equal$4(x, 0, tolerance) ? 0 : Math.abs(x) / x;
|
|
39
|
-
}
|
|
40
|
-
function isInteger(num, tolerance) {
|
|
41
|
-
return equal$4(Math.round(num), num, tolerance);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Round a number to a certain number of decimal places
|
|
45
|
-
function round$2(num, precision) {
|
|
46
|
-
const factor = Math.pow(10, precision);
|
|
47
|
-
return Math.round(num * factor) / factor;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Round num to the nearest multiple of increment
|
|
51
|
-
// i.e. roundTo(83, 5) -> 85
|
|
52
|
-
function roundTo$2(num, increment) {
|
|
53
|
-
return Math.round(num / increment) * increment;
|
|
54
|
-
}
|
|
55
|
-
function floorTo$2(num, increment) {
|
|
56
|
-
return Math.floor(num / increment) * increment;
|
|
57
|
-
}
|
|
58
|
-
function ceilTo$2(num, increment) {
|
|
59
|
-
return Math.ceil(num / increment) * increment;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* toFraction
|
|
64
|
-
*
|
|
65
|
-
* Returns a [numerator, denominator] array rational representation
|
|
66
|
-
* of `decimal`
|
|
67
|
-
*
|
|
68
|
-
* See http://en.wikipedia.org/wiki/Continued_fraction for implementation
|
|
69
|
-
* details
|
|
70
|
-
*
|
|
71
|
-
* toFraction(4/8) => [1, 2]
|
|
72
|
-
* toFraction(0.66) => [33, 50]
|
|
73
|
-
* toFraction(0.66, 0.01) => [2/3]
|
|
74
|
-
* toFraction(283 + 1/3) => [850, 3]
|
|
75
|
-
*/
|
|
76
|
-
function toFraction(decimal,
|
|
77
|
-
// can't be 0
|
|
78
|
-
tolerance = EPSILON, maxDenominator = 1000) {
|
|
79
|
-
// Initialize everything to compute successive terms of
|
|
80
|
-
// continued-fraction approximations via recurrence relation
|
|
81
|
-
let n = [1, 0];
|
|
82
|
-
let d = [0, 1];
|
|
83
|
-
let a = Math.floor(decimal);
|
|
84
|
-
let rem = decimal - a;
|
|
85
|
-
while (d[0] <= maxDenominator) {
|
|
86
|
-
if (equal$4(n[0] / d[0], decimal, tolerance)) {
|
|
87
|
-
return [n[0], d[0]];
|
|
88
|
-
}
|
|
89
|
-
n = [a * n[0] + n[1], n[0]];
|
|
90
|
-
d = [a * d[0] + d[1], d[0]];
|
|
91
|
-
a = Math.floor(1 / rem);
|
|
92
|
-
rem = 1 / rem - a;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// We failed to find a nice rational representation,
|
|
96
|
-
// so return an irrational "fraction"
|
|
97
|
-
return [decimal, 1];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
var number = /*#__PURE__*/Object.freeze({
|
|
101
|
-
__proto__: null,
|
|
102
|
-
DEFAULT_TOLERANCE: DEFAULT_TOLERANCE,
|
|
103
|
-
EPSILON: EPSILON,
|
|
104
|
-
is: is$2,
|
|
105
|
-
equal: equal$4,
|
|
106
|
-
sign: sign$1,
|
|
107
|
-
isInteger: isInteger,
|
|
108
|
-
round: round$2,
|
|
109
|
-
roundTo: roundTo$2,
|
|
110
|
-
floorTo: floorTo$2,
|
|
111
|
-
ceilTo: ceilTo$2,
|
|
112
|
-
toFraction: toFraction
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Vector Utils
|
|
117
|
-
* A vector is an array of numbers e.g. [0, 3, 4].
|
|
118
|
-
*/
|
|
119
|
-
function arraySum(array) {
|
|
120
|
-
return array.reduce((memo, arg) => memo + arg, 0);
|
|
121
|
-
}
|
|
122
|
-
function arrayProduct(array) {
|
|
123
|
-
return array.reduce((memo, arg) => memo * arg, 1);
|
|
124
|
-
}
|
|
125
|
-
function zip(...arrays) {
|
|
126
|
-
const n = Math.min(...arrays.map(a => a.length));
|
|
127
|
-
const results = [];
|
|
128
|
-
for (let i = 0; i < n; i++) {
|
|
129
|
-
results.push(arrays.map(a => a[i]));
|
|
130
|
-
}
|
|
131
|
-
return results;
|
|
132
|
-
}
|
|
133
|
-
function map(pair, f) {
|
|
134
|
-
return [f(pair[0], 0), f(pair[1], 1)];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Checks if the given vector contains only numbers and, optionally, is of the
|
|
139
|
-
* right dimension (length).
|
|
140
|
-
*
|
|
141
|
-
* is([1, 2, 3]) -> true
|
|
142
|
-
* is([1, "Hello", 3]) -> false
|
|
143
|
-
* is([1, 2, 3], 1) -> false
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
function is$1(vec, dimension) {
|
|
147
|
-
if (!Array.isArray(vec)) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
if (dimension !== undefined && vec.length !== dimension) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
return vec.every(is$2);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Normalize to a unit vector
|
|
157
|
-
function normalize(v) {
|
|
158
|
-
return scale(v, 1 / length(v));
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Length/magnitude of a vector
|
|
162
|
-
function length(v) {
|
|
163
|
-
return Math.sqrt(dot(v, v));
|
|
164
|
-
}
|
|
165
|
-
// Dot product of two vectors
|
|
166
|
-
function dot(a, b) {
|
|
167
|
-
const zipped = zip(a, b);
|
|
168
|
-
const multiplied = zipped.map(arrayProduct);
|
|
169
|
-
return arraySum(multiplied);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/* vector-add multiple [x, y] coords/vectors
|
|
173
|
-
*
|
|
174
|
-
* add([1, 2], [3, 4]) -> [4, 6]
|
|
175
|
-
*/
|
|
176
|
-
function add$1(...vecs) {
|
|
177
|
-
const zipped = zip(...vecs);
|
|
178
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
179
|
-
return zipped.map(arraySum);
|
|
180
|
-
}
|
|
181
|
-
function subtract(v1, v2) {
|
|
182
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
183
|
-
return zip(v1, v2).map(dim => dim[0] - dim[1]);
|
|
184
|
-
}
|
|
185
|
-
function negate(v) {
|
|
186
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
187
|
-
return v.map(x => {
|
|
188
|
-
return -x;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Scale a vector
|
|
193
|
-
function scale(v1, scalar) {
|
|
194
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
195
|
-
return v1.map(x => {
|
|
196
|
-
return x * scalar;
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
function equal$3(v1, v2, tolerance) {
|
|
200
|
-
return v1.length === v2.length && zip(v1, v2).every(pair => equal$4(pair[0], pair[1], tolerance));
|
|
201
|
-
}
|
|
202
|
-
function codirectional(v1, v2, tolerance) {
|
|
203
|
-
// The origin is trivially codirectional with all other vectors.
|
|
204
|
-
// This gives nice semantics for codirectionality between points when
|
|
205
|
-
// comparing their difference vectors.
|
|
206
|
-
if (equal$4(length(v1), 0, tolerance) || equal$4(length(v2), 0, tolerance)) {
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
v1 = normalize(v1);
|
|
210
|
-
v2 = normalize(v2);
|
|
211
|
-
return equal$3(v1, v2, tolerance);
|
|
212
|
-
}
|
|
213
|
-
function collinear$1(v1, v2, tolerance) {
|
|
214
|
-
return codirectional(v1, v2, tolerance) || codirectional(v1, negate(v2), tolerance);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// TODO(jeremy) These coordinate conversion functions really only handle 2D points (ie. [number, number])
|
|
218
|
-
|
|
219
|
-
// Convert a cartesian coordinate into a radian polar coordinate
|
|
220
|
-
function polarRadFromCart$1(v) {
|
|
221
|
-
const radius = length(v);
|
|
222
|
-
let theta = Math.atan2(v[1], v[0]);
|
|
223
|
-
|
|
224
|
-
// Convert angle range from [-pi, pi] to [0, 2pi]
|
|
225
|
-
if (theta < 0) {
|
|
226
|
-
theta += 2 * Math.PI;
|
|
227
|
-
}
|
|
228
|
-
return [radius, theta];
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Converts a cartesian coordinate into a degree polar coordinate
|
|
232
|
-
function polarDegFromCart$1(v) /* TODO: convert to tuple/Point */{
|
|
233
|
-
const polar = polarRadFromCart$1(v);
|
|
234
|
-
return [polar[0], polar[1] * 180 / Math.PI];
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/* Convert a polar coordinate into a cartesian coordinate
|
|
238
|
-
*
|
|
239
|
-
* Examples:
|
|
240
|
-
* cartFromPolarRad(5, Math.PI)
|
|
241
|
-
*/
|
|
242
|
-
function cartFromPolarRad$1(radius, theta = 0) /* TODO: convert to tuple/Point */{
|
|
243
|
-
return [radius * Math.cos(theta), radius * Math.sin(theta)];
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/* Convert a polar coordinate into a cartesian coordinate
|
|
247
|
-
*
|
|
248
|
-
* Examples:
|
|
249
|
-
* cartFromPolarDeg(5, 30)
|
|
250
|
-
*/
|
|
251
|
-
function cartFromPolarDeg$1(radius, theta = 0) {
|
|
252
|
-
return cartFromPolarRad$1(radius, theta * Math.PI / 180);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Rotate vector
|
|
256
|
-
function rotateRad$1(v, theta) {
|
|
257
|
-
const polar = polarRadFromCart$1(v);
|
|
258
|
-
const angle = polar[1] + theta;
|
|
259
|
-
return cartFromPolarRad$1(polar[0], angle);
|
|
260
|
-
}
|
|
261
|
-
function rotateDeg$1(v, theta) {
|
|
262
|
-
const polar = polarDegFromCart$1(v);
|
|
263
|
-
const angle = polar[1] + theta;
|
|
264
|
-
return cartFromPolarDeg$1(polar[0], angle);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Angle between two vectors
|
|
268
|
-
function angleRad(v1, v2) {
|
|
269
|
-
return Math.acos(dot(v1, v2) / (length(v1) * length(v2)));
|
|
270
|
-
}
|
|
271
|
-
function angleDeg(v1, v2) {
|
|
272
|
-
return angleRad(v1, v2) * 180 / Math.PI;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// Vector projection of v1 onto v2
|
|
276
|
-
function projection(v1, v2) {
|
|
277
|
-
const scalar = dot(v1, v2) / dot(v2, v2);
|
|
278
|
-
return scale(v2, scalar);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Round each number to a certain number of decimal places
|
|
282
|
-
function round$1(vec, precision) {
|
|
283
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
284
|
-
return vec.map((elem, i) => round$2(elem, precision[i] || precision));
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// Round each number to the nearest increment
|
|
288
|
-
|
|
289
|
-
function roundTo$1(vec, increment) {
|
|
290
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
291
|
-
return vec.map((elem, i) => roundTo$2(elem, increment[i] || increment));
|
|
292
|
-
}
|
|
293
|
-
function floorTo$1(vec, increment) {
|
|
294
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
295
|
-
return vec.map((elem, i) => floorTo$2(elem, increment[i] || increment));
|
|
296
|
-
}
|
|
297
|
-
function ceilTo$1(vec, increment) {
|
|
298
|
-
// @ts-expect-error - TS2322 - Type 'number[]' is not assignable to type 'V'.
|
|
299
|
-
return vec.map((elem, i) => ceilTo$2(elem, increment[i] || increment));
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
var vector$1 = /*#__PURE__*/Object.freeze({
|
|
303
|
-
__proto__: null,
|
|
304
|
-
zip: zip,
|
|
305
|
-
map: map,
|
|
306
|
-
is: is$1,
|
|
307
|
-
normalize: normalize,
|
|
308
|
-
length: length,
|
|
309
|
-
dot: dot,
|
|
310
|
-
add: add$1,
|
|
311
|
-
subtract: subtract,
|
|
312
|
-
negate: negate,
|
|
313
|
-
scale: scale,
|
|
314
|
-
equal: equal$3,
|
|
315
|
-
codirectional: codirectional,
|
|
316
|
-
collinear: collinear$1,
|
|
317
|
-
polarRadFromCart: polarRadFromCart$1,
|
|
318
|
-
polarDegFromCart: polarDegFromCart$1,
|
|
319
|
-
cartFromPolarRad: cartFromPolarRad$1,
|
|
320
|
-
cartFromPolarDeg: cartFromPolarDeg$1,
|
|
321
|
-
rotateRad: rotateRad$1,
|
|
322
|
-
rotateDeg: rotateDeg$1,
|
|
323
|
-
angleRad: angleRad,
|
|
324
|
-
angleDeg: angleDeg,
|
|
325
|
-
projection: projection,
|
|
326
|
-
round: round$1,
|
|
327
|
-
roundTo: roundTo$1,
|
|
328
|
-
floorTo: floorTo$1,
|
|
329
|
-
ceilTo: ceilTo$1
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Point Utils
|
|
334
|
-
* A point is an array of two numbers e.g. [0, 0].
|
|
335
|
-
*/
|
|
336
|
-
|
|
337
|
-
// A point, in 2D, 3D, or nD space.
|
|
338
|
-
|
|
339
|
-
// Rotate point (around origin unless a center is specified)
|
|
340
|
-
function rotateRad(point, theta, center) {
|
|
341
|
-
if (center === undefined) {
|
|
342
|
-
return rotateRad$1(point, theta);
|
|
343
|
-
} else {
|
|
344
|
-
return add$1(center, rotateRad$1(subtract(point, center), theta));
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
function rotateDeg(point, theta, center) {
|
|
348
|
-
if (center === undefined) {
|
|
349
|
-
return rotateDeg$1(point, theta);
|
|
350
|
-
} else {
|
|
351
|
-
return add$1(center, rotateDeg$1(subtract(point, center), theta));
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// Distance between two points
|
|
356
|
-
function distanceToPoint$1(point1, point2) {
|
|
357
|
-
return length(subtract(point1, point2));
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// Distance between point and line
|
|
361
|
-
function distanceToLine(point, line) {
|
|
362
|
-
const lv = subtract(line[1], line[0]);
|
|
363
|
-
const pv = subtract(point, line[0]);
|
|
364
|
-
const projectedPv = projection(pv, lv);
|
|
365
|
-
const distancePv = subtract(projectedPv, pv);
|
|
366
|
-
return length(distancePv);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Reflect point over line
|
|
370
|
-
function reflectOverLine(point, line) {
|
|
371
|
-
const lv = subtract(line[1], line[0]);
|
|
372
|
-
const pv = subtract(point, line[0]);
|
|
373
|
-
const projectedPv = projection(pv, lv);
|
|
374
|
-
const reflectedPv = subtract(scale(projectedPv, 2), pv);
|
|
375
|
-
return add$1(line[0], reflectedPv);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Compares two points, returning -1, 0, or 1, for use with
|
|
380
|
-
* Array.prototype.sort
|
|
381
|
-
*
|
|
382
|
-
* Note: This technically doesn't satisfy the total-ordering
|
|
383
|
-
* requirements of Array.prototype.sort unless equalityTolerance
|
|
384
|
-
* is 0. In some cases very close points that compare within a
|
|
385
|
-
* few equalityTolerances could appear in the wrong order.
|
|
386
|
-
*/
|
|
387
|
-
function compare(point1, point2, equalityTolerance) /* TODO: convert to -1 | 0 | 1 type */{
|
|
388
|
-
if (point1.length !== point2.length) {
|
|
389
|
-
return point1.length - point2.length;
|
|
390
|
-
}
|
|
391
|
-
for (let i = 0; i < point1.length; i++) {
|
|
392
|
-
if (!equal$4(point1[i], point2[i], equalityTolerance)) {
|
|
393
|
-
return point1[i] - point2[i];
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return 0;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// Check if a value is a point
|
|
400
|
-
const is = is$1;
|
|
401
|
-
|
|
402
|
-
// Add and subtract vector(s)
|
|
403
|
-
const addVector = add$1;
|
|
404
|
-
const addVectors = add$1;
|
|
405
|
-
const subtractVector = subtract;
|
|
406
|
-
const equal$2 = equal$3;
|
|
407
|
-
|
|
408
|
-
// Convert from cartesian to polar and back
|
|
409
|
-
const polarRadFromCart = polarRadFromCart$1;
|
|
410
|
-
const polarDegFromCart = polarDegFromCart$1;
|
|
411
|
-
const cartFromPolarRad = cartFromPolarRad$1;
|
|
412
|
-
const cartFromPolarDeg = cartFromPolarDeg$1;
|
|
413
|
-
|
|
414
|
-
// Rounding
|
|
415
|
-
const round = round$1;
|
|
416
|
-
const roundTo = roundTo$1;
|
|
417
|
-
const floorTo = floorTo$1;
|
|
418
|
-
const ceilTo = ceilTo$1;
|
|
419
|
-
|
|
420
|
-
var point = /*#__PURE__*/Object.freeze({
|
|
421
|
-
__proto__: null,
|
|
422
|
-
rotateRad: rotateRad,
|
|
423
|
-
rotateDeg: rotateDeg,
|
|
424
|
-
distanceToPoint: distanceToPoint$1,
|
|
425
|
-
distanceToLine: distanceToLine,
|
|
426
|
-
reflectOverLine: reflectOverLine,
|
|
427
|
-
compare: compare,
|
|
428
|
-
is: is,
|
|
429
|
-
addVector: addVector,
|
|
430
|
-
addVectors: addVectors,
|
|
431
|
-
subtractVector: subtractVector,
|
|
432
|
-
equal: equal$2,
|
|
433
|
-
polarRadFromCart: polarRadFromCart,
|
|
434
|
-
polarDegFromCart: polarDegFromCart,
|
|
435
|
-
cartFromPolarRad: cartFromPolarRad,
|
|
436
|
-
cartFromPolarDeg: cartFromPolarDeg,
|
|
437
|
-
round: round,
|
|
438
|
-
roundTo: roundTo,
|
|
439
|
-
floorTo: floorTo,
|
|
440
|
-
ceilTo: ceilTo
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* Line Utils
|
|
445
|
-
* A line is an array of two points e.g. [[-5, 0], [5, 0]].
|
|
446
|
-
*/
|
|
447
|
-
function distanceToPoint(line, point$1) {
|
|
448
|
-
return distanceToLine(point$1, line);
|
|
449
|
-
}
|
|
450
|
-
function reflectPoint(line, point$1) {
|
|
451
|
-
return reflectOverLine(point$1, line);
|
|
452
|
-
}
|
|
453
|
-
function midpoint(line) {
|
|
454
|
-
return [(line[0][0] + line[1][0]) / 2, (line[0][1] + line[1][1]) / 2];
|
|
455
|
-
}
|
|
456
|
-
function equal$1(line1, line2, tolerance) {
|
|
457
|
-
// TODO: A nicer implementation might just check collinearity of
|
|
458
|
-
// vectors using underscore magick
|
|
459
|
-
// Compare the directions of the lines
|
|
460
|
-
const v1 = subtract(line1[1], line1[0]);
|
|
461
|
-
const v2 = subtract(line2[1], line2[0]);
|
|
462
|
-
if (!collinear$1(v1, v2, tolerance)) {
|
|
463
|
-
return false;
|
|
464
|
-
}
|
|
465
|
-
// If the start point is the same for the two lines, then they are the same
|
|
466
|
-
if (equal$2(line1[0], line2[0])) {
|
|
467
|
-
return true;
|
|
468
|
-
}
|
|
469
|
-
// Make sure that the direction to get from line1 to
|
|
470
|
-
// line2 is the same as the direction of the lines
|
|
471
|
-
const line1ToLine2Vector = subtract(line2[0], line1[0]);
|
|
472
|
-
return collinear$1(v1, line1ToLine2Vector, tolerance);
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
var line = /*#__PURE__*/Object.freeze({
|
|
476
|
-
__proto__: null,
|
|
477
|
-
distanceToPoint: distanceToPoint,
|
|
478
|
-
reflectPoint: reflectPoint,
|
|
479
|
-
midpoint: midpoint,
|
|
480
|
-
equal: equal$1
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Ray Utils
|
|
485
|
-
* A ray (→) is an array of an endpoint and another point along the ray.
|
|
486
|
-
* For example, [[0, 0], [1, 0]] is the ray starting at the origin and
|
|
487
|
-
* traveling along the positive x-axis.
|
|
488
|
-
*/
|
|
489
|
-
function equal(ray1, ray2, tolerance) {
|
|
490
|
-
// Compare the directions of the rays
|
|
491
|
-
const v1 = subtract(ray1[1], ray1[0]);
|
|
492
|
-
const v2 = subtract(ray2[1], ray2[0]);
|
|
493
|
-
const sameOrigin = equal$2(ray1[0], ray2[0]);
|
|
494
|
-
const codirectional$1 = codirectional(v1, v2, tolerance);
|
|
495
|
-
return sameOrigin && codirectional$1;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
var ray = /*#__PURE__*/Object.freeze({
|
|
499
|
-
__proto__: null,
|
|
500
|
-
equal: equal
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
const KhanMath = {
|
|
504
|
-
// Simplify formulas before display
|
|
505
|
-
cleanMath: function (expr) {
|
|
506
|
-
return typeof expr === "string" ? expr.replace(/\+\s*-/g, "- ").replace(/-\s*-/g, "+ ").replace(/\^1/g, "") : expr;
|
|
507
|
-
},
|
|
508
|
-
// Bound a number by 1e-6 and 1e20 to avoid exponents after toString
|
|
509
|
-
bound: function (num) {
|
|
510
|
-
if (num === 0) {
|
|
511
|
-
return num;
|
|
512
|
-
}
|
|
513
|
-
if (num < 0) {
|
|
514
|
-
return -KhanMath.bound(-num);
|
|
515
|
-
}
|
|
516
|
-
return Math.max(1e-6, Math.min(num, 1e20));
|
|
517
|
-
},
|
|
518
|
-
factorial: function (x) {
|
|
519
|
-
if (x <= 1) {
|
|
520
|
-
return x;
|
|
521
|
-
}
|
|
522
|
-
return x * KhanMath.factorial(x - 1);
|
|
523
|
-
},
|
|
524
|
-
getGCD: function (a, b) {
|
|
525
|
-
if (arguments.length > 2) {
|
|
526
|
-
// TODO(kevinb): rewrite using rest args instead of arguments
|
|
527
|
-
// eslint-disable-next-line prefer-rest-params
|
|
528
|
-
const rest = [].slice.call(arguments, 1);
|
|
529
|
-
// @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
|
|
530
|
-
return KhanMath.getGCD(a, KhanMath.getGCD(...rest));
|
|
531
|
-
}
|
|
532
|
-
let mod;
|
|
533
|
-
a = Math.abs(a);
|
|
534
|
-
b = Math.abs(b);
|
|
535
|
-
while (b) {
|
|
536
|
-
mod = a % b;
|
|
537
|
-
a = b;
|
|
538
|
-
b = mod;
|
|
539
|
-
}
|
|
540
|
-
return a;
|
|
541
|
-
},
|
|
542
|
-
getLCM: function (a, b) {
|
|
543
|
-
if (arguments.length > 2) {
|
|
544
|
-
// TODO(kevinb): rewrite using rest args instead of arguments
|
|
545
|
-
// eslint-disable-next-line prefer-rest-params
|
|
546
|
-
const rest = [].slice.call(arguments, 1);
|
|
547
|
-
// @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
|
|
548
|
-
return KhanMath.getLCM(a, KhanMath.getLCM(...rest));
|
|
549
|
-
}
|
|
550
|
-
return Math.abs(a * b) / KhanMath.getGCD(a, b);
|
|
551
|
-
},
|
|
552
|
-
primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
|
|
553
|
-
isPrime: function (n) {
|
|
554
|
-
if (n <= 1) {
|
|
555
|
-
return false;
|
|
556
|
-
}
|
|
557
|
-
if (n < 101) {
|
|
558
|
-
return !!$.grep(KhanMath.primes, function (p, i) {
|
|
559
|
-
return Math.abs(p - n) <= 0.5;
|
|
560
|
-
}).length;
|
|
561
|
-
}
|
|
562
|
-
if (n <= 1 || n > 2 && n % 2 === 0) {
|
|
563
|
-
return false;
|
|
564
|
-
}
|
|
565
|
-
for (let i = 3, sqrt = Math.sqrt(n); i <= sqrt; i += 2) {
|
|
566
|
-
if (n % i === 0) {
|
|
567
|
-
return false;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
return true;
|
|
571
|
-
},
|
|
572
|
-
// @ts-expect-error - TS2366 - Function lacks ending return statement and return type does not include 'undefined'.
|
|
573
|
-
getPrimeFactorization: function (number) {
|
|
574
|
-
if (number === 1) {
|
|
575
|
-
return [];
|
|
576
|
-
}
|
|
577
|
-
if (KhanMath.isPrime(number)) {
|
|
578
|
-
return [number];
|
|
579
|
-
}
|
|
580
|
-
const maxf = Math.sqrt(number);
|
|
581
|
-
for (let f = 2; f <= maxf; f++) {
|
|
582
|
-
if (number % f === 0) {
|
|
583
|
-
return $.merge(KhanMath.getPrimeFactorization(f), KhanMath.getPrimeFactorization(number / f));
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
},
|
|
587
|
-
// Round a number to the nearest increment
|
|
588
|
-
// E.g., if increment = 30 and num = 40, return 30. if increment = 30 and
|
|
589
|
-
// num = 45, return 60.
|
|
590
|
-
roundToNearest: function (increment, num) {
|
|
591
|
-
return Math.round(num / increment) * increment;
|
|
592
|
-
},
|
|
593
|
-
// Round a number to a certain number of decimal places
|
|
594
|
-
roundTo: function (precision, num) {
|
|
595
|
-
const factor = Math.pow(10, precision).toFixed(5);
|
|
596
|
-
// @ts-expect-error - TS2345 - Argument of type 'string' is not assignable to parameter of type 'number'. | TS2363 - The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. | TS2363 - The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
|
|
597
|
-
return Math.round((num * factor).toFixed(5)) / factor;
|
|
598
|
-
},
|
|
599
|
-
/**
|
|
600
|
-
* Return a string of num rounded to a fixed precision decimal places,
|
|
601
|
-
* with an approx symbol if num had to be rounded, and trailing 0s
|
|
602
|
-
*/
|
|
603
|
-
toFixedApprox: function (num, precision) {
|
|
604
|
-
// TODO(aria): Make this locale-dependent
|
|
605
|
-
const fixedStr = num.toFixed(precision);
|
|
606
|
-
if (equal$4(+fixedStr, num)) {
|
|
607
|
-
return fixedStr;
|
|
608
|
-
}
|
|
609
|
-
return "\\approx " + fixedStr;
|
|
610
|
-
},
|
|
611
|
-
/**
|
|
612
|
-
* Return a string of num rounded to precision decimal places, with an
|
|
613
|
-
* approx symbol if num had to be rounded, but no trailing 0s if it was
|
|
614
|
-
* not rounded.
|
|
615
|
-
*/
|
|
616
|
-
roundToApprox: function (num, precision) {
|
|
617
|
-
const fixed = KhanMath.roundTo(precision, num);
|
|
618
|
-
if (equal$4(fixed, num)) {
|
|
619
|
-
return String(fixed);
|
|
620
|
-
}
|
|
621
|
-
return KhanMath.toFixedApprox(num, precision);
|
|
622
|
-
},
|
|
623
|
-
// toFraction(4/8) => [1, 2]
|
|
624
|
-
// toFraction(0.666) => [333, 500]
|
|
625
|
-
// toFraction(0.666, 0.001) => [2, 3]
|
|
626
|
-
//
|
|
627
|
-
// tolerance can't be bigger than 1, sorry
|
|
628
|
-
toFraction: function (decimal, tolerance) {
|
|
629
|
-
if (tolerance == null) {
|
|
630
|
-
tolerance = Math.pow(2, -46);
|
|
631
|
-
}
|
|
632
|
-
if (decimal < 0 || decimal > 1) {
|
|
633
|
-
let fract = decimal % 1;
|
|
634
|
-
fract += fract < 0 ? 1 : 0;
|
|
635
|
-
const nd = KhanMath.toFraction(fract, tolerance);
|
|
636
|
-
nd[0] += Math.round(decimal - fract) * nd[1];
|
|
637
|
-
return nd;
|
|
638
|
-
}
|
|
639
|
-
if (Math.abs(Math.round(Number(decimal)) - decimal) <= tolerance) {
|
|
640
|
-
return [Math.round(decimal), 1];
|
|
641
|
-
}
|
|
642
|
-
let loN = 0;
|
|
643
|
-
let loD = 1;
|
|
644
|
-
let hiN = 1;
|
|
645
|
-
let hiD = 1;
|
|
646
|
-
let midN = 1;
|
|
647
|
-
let midD = 2;
|
|
648
|
-
|
|
649
|
-
// eslint-disable-next-line no-constant-condition
|
|
650
|
-
while (true) {
|
|
651
|
-
if (Math.abs(Number(midN / midD) - decimal) <= tolerance) {
|
|
652
|
-
return [midN, midD];
|
|
653
|
-
}
|
|
654
|
-
if (midN / midD < decimal) {
|
|
655
|
-
loN = midN;
|
|
656
|
-
loD = midD;
|
|
657
|
-
} else {
|
|
658
|
-
hiN = midN;
|
|
659
|
-
hiD = midD;
|
|
660
|
-
}
|
|
661
|
-
midN = loN + hiN;
|
|
662
|
-
midD = loD + hiD;
|
|
663
|
-
}
|
|
664
|
-
},
|
|
665
|
-
// Returns the format (string) of a given numeric string
|
|
666
|
-
// Note: purposively more inclusive than answer-types' predicate.forms
|
|
667
|
-
// That is, it is not necessarily true that interpreted input are numeric
|
|
668
|
-
getNumericFormat: function (text) {
|
|
669
|
-
text = $.trim(text);
|
|
670
|
-
text = text.replace(/\u2212/, "-").replace(/([+-])\s+/g, "$1");
|
|
671
|
-
if (text.match(/^[+-]?\d+$/)) {
|
|
672
|
-
return "integer";
|
|
673
|
-
}
|
|
674
|
-
if (text.match(/^[+-]?\d+\s+\d+\s*\/\s*\d+$/)) {
|
|
675
|
-
return "mixed";
|
|
676
|
-
}
|
|
677
|
-
const fraction = text.match(/^[+-]?(\d+)\s*\/\s*(\d+)$/);
|
|
678
|
-
if (fraction) {
|
|
679
|
-
return parseFloat(fraction[1]) > parseFloat(fraction[2]) ? "improper" : "proper";
|
|
680
|
-
}
|
|
681
|
-
if (text.replace(/[,. ]/g, "").match(/^\d+$/)) {
|
|
682
|
-
return "decimal";
|
|
683
|
-
}
|
|
684
|
-
if (text.match(/(pi?|\u03c0|t(?:au)?|\u03c4|pau)/)) {
|
|
685
|
-
return "pi";
|
|
686
|
-
}
|
|
687
|
-
return null;
|
|
688
|
-
},
|
|
689
|
-
// Returns a string of the number in a specified format
|
|
690
|
-
toNumericString: function (number$1, format) {
|
|
691
|
-
if (number$1 == null) {
|
|
692
|
-
return "";
|
|
693
|
-
}
|
|
694
|
-
if (number$1 === 0) {
|
|
695
|
-
return "0"; // otherwise it might end up as 0% or 0pi
|
|
696
|
-
}
|
|
697
|
-
if (format === "percent") {
|
|
698
|
-
return number$1 * 100 + "%";
|
|
699
|
-
}
|
|
700
|
-
if (format === "pi") {
|
|
701
|
-
const fraction = toFraction(number$1 / Math.PI);
|
|
702
|
-
const numerator = Math.abs(fraction[0]);
|
|
703
|
-
const denominator = fraction[1];
|
|
704
|
-
if (isInteger(numerator)) {
|
|
705
|
-
const sign = number$1 < 0 ? "-" : "";
|
|
706
|
-
const pi = "\u03C0";
|
|
707
|
-
return sign + (numerator === 1 ? "" : numerator) + pi + (denominator === 1 ? "" : "/" + denominator);
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
if (_(["proper", "improper", "mixed", "fraction"]).contains(format)) {
|
|
711
|
-
const fraction = toFraction(number$1);
|
|
712
|
-
const numerator = Math.abs(fraction[0]);
|
|
713
|
-
const denominator = fraction[1];
|
|
714
|
-
const sign = number$1 < 0 ? "-" : "";
|
|
715
|
-
if (denominator === 1) {
|
|
716
|
-
return sign + numerator; // for integers, irrational, d > 1000
|
|
717
|
-
}
|
|
718
|
-
if (format === "mixed") {
|
|
719
|
-
const modulus = numerator % denominator;
|
|
720
|
-
const integer = (numerator - modulus) / denominator;
|
|
721
|
-
return sign + (integer ? integer + " " : "") + modulus + "/" + denominator;
|
|
722
|
-
} // otherwise proper, improper, or fraction
|
|
723
|
-
return sign + numerator + "/" + denominator;
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
// otherwise (decimal, float, long long)
|
|
727
|
-
return String(number$1);
|
|
728
|
-
}
|
|
729
|
-
};
|
|
730
|
-
function sum(array) {
|
|
731
|
-
return array.reduce(add, 0);
|
|
732
|
-
}
|
|
733
|
-
function add(a, b) {
|
|
734
|
-
return a + b;
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
/**
|
|
738
|
-
* A collection of geomtry-related utility functions
|
|
739
|
-
*/
|
|
740
|
-
|
|
741
|
-
// This should really be a readonly tuple of [number, number]
|
|
742
|
-
|
|
743
|
-
// Given a number, return whether it is positive (1), negative (-1), or zero (0)
|
|
744
|
-
function sign(val) {
|
|
745
|
-
if (approximateEqual(val, 0)) {
|
|
746
|
-
return 0;
|
|
747
|
-
}
|
|
748
|
-
return val > 0 ? 1 : -1;
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
// Determine whether three points are collinear (0), for a clockwise turn (negative),
|
|
752
|
-
// or counterclockwise turn (positive)
|
|
753
|
-
function ccw(a, b, c) {
|
|
754
|
-
return (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
|
|
755
|
-
}
|
|
756
|
-
function collinear(a, b, c) {
|
|
757
|
-
return approximateEqual(ccw(a, b, c), 0);
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
// Given rect bounding points A and B, whether point C is inside the rect
|
|
761
|
-
function pointInRect(a, b, c) {
|
|
762
|
-
return c[0] <= Math.max(a[0], b[0]) && c[0] >= Math.min(a[0], b[0]) && c[1] <= Math.max(a[1], b[1]) && c[1] >= Math.min(a[1], b[1]);
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
// Whether line segment AB intersects line segment CD
|
|
766
|
-
// http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
|
|
767
|
-
function intersects(ab, cd) {
|
|
768
|
-
const triplets = [[ab[0], ab[1], cd[0]], [ab[0], ab[1], cd[1]], [cd[0], cd[1], ab[0]], [cd[0], cd[1], ab[1]]];
|
|
769
|
-
const orientations = _.map(triplets, function (triplet) {
|
|
770
|
-
return sign(ccw(...triplet));
|
|
771
|
-
});
|
|
772
|
-
if (orientations[0] !== orientations[1] && orientations[2] !== orientations[3]) {
|
|
773
|
-
return true;
|
|
774
|
-
}
|
|
775
|
-
for (let i = 0; i < 4; i++) {
|
|
776
|
-
if (orientations[i] === 0 && pointInRect(...triplets[i])) {
|
|
777
|
-
return true;
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
return false;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
// Whether any two sides of a polygon intersect each other
|
|
784
|
-
function polygonSidesIntersect(vertices) {
|
|
785
|
-
for (let i = 0; i < vertices.length; i++) {
|
|
786
|
-
for (let k = i + 1; k < vertices.length; k++) {
|
|
787
|
-
// If any two vertices are the same point, sides overlap
|
|
788
|
-
if (equal$2(vertices[i], vertices[k])) {
|
|
789
|
-
return true;
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
// Find the other end of the sides starting at vertices i and k
|
|
793
|
-
const iNext = (i + 1) % vertices.length;
|
|
794
|
-
const kNext = (k + 1) % vertices.length;
|
|
795
|
-
|
|
796
|
-
// Adjacent sides always intersect (at the vertex); skip those
|
|
797
|
-
if (iNext === k || kNext === i) {
|
|
798
|
-
continue;
|
|
799
|
-
}
|
|
800
|
-
const side1 = [vertices[i], vertices[iNext]];
|
|
801
|
-
const side2 = [vertices[k], vertices[kNext]];
|
|
802
|
-
if (intersects(side1, side2)) {
|
|
803
|
-
return true;
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
return false;
|
|
808
|
-
}
|
|
809
|
-
function vector(a, b) {
|
|
810
|
-
return _.map(_.zip(a, b), function (pair) {
|
|
811
|
-
return pair[0] - pair[1];
|
|
812
|
-
});
|
|
813
|
-
}
|
|
814
|
-
function reverseVector(vector) {
|
|
815
|
-
return [-vector[0], -vector[1]];
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
// Returns whether connecting the given sequence of `points` forms a clockwise
|
|
819
|
-
// path (assuming a closed loop, where the last point connects back to the
|
|
820
|
-
// first).
|
|
821
|
-
function clockwise(points) {
|
|
822
|
-
const segments = _.zip(points, points.slice(1).concat(points.slice(0, 1)));
|
|
823
|
-
const areas = _.map(segments, function (segment) {
|
|
824
|
-
const p1 = segment[0];
|
|
825
|
-
const p2 = segment[1];
|
|
826
|
-
return (p2[0] - p1[0]) * (p2[1] + p1[1]);
|
|
827
|
-
});
|
|
828
|
-
return sum(areas) > 0;
|
|
829
|
-
}
|
|
830
|
-
function magnitude(v) {
|
|
831
|
-
return Math.sqrt(_.reduce(v, function (memo, el) {
|
|
832
|
-
// @ts-expect-error - TS2345 - Argument of type 'Coord' is not assignable to parameter of type 'number'.
|
|
833
|
-
return memo + Math.pow(el, 2);
|
|
834
|
-
}, 0));
|
|
835
|
-
}
|
|
836
|
-
function dotProduct(a, b) {
|
|
837
|
-
return _.reduce(_.zip(a, b), function (memo, pair) {
|
|
838
|
-
return memo + pair[0] * pair[1];
|
|
839
|
-
}, 0);
|
|
840
|
-
}
|
|
841
|
-
function sideLengths(coords) {
|
|
842
|
-
const segments = _.zip(coords, rotate(coords));
|
|
843
|
-
return segments.map(function (segment) {
|
|
844
|
-
// @ts-expect-error - TS2345 - Argument of type 'number[]' is not assignable to parameter of type 'readonly Coord[]'. | TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
|
|
845
|
-
return magnitude(vector(...segment));
|
|
846
|
-
});
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
// Based on http://math.stackexchange.com/a/151149
|
|
850
|
-
function angleMeasures(coords) {
|
|
851
|
-
const triplets = _.zip(rotate(coords, -1), coords, rotate(coords, 1));
|
|
852
|
-
const offsets = _.map(triplets, function (triplet) {
|
|
853
|
-
const p = vector(triplet[1], triplet[0]);
|
|
854
|
-
const q = vector(triplet[2], triplet[1]);
|
|
855
|
-
// @ts-expect-error - TS2345 - Argument of type 'number[]' is not assignable to parameter of type 'Coord'. | TS2345 - Argument of type 'number[]' is not assignable to parameter of type 'readonly Coord[]'. | TS2345 - Argument of type 'number[]' is not assignable to parameter of type 'readonly Coord[]'.
|
|
856
|
-
const raw = Math.acos(dotProduct(p, q) / (magnitude(p) * magnitude(q)));
|
|
857
|
-
// @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
|
|
858
|
-
return sign(ccw(...triplet)) > 0 ? raw : -raw;
|
|
859
|
-
});
|
|
860
|
-
const sum = _.reduce(offsets, function (memo, arg) {
|
|
861
|
-
return memo + arg;
|
|
862
|
-
}, 0);
|
|
863
|
-
return _.map(offsets, function (offset) {
|
|
864
|
-
return sum > 0 ? Math.PI - offset : Math.PI + offset;
|
|
865
|
-
});
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
// Whether two polygons are similar (or if specified, congruent)
|
|
869
|
-
function similar(coords1, coords2, tolerance) {
|
|
870
|
-
if (coords1.length !== coords2.length) {
|
|
871
|
-
return false;
|
|
872
|
-
}
|
|
873
|
-
const n = coords1.length;
|
|
874
|
-
const angles1 = angleMeasures(coords1);
|
|
875
|
-
const angles2 = angleMeasures(coords2);
|
|
876
|
-
const sides1 = sideLengths(coords1);
|
|
877
|
-
const sides2 = sideLengths(coords2);
|
|
878
|
-
for (let i = 0; i < 2 * n; i++) {
|
|
879
|
-
let angles = angles2.slice();
|
|
880
|
-
let sides = sides2.slice();
|
|
881
|
-
|
|
882
|
-
// Reverse angles and sides to allow matching reflected polygons
|
|
883
|
-
if (i >= n) {
|
|
884
|
-
angles.reverse();
|
|
885
|
-
sides.reverse();
|
|
886
|
-
// Since sides are calculated from two coordinates,
|
|
887
|
-
// simply reversing results in an off by one error
|
|
888
|
-
// @ts-expect-error - TS4104 - The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
|
|
889
|
-
sides = rotate(sides, 1);
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
// @ts-expect-error - TS4104 - The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
|
|
893
|
-
angles = rotate(angles, i);
|
|
894
|
-
// @ts-expect-error - TS4104 - The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
|
|
895
|
-
sides = rotate(sides, i);
|
|
896
|
-
if (approximateDeepEqual(angles1, angles)) {
|
|
897
|
-
const sidePairs = _.zip(sides1, sides);
|
|
898
|
-
const factors = _.map(sidePairs, function (pair) {
|
|
899
|
-
return pair[0] / pair[1];
|
|
900
|
-
});
|
|
901
|
-
const same = _.all(factors, function (factor) {
|
|
902
|
-
return approximateEqual(factors[0], factor);
|
|
903
|
-
});
|
|
904
|
-
const congruentEnough = _.all(sidePairs, function (pair) {
|
|
905
|
-
return equal$4(pair[0], pair[1], tolerance);
|
|
906
|
-
});
|
|
907
|
-
if (same && congruentEnough) {
|
|
908
|
-
return true;
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
return false;
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
// Given triangle with sides ABC return angle opposite side C in degrees
|
|
916
|
-
function lawOfCosines(a, b, c) {
|
|
917
|
-
return Math.acos((a * a + b * b - c * c) / (2 * a * b)) * 180 / Math.PI;
|
|
918
|
-
}
|
|
919
|
-
function canonicalSineCoefficients([amplitude, angularFrequency, phase, verticalOffset]) {
|
|
920
|
-
// For a curve of the form f(x) = a * Sin(b * x - c) + d,
|
|
921
|
-
// this function ensures that a, b > 0, and c is its
|
|
922
|
-
// smallest possible positive value.
|
|
923
|
-
|
|
924
|
-
// Guarantee a > 0
|
|
925
|
-
if (amplitude < 0) {
|
|
926
|
-
amplitude *= -1;
|
|
927
|
-
angularFrequency *= -1;
|
|
928
|
-
phase *= -1;
|
|
929
|
-
}
|
|
930
|
-
const period = 2 * Math.PI;
|
|
931
|
-
// Guarantee b > 0
|
|
932
|
-
if (angularFrequency < 0) {
|
|
933
|
-
angularFrequency *= -1;
|
|
934
|
-
phase *= -1;
|
|
935
|
-
phase += period / 2;
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
// Guarantee c is smallest possible positive value
|
|
939
|
-
while (phase > 0) {
|
|
940
|
-
phase -= period;
|
|
941
|
-
}
|
|
942
|
-
while (phase < 0) {
|
|
943
|
-
phase += period;
|
|
944
|
-
}
|
|
945
|
-
return [amplitude, angularFrequency, phase, verticalOffset];
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
// e.g. rotate([1, 2, 3]) -> [2, 3, 1]
|
|
949
|
-
function rotate(array, n) {
|
|
950
|
-
n = typeof n === "undefined" ? 1 : n % array.length;
|
|
951
|
-
return array.slice(n).concat(array.slice(0, n));
|
|
952
|
-
}
|
|
953
|
-
function getLineEquation(first, second) {
|
|
954
|
-
if (approximateEqual(first[0], second[0])) {
|
|
955
|
-
return "x = " + first[0].toFixed(3);
|
|
956
|
-
}
|
|
957
|
-
const m = (second[1] - first[1]) / (second[0] - first[0]);
|
|
958
|
-
const b = first[1] - m * first[0];
|
|
959
|
-
return "y = " + m.toFixed(3) + "x + " + b.toFixed(3);
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
// Stolen from the wikipedia article
|
|
963
|
-
// http://en.wikipedia.org/wiki/Line-line_intersection
|
|
964
|
-
function getLineIntersection(
|
|
965
|
-
// TODO(LP-10725): update these to be 2-tuples
|
|
966
|
-
firstPoints, secondPoints) {
|
|
967
|
-
const x1 = firstPoints[0][0];
|
|
968
|
-
const y1 = firstPoints[0][1];
|
|
969
|
-
const x2 = firstPoints[1][0];
|
|
970
|
-
const y2 = firstPoints[1][1];
|
|
971
|
-
const x3 = secondPoints[0][0];
|
|
972
|
-
const y3 = secondPoints[0][1];
|
|
973
|
-
const x4 = secondPoints[1][0];
|
|
974
|
-
const y4 = secondPoints[1][1];
|
|
975
|
-
const determinant = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
|
976
|
-
if (Math.abs(determinant) < 1e-9) {
|
|
977
|
-
// Lines are parallel
|
|
978
|
-
return null;
|
|
979
|
-
}
|
|
980
|
-
const x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / determinant;
|
|
981
|
-
const y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / determinant;
|
|
982
|
-
return [x, y];
|
|
983
|
-
}
|
|
984
|
-
function getLineIntersectionString(firstPoints, secondPoints) {
|
|
985
|
-
const intersection = getLineIntersection(firstPoints, secondPoints);
|
|
986
|
-
if (intersection === null) {
|
|
987
|
-
return "Lines are parallel";
|
|
988
|
-
}
|
|
989
|
-
const [x, y] = intersection;
|
|
990
|
-
return "Intersection: (" + x.toFixed(3) + ", " + y.toFixed(3) + ")";
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
var geometry = /*#__PURE__*/Object.freeze({
|
|
994
|
-
__proto__: null,
|
|
995
|
-
sign: sign,
|
|
996
|
-
ccw: ccw,
|
|
997
|
-
collinear: collinear,
|
|
998
|
-
intersects: intersects,
|
|
999
|
-
polygonSidesIntersect: polygonSidesIntersect,
|
|
1000
|
-
vector: vector,
|
|
1001
|
-
reverseVector: reverseVector,
|
|
1002
|
-
clockwise: clockwise,
|
|
1003
|
-
magnitude: magnitude,
|
|
1004
|
-
angleMeasures: angleMeasures,
|
|
1005
|
-
similar: similar,
|
|
1006
|
-
lawOfCosines: lawOfCosines,
|
|
1007
|
-
canonicalSineCoefficients: canonicalSineCoefficients,
|
|
1008
|
-
rotate: rotate,
|
|
1009
|
-
getLineEquation: getLineEquation,
|
|
1010
|
-
getLineIntersection: getLineIntersection,
|
|
1011
|
-
getLineIntersectionString: getLineIntersectionString
|
|
1012
|
-
});
|
|
1013
|
-
|
|
1014
|
-
// This file contains helper functions for working with angles.
|
|
1015
|
-
function convertDegreesToRadians(degrees) {
|
|
1016
|
-
return degrees / 180 * Math.PI;
|
|
1017
|
-
}
|
|
1018
|
-
function convertRadiansToDegrees(radians) {
|
|
1019
|
-
const degree = radians / Math.PI * 180;
|
|
1020
|
-
// Account for floating point errors.
|
|
1021
|
-
return Number(degree.toPrecision(15));
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
// Returns a value between -180 and 180, inclusive. The angle is measured
|
|
1025
|
-
// between the positive x-axis and the given vector.
|
|
1026
|
-
function calculateAngleInDegrees([x, y]) {
|
|
1027
|
-
return Math.atan2(y, x) * 180 / Math.PI;
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
// Converts polar coordinates to cartesian. The th(eta) parameter is in degrees.
|
|
1031
|
-
function polar(r, th) {
|
|
1032
|
-
if (typeof r === "number") {
|
|
1033
|
-
r = [r, r];
|
|
1034
|
-
}
|
|
1035
|
-
th = th * Math.PI / 180;
|
|
1036
|
-
return [r[0] * Math.cos(th), r[1] * Math.sin(th)];
|
|
1037
|
-
}
|
|
1038
|
-
// This function calculates the angle between two points and an optional vertex.
|
|
1039
|
-
// If the vertex is not provided, the angle is measured between the two points.
|
|
1040
|
-
// This does not account for reflex angles or clockwise position.
|
|
1041
|
-
const getAngleFromVertex = (point, vertex) => {
|
|
1042
|
-
const x = point[0] - vertex[0];
|
|
1043
|
-
const y = point[1] - vertex[1];
|
|
1044
|
-
if (!x && !y) {
|
|
1045
|
-
return 0;
|
|
1046
|
-
}
|
|
1047
|
-
return (180 + Math.atan2(-y, -x) * 180 / Math.PI + 360) % 360;
|
|
1048
|
-
};
|
|
1049
|
-
|
|
1050
|
-
// This function calculates the clockwise angle between three points,
|
|
1051
|
-
// and is used to generate the labels and equation strings of the
|
|
1052
|
-
// current angle for the interactive graph.
|
|
1053
|
-
const getClockwiseAngle = (coords, allowReflexAngles = false) => {
|
|
1054
|
-
const coordsCopy = [...coords];
|
|
1055
|
-
// The coords are saved as [point1, vertex, point2] in the interactive graph
|
|
1056
|
-
const areClockwise = clockwise([coordsCopy[0], coordsCopy[2], coordsCopy[1]]);
|
|
1057
|
-
|
|
1058
|
-
// We may need to reverse the coordinates if we allow
|
|
1059
|
-
// reflex angles and the points are not in clockwise order.
|
|
1060
|
-
const shouldReverseCoords = !areClockwise || allowReflexAngles;
|
|
1061
|
-
|
|
1062
|
-
// Reverse the coordinates accordingly to ensure the angle is calculated correctly
|
|
1063
|
-
const clockwiseCoords = shouldReverseCoords ? coordsCopy.reverse() : coordsCopy;
|
|
1064
|
-
|
|
1065
|
-
// Calculate the angles between the two points and get the difference
|
|
1066
|
-
// between the two angles to get the clockwise angle.
|
|
1067
|
-
const startAngle = getAngleFromVertex(clockwiseCoords[0], clockwiseCoords[1]);
|
|
1068
|
-
const endAngle = getAngleFromVertex(clockwiseCoords[2], clockwiseCoords[1]);
|
|
1069
|
-
const angle = (startAngle + 360 - endAngle) % 360;
|
|
1070
|
-
return angle;
|
|
1071
|
-
};
|
|
1072
|
-
|
|
1073
|
-
var angles = /*#__PURE__*/Object.freeze({
|
|
1074
|
-
__proto__: null,
|
|
1075
|
-
convertDegreesToRadians: convertDegreesToRadians,
|
|
1076
|
-
convertRadiansToDegrees: convertRadiansToDegrees,
|
|
1077
|
-
calculateAngleInDegrees: calculateAngleInDegrees,
|
|
1078
|
-
polar: polar,
|
|
1079
|
-
getAngleFromVertex: getAngleFromVertex,
|
|
1080
|
-
getClockwiseAngle: getClockwiseAngle
|
|
1081
|
-
});
|
|
1082
|
-
|
|
1083
|
-
// TODO: there's another, very similar getSinusoidCoefficients function
|
|
1084
|
-
// they should probably be merged
|
|
1085
|
-
function getSinusoidCoefficients(coords) {
|
|
1086
|
-
// It's assumed that p1 is the root and p2 is the first peak
|
|
1087
|
-
const p1 = coords[0];
|
|
1088
|
-
const p2 = coords[1];
|
|
1089
|
-
|
|
1090
|
-
// Resulting coefficients are canonical for this sine curve
|
|
1091
|
-
const amplitude = p2[1] - p1[1];
|
|
1092
|
-
const angularFrequency = Math.PI / (2 * (p2[0] - p1[0]));
|
|
1093
|
-
const phase = p1[0] * angularFrequency;
|
|
1094
|
-
const verticalOffset = p1[1];
|
|
1095
|
-
return [amplitude, angularFrequency, phase, verticalOffset];
|
|
1096
|
-
}
|
|
1097
|
-
// TODO: there's another, very similar getQuadraticCoefficients function
|
|
1098
|
-
// they should probably be merged
|
|
1099
|
-
function getQuadraticCoefficients(coords) {
|
|
1100
|
-
const p1 = coords[0];
|
|
1101
|
-
const p2 = coords[1];
|
|
1102
|
-
const p3 = coords[2];
|
|
1103
|
-
const denom = (p1[0] - p2[0]) * (p1[0] - p3[0]) * (p2[0] - p3[0]);
|
|
1104
|
-
if (denom === 0) {
|
|
1105
|
-
// Many of the callers assume that the return value is always defined.
|
|
1106
|
-
// @ts-expect-error - TS2322 - Type 'undefined' is not assignable to type 'QuadraticCoefficient'.
|
|
1107
|
-
return;
|
|
1108
|
-
}
|
|
1109
|
-
const a = (p3[0] * (p2[1] - p1[1]) + p2[0] * (p1[1] - p3[1]) + p1[0] * (p3[1] - p2[1])) / denom;
|
|
1110
|
-
const b = (p3[0] * p3[0] * (p1[1] - p2[1]) + p2[0] * p2[0] * (p3[1] - p1[1]) + p1[0] * p1[0] * (p2[1] - p3[1])) / denom;
|
|
1111
|
-
const c = (p2[0] * p3[0] * (p2[0] - p3[0]) * p1[1] + p3[0] * p1[0] * (p3[0] - p1[0]) * p2[1] + p1[0] * p2[0] * (p1[0] - p2[0]) * p3[1]) / denom;
|
|
1112
|
-
return [a, b, c];
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
|
-
var coefficients = /*#__PURE__*/Object.freeze({
|
|
1116
|
-
__proto__: null,
|
|
1117
|
-
getSinusoidCoefficients: getSinusoidCoefficients,
|
|
1118
|
-
getQuadraticCoefficients: getQuadraticCoefficients
|
|
1119
|
-
});
|
|
1120
|
-
|
|
1121
|
-
export { KhanMath, angles, coefficients, geometry, libVersion, line, number, point, ray, sum, vector$1 as vector };
|
|
1122
|
-
//# sourceMappingURL=index.js.map
|