Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

29
LINES

< > BotCompany Repo | #460 // Floodfill (include)

Lua code

-- 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

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
864 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
-- 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
}}
2015-08-19 22:48:22  delete 
795 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #460
Snippet name: Floodfill (include)
Eternal ID of this version: #460/1
Text MD5: 0a9bb6c449c2a3d83907647022f7ec61
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-13 18:25:22
Source code size: 683 bytes / 29 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1017 / 158
Referenced in: [show references]