Skip to content

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

@tea18andlife

Description

@tea18andlife

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;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions