Calculate min distance between a “line” and one “p

2019-06-14 14:07发布

I have a "linestring" (with init and end points) and a single "point" (two coordinates).

And I have implemented the following ActionSctipt code to use "haversine formula" to calculate the distance between two points (each point has x & y coordinates); this function can return the "distance" in "kms", "meters", "feets" or "miles":

private function distanceBetweenCoordinates(lat1:Number, lon1:Number, lat2:Number, lon2:Number, units:String = "miles"):Number {
    var R:int = RADIUS_OF_EARTH_IN_MILES;

    if (units == "km") {
        R = RADIUS_OF_EARTH_IN_KM;
    }

    if (units == "meters") {
        R = RADIUS_OF_EARTH_IN_M;
    }

    if (units == "feet") {
        R = RADIUS_OF_EARTH_IN_FEET;
    }

    var dLat:Number = (lat2 - lat1) * Math.PI / 180;

    var dLon:Number = (lon2 - lon1) * Math.PI / 180;

    var lat1inRadians:Number = lat1 * Math.PI / 180;

    var lat2inRadians:Number = lat2 * Math.PI / 180;

    var a:Number = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1inRadians) * Math.cos(lat2inRadians);

    var c:Number = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    var d:Number = R * c;

    return d;
}

This code is functioning well. But I need to improving this code to allow calculate the minimum distance between a "single point" and one "linestring" (with 2 points). How can I do?

I thought this solution: * Divide the "linesting" for each point (Init and end)... and for each of these calculate the distance to the "single point"... and after I getting both "distances" return the minimum distance. This solution is not the better, this is explained in the following image:

enter image description here

"d1" and "d2" distances are invalid... because only "d0" is the valid distance.

Please! help me!!! How can I improve the haversine formula to calculate the distance between a line and a single point in kilometres?

Thanks!!!!

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-14 14:39

In your case d0 distance is a height of triangle. It's Hb=2*A/b where A- Area & b-length of the base side (your linestring).

If given 3 points you can calculate the the distances between them (sides a, b, c of triangle). It will allow you to calculate triangle Area: A=sqrt(p*(p-a)*(p-b)*(p-c)) where p is half perimeter: p=(a+b+c)/2. So, now u have all variables u need to calculate the distance Hb (your "d0").

查看更多
登录 后发表回答