(zz)点在多边形内部判断算法(不限凹凸)

http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/

利用点和其中每个边的关系来判断

 

#define MIN(x,y) (x < y ? x : y)
#define MAX(x,y) (x > y ? x : y)
#define INSIDE 0
#define OUTSIDE 1
typedef struct {
   double x,y;
} Point;
int InsidePolygon(Point *polygon,int N,Point p)
{
  int counter = 0;
  int i;
  double xinters;
  Point p1,p2;
  p1 = polygon[0];
  for (i=1;i<=N;i++) {
    p2 = polygon[i % N];
    if (p.y > MIN(p1.y,p2.y)) {
      if (p.y <= MAX(p1.y,p2.y)) {
        if (p.x <= MAX(p1.x,p2.x)) {
          if (p1.y != p2.y) {
            xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
            if (p1.x == p2.x || p.x <= xinters)
              counter++;
          }
        }
      }
    }
    p1 = p2;
  }
  if (counter % 2 == 0)
    return(OUTSIDE);
  else
    return(INSIDE);
}
 

 

 


原文链接: http://blog.csdn.net/ccanan/article/details/5868022

你可能感兴趣的:((zz)点在多边形内部判断算法(不限凹凸))