判斷點在多邊形內部

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) //testx,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;
}
轉自 http://www.javashuo.com/article/p-aektebzd-bg.html
相關文章
相關標籤/搜索