fillPolygon off by 1 (both C and Lua)

,

In both x and y directions, fillPolygon falls 1 pixel short of what I would expect.

However, this is does not apply to all vertices. It seems the vertices that are at the right and bottom of the shape are affected.

When I use fillPolygon, I expect the coordinates are "inclusive", ie. when the coordinate (8,8) is included, I expect the 9th pixel from the top left to be set, just as it actually is for drawLine and drawPolygon.

The - very wasteful - workaround seems to be to call both fill and drawPolygon functions with the same parameters*. I expect the result of fillPolygon to be equal to "fill+draw" in the screenshot and reproduction sample code.

Affects both Lua and C APIs

*drawPolygon not available in C

image

function playdate:update()
    gfx.setPattern({ 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF })
    gfx.fillRect(0, 0, 400, 240)
    gfx.setColor(gfx.kColorBlack)

    gfx.drawText("Draw", 80, 10)
    gfx.drawPolygon(
            80, 80,
            120, 80,
            120, 120,
            80, 120,
            80, 80
    )

    gfx.drawText("Fill", 160, 10)
    gfx.fillPolygon(
            160, 80,
            200, 80,
            200, 120,
            160, 120,
            160, 80
    )

    gfx.drawText("Fill+Draw", 240, 10)
    gfx.fillPolygon(
            240, 80,
            280, 80,
            280, 120,
            240, 120,
            240, 80
    )
    gfx.drawPolygon(
            240, 80,
            280, 80,
            280, 120,
            240, 120,
            240, 80
    )

    -- more complex shape
    gfx.fillPolygon(
            8, 144,
            200, 136,
            248, 144,
            248, 152,
            200, 144,
            8, 152,
            8, 144
    )
end
5 Likes

@dave could I get some triage for this? Bug or expected?

Sorry, missed your followup there. Looks like a bug to me, though I say that with some hesitation. We had so many discussions about where exactly "draw a square of width 10 with top left corner at (0,0)" should draw, whether the right side would be at x=9 or x=10, and never came to a consensus. I can't even remember what my opinion was now.

But in this case I think it's clear that you'd want the right and bottom edges when you do fillPolygon. In the square case the "width" term is ambiguous and you don't have that here.

I'll file it and take a closer look. It might be a simple fix or it might be a whole thing, hard to tell.