2011-03-18 59 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