判斷一個座標是否在多邊形內部

在GIS(地理信息管理系統)中,判斷一個座標是否在多邊形內部是個常常要遇到的問題。乍聽起來還挺複雜。根據W. Randolph Franklin 提出的PNPoly算法,只需區區幾行代碼就解決了這個問題。
 
假設多邊形的座標存放在一個數組裏,首先咱們須要取得該數組在橫座標和縱座標的最大值和最小值,根據這四個點算出一個四邊型,首先判斷目標座標點是否在這個四邊型以內,若是在這個四邊型以外,那能夠跳事後面較爲複雜的計算,直接返回false。
 
if (p.x < minX || p.x > maxX || p.y < minY || p.y > maxY) {
     // 這個測試都過不了。。。直接返回false;
}
接下來是核心算法部分:
 
int pnpoly (int nvert, float *vertx, float *verty, float testx, float testy) {
    int i, j, c = 0;
    for (i = 0, j = nvert-1; i < nvert; j = i++) {
        if ( ( (verty[i]>testy) != (verty[j]>testy) ) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
            c = !c;
    }
    return c;
}
 
額,代碼就這麼簡單,但到底啥意思呢:
 
首先,參數nvert 表明多邊形有幾個點。浮點數testx, testy表明待測試點的橫座標和縱座標,*vertx,*verty分別指向儲存多邊形橫縱座標數組的首地址。
咱們注意到,每次計算都涉及到相鄰的兩個點和待測試點,而後考慮兩個問題:
1. 被測試點的縱座標testy是否在本次循環所測試的兩個相鄰點縱座標範圍以內?即
verty[i] <testy < verty[j]
或者
verty[j] <testy < verty[i]
2. 待測點test是否在i,j兩點之間的連線之下?看不懂後半短if statement的朋友請自行在紙上寫下i,j兩點間的斜率公式,要用到一點初中解析幾何和不等式的知識範疇,對廣大碼農來講小菜一碟。
而後每次這兩個條件同時知足的時候咱們把返回的布爾量取反。
可這究竟是啥意思啊?
這個表達式的意思是說,隨便畫個多邊形,隨便定一個點,而後經過這個點水平劃一條射線,先數數看這條
射線和多邊形的邊相交幾回,(或者說先排除那些不相交的邊,第一個判斷條件),而後再數這條射線穿越多邊形的次數是否爲奇數,若是是奇數,那麼該點在多邊形內,若是是偶數,則在多邊形外。詳細的數學證實這裏就不作了,不過讀者能夠自行畫多邊形進行驗證。
 
以上轉載百度知道;如下轉載阿凡盧,轉載地址:http://www.cnblogs.com/luxiaoxun/p/3722358.html
 

判斷點是否在多邊形內部

 

如何判斷一個點是否在多邊形內部?html

(1)面積和判別法:判斷目標點與多邊形的每條邊組成的三角形面積和是否等於該多邊形,相等則在多邊形內部。算法

(2)夾角和判別法:判斷目標點與全部邊的夾角和是否爲360度,爲360度則在多邊形內部。數組

(3)引射線法:從目標點出發引一條射線,看這條射線和多邊形全部邊的交點數目。若是有奇數個交點,則說明在內部,若是有偶數個交點,則說明在外部。ide

具體作法:將測試點的Y座標與多邊形的每個點進行比較,會獲得一個測試點所在的行與多邊形邊的交點的列表。在下圖的這個例子中有8條邊與測試點所在的行相交,而有6條邊沒有相交。若是測試點的兩邊點的個數都是奇數個則該測試點在多邊形內,不然在多邊形外。在這個例子中測試點的左邊有5個交點,右邊有三個交點,它們都是奇數,因此點在多邊形內。post

算法圖解:測試

關於這個算法的具體的更多圖形例子:http://alienryderflex.com/polygon/flex

參考代碼:this

複製代碼
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
  int i, j, c = 0;
  for (i = 0, j = nvert-1; i < nvert; j = i++) 
  {
    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
       c = !c;
  }
  return c;
}
複製代碼

來自一個polygon的內部實現:url

複製代碼
      public bool IsInside(PointLatLng p)
      {
         int count = Points.Count;

         if(count < 3)
         {
            return false;
         }

         bool result = false;

         for(int i = 0, j = count - 1; i < count; i++)
         {
            var p1 = Points[i];
            var p2 = Points[j];

            if(p1.Lat < p.Lat && p2.Lat >= p.Lat || p2.Lat < p.Lat && p1.Lat >= p.Lat)
            {
               if(p1.Lng + (p.Lat - p1.Lat) / (p2.Lat - p1.Lat) * (p2.Lng - p1.Lng) < p.Lng)
               {
                  result = !result;
               }
            }
            j = i;
         }
         return result;
      }
複製代碼

特殊狀況:要檢測的點在多變形的一條邊上,射線法判斷的結果是不肯定的,須要特殊處理(If the test point is on the border of the polygon, this algorithm will deliver unpredictable results)。spa

計算一個多邊形的面積(area of a polygon):

複製代碼
        private static double SignedPolygonArea(List<PointLatLng> points)
        {
            // Add the first point to the end.
            int pointsCount = points.Count;
            PointLatLng[] pts = new PointLatLng[pointsCount + 1];
            points.CopyTo(pts, 0);
            pts[pointsCount] = points[0];

            for (int i = 0; i < pointsCount + 1; ++i)
            {
                pts[i].Lat = pts[i].Lat * (System.Math.PI * 6378137 / 180);
                pts[i].Lng = pts[i].Lng * (System.Math.PI * 6378137 / 180);
            }

            // Get the areas.
            double area = 0;
            for (int i = 0; i < pointsCount; i++)
            {
                area += (pts[i + 1].Lat - pts[i].Lat) * (pts[i + 1].Lng + pts[i].Lng) / 2;
            }

            // Return the result.
            return area;
        }

        /// <summary>
        /// Get the area of a polygon
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        public static double GetPolygonArea(List<PointLatLng> points)
        {
            // Return the absolute value of the signed area.
            // The signed area is negative if the polygon is oriented clockwise.
            return Math.Abs(SignedPolygonArea(points));
        }
複製代碼

 

 

 

參考資料:

http://alienryderflex.com/polygon/

http://en.wikipedia.org/wiki/Point_in_polygon

http://www.codeproject.com/Tips/84226/Is-a-Point-inside-a-Polygon

 

做者: 阿凡盧
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。
相關文章
相關標籤/搜索