如何找到两个或多个多边形之间的最小距离?

时间:2013-02-06 14:55:46

标签: java javascript google-api

我想找到两个多边形之间的最小距离。但我找到了一些问题。我是谷歌地图的新手,你能帮助我吗?我有那些坐标可供使用;

    polygonArray = [[
       new google.maps.LatLng(40.56389453066509,33.739013671875),
       new google.maps.LatLng(40.39676430557206,32.135009765625),
       new google.maps.LatLng(39.87601941962116,36.046142578125)
    ]];

提前谢谢。

3 个答案:

答案 0 :(得分:3)

您需要一个几何库来计算两点之间的距离,多边形的中心等。看看这个other question

答案 1 :(得分:3)

如果您决定编写自己的代码:

对于2个非重叠多边形,最简单的代码(从编程的角度来看)将迭代一个多边形的点并找到一个多边形中的每个点与第二个多边形中的每一行之间的距离,然后执行相同的操作对于第二个多边形(每个点到每一行)。

如果您不确定多边形是否重叠,还必须检查一个多边形中的每条线是否都不跨越第二个多边形中的任何其他线(仅需要对1个多边形执行此操作)。

答案 2 :(得分:0)

下面的代码部分地解决了该问题,但是仍然需要通过检测更靠近另一个多边形的边缘上的点来进行增强,因为该部分仅适用于顶点

        static double solve(List<List<Integer>> p, List<List<Integer>> q) {
        double minLength = 100000000;
        for (List<Integer> qq : q) {
            for (List<Integer> pp : p) {
                double current = distanceBTWPoints(pp, qq);
                if (current < minLength) {
                    minLength = current;
                }
            }
        }
        System.out.println(minLength);
        return minLength;
    }

    static double distanceBTWPoints(List<Integer> p, List<Integer> q) {
        double xDiff = q.get(0) - p.get(0);
        double yDiff = q.get(1) - p.get(1);
//        if(xDiff==0 || yDiff==0)
//            return 0;
        System.out.println(+p.get(0) + "  " + q.get(0) + "  " + p.get(1) + "  " + q.get(1) + " x:" + xDiff + "  y: " + yDiff + " p: " + ((xDiff * xDiff) + (yDiff * yDiff)) + " VAl= " + Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
//        System.out.println("p.get(0): "+p.get(0) +"  q.get(0) :"+q.get(0) +"  p.get(1):  "+p.get(1) +"  q.get(1): "+q.get(1) +" xDiff: " + xDiff + "  yDiff: " + yDiff + " plus: "+((xDiff * xDiff) + (yDiff * yDiff))+ " VAl= " + Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
        System.out.println("--" + Math.hypot(xDiff, yDiff));
        System.out.println("--" + Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
        return Math.hypot(xDiff, yDiff);
    }