1 | function fnNumberSort (a, b) {
|
2 | return a - b
|
3 | }
|
4 |
|
5 | function fnSortByIndex (a, b) {
|
6 | return a.index - b.index
|
7 | }
|
8 |
|
9 | const IsFloat = function (n) {
|
10 | return (!isNaN(n)) || (n === Infinity) || (n === -Infinity)
|
11 | }
|
12 |
|
13 | const solve2Linear = function (a, b, c, d, u, v) {
|
14 | let det = a * d - b * c
|
15 | let invdet = 1.0 / det
|
16 | let x = u * d - b * v
|
17 | let y = -u * c + a * v
|
18 | x *= invdet
|
19 | y *= invdet
|
20 | return [x, y]
|
21 | }
|
22 |
|
23 | function insertSorted (array, element, comparefunc) {
|
24 | let leftbound = 0
|
25 | let rightbound = array.length
|
26 | while (rightbound > leftbound) {
|
27 | let testindex = Math.floor((leftbound + rightbound) / 2)
|
28 | let testelement = array[testindex]
|
29 | let compareresult = comparefunc(element, testelement)
|
30 | if (compareresult > 0)
|
31 | {
|
32 | leftbound = testindex + 1
|
33 | } else {
|
34 | rightbound = testindex
|
35 | }
|
36 | }
|
37 | array.splice(leftbound, 0, element)
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const interpolateBetween2DPointsForY = function (point1, point2, y) {
|
44 | let f1 = y - point1.y
|
45 | let f2 = point2.y - point1.y
|
46 | if (f2 < 0) {
|
47 | f1 = -f1
|
48 | f2 = -f2
|
49 | }
|
50 | let t
|
51 | if (f1 <= 0) {
|
52 | t = 0.0
|
53 | } else if (f1 >= f2) {
|
54 | t = 1.0
|
55 | } else if (f2 < 1e-10) {
|
56 | t = 0.5
|
57 | } else {
|
58 | t = f1 / f2
|
59 | }
|
60 | let result = point1.x + t * (point2.x - point1.x)
|
61 | return result
|
62 | }
|
63 |
|
64 | module.exports = {
|
65 | fnNumberSort,
|
66 | fnSortByIndex,
|
67 | IsFloat,
|
68 | solve2Linear,
|
69 | insertSorted,
|
70 | interpolateBetween2DPointsForY
|
71 | }
|