Spaces:
Running
Running
File size: 1,844 Bytes
4ee4376 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
//
//
//
function wrap (value, min, max) {
if (value > max)
value = min;
if (value < min)
value = max;
return value;
}
//
//
//
function isNumberBetween (value, min, max) {
return value >= min && value <= max;
}
//
// adapted from https://github.com/lovasoa/graham-fast
// licensed under the MIT license
//
function polygonUnion(poly1, poly2) {
let points = [];
for (let i = 0; i < poly1.length; i++)
points.push([poly1[i].x, poly1[i].y]);
for (let i = 0; i < poly2.length; i++)
points.push([poly2[i].x, poly2[i].y]);
// The enveloppe is the points themselves
if (points.length <= 3)
return points;
// Find the pivot
let pivot = points[0];
for (let i = 0; i < points.length; i++)
if (points[i][1] < pivot[1] || (points[i][1] === pivot[1] && points[i][0] < pivot[0]))
pivot = points[i];
// Attribute an angle to the points
for (let i = 0; i < points.length; i++)
points[i]._graham_angle = Math.atan2(points[i][1] - pivot[1], points[i][0] - pivot[0]);
points.sort(function(a, b){
return a._graham_angle === b._graham_angle ? a[0] - b[0] : a._graham_angle - b._graham_angle;
});
// Adding points to the result if they "turn left"
let result = [points[0]];
let len = 1;
for (let i = 1; i < points.length; i++) {
let a = result[len-2];
let b = result[len-1];
let c = points[i];
while ((len === 1 && b[0] === c[0] && b[1] === c[1]) || (len > 1 && (b[0]-a[0]) * (c[1]-a[1]) <= (b[1]-a[1]) * (c[0]-a[0]))) {
len--;
b = a;
a = result[len-2];
}
result[len++] = c;
}
result.length = len;
// create new polygon object
let polygon = [];
for (let i = 0; i < result.length; i++)
polygon.push({ x: result[i][0], y: result[i][1] });
return polygon;
}
export { wrap, isNumberBetween, polygonUnion }; |