Geometry.polygon:unpack() method

Hello!

I'd like to request the addition of an :unpack method for polygons similar and consistent to what's available for points, lineSegments, vectors etcetera. Should be pretty easy to implement and unlikely to break anything.

I'd suggest it return a flat list of {x1, y1, x2, y2, x3, y3 ...}, as people likely to use it probably want the efficient option rather than using table lookups on {{x, y}, {x, y}, {x, y}, ...}. We could unpack the table but that seems unnecessary.

Reason for requesting this feature:
I use polygons a lot because it's quick to rotate/scale/skew/position them with affine transforms. But modifying the shapes of polygons can be slow as it typically goes:

  • count points
  • iterate through points and:
    • getPoint
    • modify point position
    • setPoint

How would this request improve your experience developing for Playdate?
It would increase efficiency and performance of polygon-based games by providing additional optimisation opportunities.

Other details you have relating to this request.

I'm currently using this work-around, but something built-in would be faster and neater.

function polygonToList(polygon)
    local polyString = tostring(polygon)
    local coordList = {}
    local i = 1
    for number in polyString:gmatch("[-%d%.]+") do
        coordList[i] = tonumber(number)
        i += 1
    end
    
    return coordList
end

Edit: gmatch("[-+]?%d*%.?%d+[eE]?[-+]?%d*") to handle very small numbers.

2 Likes

Sub-request: and polygon:pack too please. Or polygon:setPoints.

It’s currently quicker to use polygon:new than to loop through and update using polygon:setPointAt. Updating the userdata in one pass would be more efficient and convenient.