2011-03-18 99 views
3

我需要在C#中實現地理柵欄。 Geofence區域可以是圓形,矩形,多邊形等。有沒有人在C#中使用Geofence實現?我發現Geo Fencing - point inside/outside polygon。但是,它只支持多邊形。實現地理柵欄 - C#

+0

多邊形trivially包含矩形作爲一種特殊情況(假設您設法定義球體上的矩形實際上是什麼)。圓圈可以用畢達哥拉斯定理來檢查。 – CodesInChaos 2011-03-18 13:26:53

回答

1

請參閱我的執行情況:

Polygon

Circle

+0

這是你的圈子的實現,我dint找到你的名字的任何答案 – ShivaPrasad 2013-10-15 13:00:45

6

我已經測試了各種實現方式和這個例子正常工作對我來說:

Example

public static bool PolyContainsPoint(List<Point> points, Point p) { 
    bool inside = false; 

    // An imaginary closing segment is implied, 
    // so begin testing with that. 
    Point v1 = points[points.Count - 1]; 

    foreach (Point v0 in points) 
    { 
     double d1 = (p.Y - v0.Y) * (v1.X - v0.X); 
     double d2 = (p.X - v0.X) * (v1.Y - v0.Y); 

     if (p.Y < v1.Y) 
     { 
      // V1 below ray 
      if (v0.Y <= p.Y) 
      { 
       // V0 on or above ray 
       // Perform intersection test 
       if (d1 > d2) 
       { 
        inside = !inside; // Toggle state 
       } 
      } 
     } 
     else if (p.Y < v0.Y) 
     { 
      // V1 is on or above ray, V0 is below ray 
      // Perform intersection test 
      if (d1 < d2) 
      { 
       inside = !inside; // Toggle state 
      } 
     } 

     v1 = v0; //Store previous endpoint as next startpoint 
    } 

    return inside; 
} 
+1

偉大的答案Fnascimento! – 2014-08-01 13:47:24