簡體   English   中英

如何找到兩個或多個多邊形之間的最小距離?

[英]how can i find the minimum distance between two or more polygons?

我想找到兩個多邊形之間的最小距離。 但是我發現它有些困難。 我是Google地圖的新手,所以可以幫我嗎? 我有那些坐標可以使用;

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

先感謝您。

您需要一個幾何庫來計算兩個點之間的距離,多邊形的中心等。看看另一個問題

如果您決定編寫自己的代碼:

對於2個不重疊的多邊形,最簡單的代碼(從編程角度出發)將迭代一個多邊形的點,並找到一個多邊形中每個點與第二個多邊形中每個線之間的距離,然后對第二個多邊形執行相同的操作多邊形(每個點都指向每條線)。

如果不確定多邊形是否重疊,則還必須檢查一個多邊形中的每條線是否不與第二個多邊形中的任何其他線相交(只需要對1個多邊形執行此操作)。

下面的代碼部分地解決了該問題,但是仍然需要通過檢測更靠近另一個多邊形的邊上的點來進行增強,因為該部分僅在頂點上起作用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM