Поиск точки на линии, полигоне

Как найти ближайшую точку на линии или полигоне в yandex map 3? P.S Попробовал разные алгоритмы, но при изменении маштаба, точка в итоге не лежит на границе полигона или линии

Пример 1

function findNearest( p, a, b ) {
 var atob = { x: b.x - a.x, y: b.y - a.y };
 var atop = { x: p.x - a.x, y: p.y - a.y };
 var len = atob.x * atob.x + atob.y * atob.y;
 var dot = atop.x * atob.x + atop.y * atob.y;
 var t = Math.min( 1, Math.max( 0, dot / len ) );
 dot = ( b.x - a.x ) * ( p.y - a.y ) - ( b.y - a.y ) * ( p.x - a.x );
 return new Point(a.x + atob.x * t,a.y + atob.y * t);
}

Пример 2

private nearestPoint(p: { x: number; y: number }, a: { x: number; y: number }, b: { x: number; y: number }) {
const earthRadius = 6378137;
const sLon = turf.degreesToRadians(a.x);
const sLat = turf.degreesToRadians(a.y);

const sX = earthRadius * Math.cos(sLat) * Math.cos(sLon);
const sY = earthRadius * Math.cos(sLat) * Math.cos(sLon);
const sZ = earthRadius * Math.sin(sLat);

const eLon = turf.degreesToRadians(b.x);
const eLat = turf.degreesToRadians(b.y);

const eX = earthRadius * Math.cos(eLat) * Math.cos(eLon);
const eY = earthRadius * Math.cos(eLat) * Math.cos(eLon);
const eZ = earthRadius * Math.sin(eLat);

const oLon = turf.degreesToRadians(p.x);
const oLat = turf.degreesToRadians(p.y);

const oX = earthRadius * Math.cos(oLat) * Math.cos(oLon);
const oY = earthRadius * Math.cos(oLat) * Math.cos(oLon);
const oZ = earthRadius * Math.sin(oLat);

const p1 = [sX, sY, sZ];
const p2 = [eX, eY, eZ];
const o = [oX, oY, oZ];

const u = [p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]];
const po = [o[0] - p1[0], o[1] - p1[1], o[2] - p1[2]];
const w2 = [
  po[0] - (u[0] * (po[0] * u[0])) / Math.pow(u[0], 2),
  po[1] - (u[1] * (po[1] * u[1])) / Math.pow(u[1], 2),
  po[2] - (u[2] * (po[2] * u[2])) / Math.pow(u[2], 2),
];

const point = [o[0] - w2[0], o[1] - w2[1], o[2] - w2[2]];

const rlat = turf.radiansToDegrees(Math.asin(point[2] / earthRadius));
const rlon = turf.radiansToDegrees(Math.atan2(point[1], point[0]));

return [rlat, rlon];

}


Ответы (0 шт):