Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

you code is: for (size_t i = 0, j = length-1; i < length; j = ++i) /// should be for (size_t i = 0, j = length-1; i < length; j = i++)!! #176

Open
tea18andlife opened this issue Jun 22, 2018 · 0 comments

Comments

@tea18andlife
Copy link

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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant