bool Intersection::pointInPolygon(const cocos2d::Vec2& pos, const std::vectorcocos2d::Vec2& polygon)
{
bool inside = false;
auto x = pos.x;
auto y = pos.y;
// use some raycasting to test hits
// https://github.com/substack/point-in-polygon/blob/master/index.js
auto length = polygon.size();
bool intersect = false;
for (size_t i = 0, j = length-1; i < length; j = i++)
{
auto xi = polygon[i].x, yi = polygon[i].y,
xj = polygon[j].x, yj = polygon[j].y;
intersect = ((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect)
inside = !inside;
}
return inside;
}