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

29
LINES

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

Lua code

1  
-- expects globals grid[], gw, gh
2  
-- uses global r
3  
-- stack layout: {{x, y}, ...}
4  
5  
function fill(stack)
6  
  while #stack > 0 do
7  
    local x, y = unpack(stack[#stack])
8  
    stack[#stack] = nil
9  
    
10  
    if not (x < 0 or y < 0 or x >= gw or y >= gh) then
11  
      local idx = y*gw+x+1
12  
      
13  
      if grid[idx] then
14  
        grid[idx] = nil
15  
        local me = newRectangle(x, y, 1, 1)
16  
        if r == nil then
17  
          r = me
18  
        else
19  
          r = mergeRectangles(r, me)
20  
        end
21  
        
22  
        stack[#stack+1] = {x-1, y}
23  
        stack[#stack+1] = {x+1, y}
24  
        stack[#stack+1] = {x, y-1}
25  
        stack[#stack+1] = {x, y+1}
26  
      end
27  
    end
28  
  end
29  
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

2 comment(s) hidden. show

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: 1247 / 189
Referenced in: [show references]