Distance from point to line on Earth

后端 未结 6 1094
傲寒
傲寒 2020-12-09 14:21

I need something as simple as

\"Subject 1.02: How do I find the distance from a point to a line?\"

But that works with Lon/Lat.

6条回答
  •  -上瘾入骨i
    2020-12-09 14:48

    Ilya Golota's answer is a correct approach but, turns out it always returns a really large number such as "3.0355243098445522e12" even for points that are 2 meters away from the line. I did this for computing the triangle area instead:

    double d0 = computeDistanceBetween(p0, p1);
    double d1 = computeDistanceBetween(p1, p2);
    double d2 = computeDistanceBetween(p2, p0);
    

    (Using Heron's formula)

    double area = Math.sqrt(halfP*(halfP-d0)*(halfP-d1)*(halfP-d2));
    

    And then plug in the area value here

    double distanceToLine = 2*area/computeDistanceBetween(line.startPoint, line.endPoint)
    

    This works well for me and returns the perpendicular distance from the point to the line in meters.

提交回复
热议问题