polygon:containsPoint includes wrong points

I'm on the Simulator on the Mac. Version 1.9.3 (133541)

Running this code:

local geom = playdate.geometry
local box = geom.polygon.new(
    72, -120,
    72, -300,
    -11, -150,
    -11, -120
)
box:close()
print(box:containsPoint(geom.point.new(114, -150)))

prints true when it should be false. Further investigation shows that any point at y = -150 and x > -11 is reported as being inside the polygon, with either non-zero or even-odd winding rules. (Painting the polygon works as expected.) I haven't investigated which polygons have the issue, but I haven't run into it with the other polygons I'm using in my game.

For now I monkey-patched the code to fix this in my game:

local cp = playdate.geometry.polygon.containsPoint
function playdate.geometry.polygon:containsPoint(x, y)
    return self:getBoundsRect():containsPoint(x, y) and cp(self, x, y)
end

but obviously it would be better if I didn't have to.

This is the "ray passes exactly through a vertex of a polygon" problem mentioned at Point in polygon - Wikipedia. They make it sound simple to work around but after spending hours nailing down all the edge cases either they're just being cute or I'm missing something obvious. Anyway, fixed in the next release!

2 Likes