作者:一蓝
链接:https:
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
bool pointInRegion(cocos2d::CCPoint pt,vector<cocos2d::CCPoint> plist)
{
int nCross = 0;
for (int i = 0; i < plist.size(); i++) {
cocos2d::CCPoint p1;
cocos2d::CCPoint p2;
p1 = plist[i];
p2 = plist[(i+1)%plist.size()];
if ( p1.y == p2.y )
continue;
if ( pt.y < min(p1.y, p2.y))
continue;
if ( pt.y >= max(p1.y, p2.y))
continue;
double x = (double)(pt.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
if ( x > pt.x )
nCross++;
}
if (nCross % 2 == 1) {
return true;
}
else {
return false;
}
}