找到最接近第三点的线上的一个点 [英] find a point on a line closest to a third point javascript

查看:109
本文介绍了找到最接近第三点的线上的一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在离线最近的第三个点上找到一个点。点是纬度/经度。

简单的图形显示了我想要达到的目标。我使用它的JavaScript,但任何语言或公式仍然可以工作。我知道这是基本的几何图形,但我仍然无法在谷歌上找到一个公式:S lol ...留在学校!

  var a = '48,-90'; 
var b = '49,-92';
var c = '48 .25,-91.8';
var d ='在线计算的点';

解决方案

RCrowe @ 在最接近latlng的折线中找到一个点

  / * desc静态功能。找到最近测试点上的点
测试点pXy带有属性.x和.y
由数组aXys定义的行,节点具有属性.x和.y
return是带有.x的对象, .y属性和属性i表示aXys
中的最近段和属性f从aXy [i-1]
和属性fTo返回点的分数距离aXy [i ] * /


函数getClosestPointOnLines(pXy,aXys){

var minDist;
var fTo;
var fFrom;
var x;
var y;
var i;
var dist;如果(aXys.length> 1){

for(var n = 1; n< aXys.length; n ++){
$ b $ (aXys [n] .x!= aXys [n-1] .x){
var a =(aXys [n] .y - aXys [n - 1] .y)/(aXys [n ] .x - aXys [n - 1] .x);
var b = aXys [n] .y - a * aXys [n] .x;
dist = Math.abs(a * pXy.x + b - pXy.y)/ Math.sqrt(a * a + 1);
}
else
dist = Math.abs(pXy.x - aXys [n] .x)

//线段的长度^ 2
var rl2 = Math.pow(aXys [n] .y - aXys [n - 1] .y,2)+ Math.pow(aXys [n] .x - aXys [n - 1] .x,2); (aXys [n] .y - pXy.y,2)+ Math.pow(aXys [
$ b // b距离pt到结束线段的距离^ b $ b var ln2 = Math.pow n] .x - pXy.x,2);

//距离^ 2开始线段
var lnm12 = Math.pow(aXys [n - 1] .y - pXy.y,2)+ Math.pow( aXys [n-1] .x - pXy.x,2);

//最小距离^ 2到pt到无穷线
var dist2 = Math.pow(dist,2);

//计算线段长度^ 2
var calcrl2 = ln2 - dist2 + lnm12 - dist2;

//根据需要重新定义到线段的最小距离(不是无限长线)
if(calcrl2> rl2)
dist = Math.sqrt(Math.min(ln2, lnm12)); ((minDist == null)||(minDist> dist)){
if(calcrl2> r12){
if(lnm12< ln2){
fTo = 0; //接近前一点
fFrom = 1;
}
else {
fFrom = 0; //接近当前点
fTo = 1;


else {
//从点垂直与线段相交
fTo =((Math.sqrt(lnm12 - dist2))/ Math.sqrt( RL2));
fFrom =((Math.sqrt(ln2-dist2))/ Math.sqrt(r12));
}
minDist = dist;
i = n;
}
}

var dx = aXys [i - 1] .x - aXys [i] .x;
var dy = aXys [i - 1] .y - aXys [i] .y;

x = aXys [i - 1] .x - (dx * fTo);
y = aXys [i - 1] .y - (dy * fTo);


$ b return {'x':x,'y':y,'i':我,'fTo':fTo,'fFrom':fFrom};
}


I'm trying to find a point on a line closest to a third point off of the line. The points are latitude/longitude.

The simple graphic shows what I'm trying to achieve. I'm using it for javascript, but any language or formula would still work. I know this is basic geometry, but I'm still having trouble finding a formula on google :S lol... stay in school!

var a = '48,-90';
var b = '49,-92';
var c = '48.25,-91.8';
var d = 'calculated point on line';

解决方案

RCrowe @ Find a point in a polyline which is closest to a latlng

/* desc Static function. Find point on lines nearest test point
   test point pXy with properties .x and .y
   lines defined by array aXys with nodes having properties .x and .y 
   return is object with .x and .y properties and property i indicating nearest segment in aXys 
   and property fFrom the fractional distance of the returned point from aXy[i-1]
   and property fTo the fractional distance of the returned point from aXy[i]   */


function getClosestPointOnLines(pXy, aXys) {

    var minDist;
    var fTo;
    var fFrom;
    var x;
    var y;
    var i;
    var dist;

    if (aXys.length > 1) {

        for (var n = 1 ; n < aXys.length ; n++) {

            if (aXys[n].x != aXys[n - 1].x) {
                var a = (aXys[n].y - aXys[n - 1].y) / (aXys[n].x - aXys[n - 1].x);
                var b = aXys[n].y - a * aXys[n].x;
                dist = Math.abs(a * pXy.x + b - pXy.y) / Math.sqrt(a * a + 1);
            }
            else
                dist = Math.abs(pXy.x - aXys[n].x)

            // length^2 of line segment 
            var rl2 = Math.pow(aXys[n].y - aXys[n - 1].y, 2) + Math.pow(aXys[n].x - aXys[n - 1].x, 2);

            // distance^2 of pt to end line segment
            var ln2 = Math.pow(aXys[n].y - pXy.y, 2) + Math.pow(aXys[n].x - pXy.x, 2);

            // distance^2 of pt to begin line segment
            var lnm12 = Math.pow(aXys[n - 1].y - pXy.y, 2) + Math.pow(aXys[n - 1].x - pXy.x, 2);

            // minimum distance^2 of pt to infinite line
            var dist2 = Math.pow(dist, 2);

            // calculated length^2 of line segment
            var calcrl2 = ln2 - dist2 + lnm12 - dist2;

            // redefine minimum distance to line segment (not infinite line) if necessary
            if (calcrl2 > rl2)
                dist = Math.sqrt(Math.min(ln2, lnm12));

            if ((minDist == null) || (minDist > dist)) {
                if (calcrl2 > rl2) {
                    if (lnm12 < ln2) {
                        fTo = 0;//nearer to previous point
                        fFrom = 1;
                    }
                    else {
                        fFrom = 0;//nearer to current point
                        fTo = 1;
                    }
                }
                else {
                    // perpendicular from point intersects line segment
                    fTo = ((Math.sqrt(lnm12 - dist2)) / Math.sqrt(rl2));
                    fFrom = ((Math.sqrt(ln2 - dist2)) / Math.sqrt(rl2));
                }
                minDist = dist;
                i = n;
            }
        }

        var dx = aXys[i - 1].x - aXys[i].x;
        var dy = aXys[i - 1].y - aXys[i].y;

        x = aXys[i - 1].x - (dx * fTo);
        y = aXys[i - 1].y - (dy * fTo);

    }

    return { 'x': x, 'y': y, 'i': i, 'fTo': fTo, 'fFrom': fFrom };
}

这篇关于找到最接近第三点的线上的一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆