Page 1 of 1

PointInConcavePolygon at this website is not correct, it gives a incorrect true

Posted: Sat Oct 17, 2020 12:22 pm
by gcmartijn
Hi,

I'm write a unittest and using this function to detect if a point is inside a polygon.
https://love2d.org/wiki/PointInConcavePolygon

Code: Select all

poly = {
        0,
        0,
        10,
        0,
        10,
        10,
        0,
        10
    }

pointInPolygon(poly, 10, 10) -- returns false , correct
pointInPolygon(poly, 10, 0) -- returns false , correct
pointInPolygon(poly, 0, 10) -- returns false , correct
pointInPolygon(poly, 10, 10) -- returns true but that is incorrect.
 

Code: Select all

  -- detect all lines in the polygon
    for y = 1, 9 do
        for x = 1, 9 do
            lu.assertEquals(Polygon.pointInPolygon(poly, x, y), true, "point x: " .. x .. "point y: " .. y)
        end
    end

    -- the outer line is not included
    -- here I was working to scan the outline that returns false

I don't know if someone has a fix or a correct function that I can use ?
And maybe with an optional function argument to include the border in the collision detection.

Thanks

Re: PointInConcavePolygon at this website is not correct, it gives a incorrect true

Posted: Sat Oct 17, 2020 1:29 pm
by ivan
There is no such thing as the "border" of a polygon since the edges are lines which are in theory infinitely thin.
Floating point math has a hardware limit to the precision so forget about it.
Also, check out my tutorial for more info:
https://2dengine.com/?p=polygons

Re: PointInConcavePolygon at this website is not correct, it gives a incorrect true

Posted: Sat Oct 17, 2020 2:23 pm
by gcmartijn
Ow I did know that, thanks for the information.
I will unittest only the inside points and not the 'border'.

Re: PointInConcavePolygon at this website is not correct, it gives a incorrect true

Posted: Sat Oct 17, 2020 2:43 pm
by pgimeno
Technically (mathematically speaking), the border may be included or not, generating "open" or "closed" rectangles; however, as Ivan said, floating point precision renders that distinction useless, and you have to settle for "a point in the polygon's border, or very close to it, may or may not be included as part of the polygon", and that distinction may even vary depending on the position or width/height of the polygon.