-- expects globals grid[], gw, gh -- uses global r -- stack layout: {{x, y}, ...} function fill(stack) while #stack > 0 do local x, y = unpack(stack[#stack]) stack[#stack] = nil if not (x < 0 or y < 0 or x >= gw or y >= gh) then local idx = y*gw+x+1 if grid[idx] then grid[idx] = nil local me = newRectangle(x, y, 1, 1) if r == nil then r = me else r = mergeRectangles(r, me) end stack[#stack+1] = {x-1, y} stack[#stack+1] = {x+1, y} stack[#stack+1] = {x, y-1} stack[#stack+1] = {x, y+1} end end end end