UNPKG

reproject-line

Version:
88 lines (70 loc) 2.14 kB
function reproject_line(coords, reproject, options) { const debug_level = typeof options === "object" && typeof options.debug_level === "number" ? options.debug_level : 0; let densify = typeof options === "object" && typeof options.densify === "number" ? options.densify : 0; const strategy = typeof options === "object" && typeof options.strategy === "string" ? options.strategy : "auto"; // just in case densify isn't a round number densify = Math.round(densify); if (debug_level >= 1) { console.log("[reproject-line] debug_level:", debug_level); console.log("[reproject-line] densify:", densify); console.log("[reproject-line] strategy:", strategy); } // algorithm // drop point when the slope changes (and at the end) const out = []; let [xprev, yprev] = reproject(coords[0]); let mprev = null; let m = null; for (let i = 1; i < coords.length; i++) { const [x1, y1] = coords[i - 1]; const [x2, y2] = coords[i]; const xdist = x2 - x1; const ydist = y2 - y1; const xstep = xdist / (densify + 1); const ystep = ydist / (densify + 1); for (let ii = 1; ii <= densify; ii++) { const [rx, ry] = reproject([x1 + ii * xstep, y1 + ii * ystep]); m = (ry - yprev) / (rx - xprev); if (strategy === "always" || m !== mprev) { out.push([xprev, yprev]); mprev = m; } xprev = rx; yprev = ry; } // try with last coord in segment const [rx2, ry2] = reproject([x2, y2]); m = (ry2 - yprev) / (rx2 - xprev); // if slope changes, drop point if (strategy === "always" || m !== mprev) { out.push([xprev, yprev]); mprev = m; } xprev = rx2; yprev = ry2; } // drop last point out.push([xprev, yprev]); if (debug_level >= 3) { console.log("[reproject-line] returning", out); } return out; } if (typeof define === "function" && define.amd) { define(function () { return reproject_line; }); } if (typeof module === "object") { module.exports = reproject_line; module.exports.default = reproject_line; }