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

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