UNPKG

1.62 kBJavaScriptView Raw
1function fnNumberSort (a, b) {
2 return a - b
3}
4
5function fnSortByIndex (a, b) {
6 return a.index - b.index
7}
8
9const IsFloat = function (n) {
10 return (!isNaN(n)) || (n === Infinity) || (n === -Infinity)
11}
12
13const 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
23function 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) // element > testelement
31 {
32 leftbound = testindex + 1
33 } else {
34 rightbound = testindex
35 }
36 }
37 array.splice(leftbound, 0, element)
38}
39
40// Get the x coordinate of a point with a certain y coordinate, interpolated between two
41// points (CSG.Vector2D).
42// Interpolation is robust even if the points have the same y coordinate
43const 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) { // FIXME Should this be CSG.EPS?
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
64module.exports = {
65 fnNumberSort,
66 fnSortByIndex,
67 IsFloat,
68 solve2Linear,
69 insertSorted,
70 interpolateBetween2DPointsForY
71}