1

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

    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)
    ]];

先感谢您。

4

3 回答 3

3

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

于 2013-02-06T14:59:47.000 回答
3

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

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

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

于 2013-02-06T15:12:21.103 回答
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);
    }
于 2018-09-21T21:23:26.860 回答