v1.6.03 - linear ruler: getSegmentId

This commit is contained in:
Azgaar 2021-03-05 13:54:52 +03:00
parent e6a79f540d
commit ee324a3fef
4 changed files with 47 additions and 3 deletions

View file

@ -358,6 +358,38 @@ void function addFindAll() {
}
}()
// get segment of any point on polyline
function getSegmentId(points, point) {
if (points.length === 2) return 1;
const d2 = (p1, p2) => (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2;
let minSegment = 1;
let minDist = Infinity;
for (let i=0; i < points.length-1; i++) {
const p1 = points[i];
const p2 = points[i+1];
const length = Math.sqrt(d2(p1, p2));
const segments = Math.ceil(length / 10);
const dx = (p2[0] - p1[0]) / segments;
const dy = (p2[1] - p1[1]) / segments;
for (let s=0; s < segments; s++) {
const x = p1[0] + s * dx;
const y = p1[1] + s * dy;
const dist2 = d2(point, [x, y]);
if (dist2 >= minDist) continue;
minDist = dist2;
minSegment = i+1;
}
}
console.log(minSegment);
return minSegment;
}
// normalization function
function normalize(val, min, max) {
return Math.min(Math.max((val - min) / (max - min), 0), 1);