比较完美的点在多边行内得判断(c++)

来源:互联网 发布:mac版本不支持银联 编辑:程序博客网 时间:2024/06/06 05:23

inline int
isLeft( CPoint *P0, CPoint *P1, CPoint *P2 )
{
    return ( (P1->x - P0->x) * (P2->y - P0->y)
            - (P2->x - P0->x) * (P1->y - P0->y) );

}
bool CPointInPolygonDlg::PointPolygonAlgorithm(CPoint *pt)
{
    int    wn = 0;    // the winding number counter
 int j=0;
 std::vector<CPoint *>::iterator it;
    // loop through all edges of the polygon
    for (it=vPolygon.begin(); it<vPolygon.end()-1; it++)// edge from V[i] to V[i+1]
 {
  j++;
        if ((*(it))->y <= pt->y) {         // start y <= pt->y
            if ((*(it+1))->y > pt->y)      // an upward crossing
                if (isLeft( *it, *(it+1), pt) > 0)  // P left of edge
                    ++wn;            // have a valid up intersect
        }
        else {                       // start y > P.y (no test needed)
            if ((*(it+1))->y <= pt->y)     // a downward crossing
                if (isLeft( *it, *(it+1), pt) < 0)  // P right of edge
                    --wn;            // have a valid down intersect
        }
    }
    if (wn==0)
  return false;
 return true;
}

 

原创粉丝点击