add array grouping and walk order. add new thin fill output method

This commit is contained in:
Stewart Allen 2021-12-30 17:35:10 -05:00
commit 5e6c4b91aa
4 changed files with 81 additions and 2 deletions

View file

@ -428,6 +428,39 @@
return diff;
}
// order array of elements using next closest element comparator
// used for things like next-closest point walks (fdm thin fill)
// in future, replace poly2poly and similar with this
function orderClosest(array, fn, from) {
if (!array.length) {
return array;
}
let out = new Array(array.length);
let outi = 0;
let root = 0;
if (!from) {
from = array[root++];
out[outi++] = from;
}
for (;;) {
let best, best_i, best_dist = Infinity;
for (let i=root; i<array.length; i++) {
let el = array[i];
if (!el) continue;
let dist = fn(from, el);
if (dist < best_dist) {
best_dist = dist;
best_i = i;
best = el;
}
}
if (!best) break;
array[best_i] = undefined;
from = out[outi++] = best;
}
return out;
}
/** ******************************************************************
* Connect to base
******************************************************************* */
@ -530,6 +563,7 @@
intersect,
determinant,
numOrDefault,
orderClosest,
zInPlane,
comma: (v) => {
if (!v) return v;