MarchingSquares.js
MarchingSquares.js copied to clipboard
IsoLines does not trace lines, if multiple isolines are not closed.
Minimal non-working example:
MarchingSquaresJS.isoLines([[1,1,1,1],[0,0,0,0],[1,1,1,1]],0.5,{linearRing:false});
This should return two open isolines. Instead, the first isoline is connected, the second one is left in small segments without connecting them.
I run into the same issue with isoLines in many small segments. This does not work well, or look well, when drawing paths in dashArray style... I did some experimenting with below function to combine arrays that seems to be connected. Works well in my case as a workaround :
function mergeConnectedPaths(pointArrays) {
// Helper function to check if two arrays are connected
function areConnected(array1, array2) {
const end1 = array1[array1.length - 1];
const start2 = array2[0];
return end1[0] === start2[0] && end1[1] === start2[1];
}
const mergedPaths: number[][] = [];
pointArrays.forEach((points) => {
let merged = false;
for (let i = 0; i < mergedPaths.length; i++) {
const path = mergedPaths[i];
if (areConnected(path, points)) {
// Merge current points at the end of the existing path
mergedPaths[i] = path.concat(points.slice(1));
merged = true;
break;
} else if (areConnected(points, path)) {
// Merge existing path at the end of the current points
mergedPaths[i] = points.concat(path.slice(1));
merged = true;
break;
}
}
if (!merged) {
mergedPaths.push(points);
}
});
return mergedPaths;
}