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

77
LINES

< > BotCompany Repo | #1033582 - FloodFill

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6394L/36K).

1  
persistable sclass FloodFill is Steppable {
2  
  // input
3  
  
4  
  int w, h;
5  
  swappable bool isConnectable(Pt a, Pt b) { throw unimplemented(); }
6  
  void visited(Pt p) {}
7  
  
8  
  // internal
9  
  
10  
  new L<Pt> stack;
11  
  BitMatrix visited;
12  
  
13  
  *(int *w, int *h) { visited = new BitSetMatrix(w, h); }
14  
  *(int *w, int *h, BitMatrix *visited) {}
15  
  
16  
  selfType useQuadTree() { visited = new QuadTreeBitMatrix(w, h); this; }
17  
  
18  
  bool isPointVisited(Pt p) {
19  
    ret visited.get(p);
20  
  }
21  
22  
  void startAt aka addPoint(Pt p) {
23  
    if (!isAddable(p)) ret;
24  
    stack.add(p);
25  
    addedPoint(p);
26  
  }
27  
  
28  
  bool isValidPoint(Pt p) {
29  
    ret p != null && !(p.x < 0 || p.y < 0 || p.x >= w || p.y >= h);
30  
  }
31  
32  
  bool isAddable(Pt p) {  
33  
    if (!isValidPoint(p)) false;
34  
    if (isPointVisited(p)) false;
35  
    true;
36  
  }
37  
  
38  
  void addedPoint(Pt p) {}
39  
  
40  
  public bool step() {
41  
    if (empty(stack)) false;
42  
    
43  
    Pt p = popLast(stack);
44  
    int x = p.x, y = p.y;
45  
  
46  
    visited.set(p, true);
47  
    visited(p);
48  
  
49  
    possibleBridge(p, new Pt(x-1, y));
50  
    possibleBridge(p, new Pt(x+1, y));
51  
    possibleBridge(p, new Pt(x, y-1));
52  
    possibleBridge(p, new Pt(x, y+1));
53  
    
54  
    true;
55  
  }
56  
  
57  
  void possibleBridge(Pt a, Pt b) {
58  
    if (isValidPoint(b) && isConnectable(a, b)) {
59  
      addPoint(b);
60  
      createBridge(a, b);
61  
    }
62  
  }
63  
  
64  
  void createBridge(Pt a, Pt b) {}
65  
66  
  void addAllPoints {  
67  
    for (p : allPointsInRect_virtual(w, h))
68  
      addPoint(p);
69  
  }
70  
  
71  
  void doAllPoints {
72  
    for (p : allPointsInRect_virtual(w, h)) {
73  
      addPoint(p);
74  
      stepAll(this);
75  
    }
76  
  }
77  
}

Author comment

Began life as a copy of #1006206

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033582
Snippet name: FloodFill
Eternal ID of this version: #1033582/22
Text MD5: 6fe0ba96b484a6775df10a23c76b0c76
Transpilation MD5: 796e0a9cd896f4478a6015c7f9ec1222
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-12-28 19:04:21
Source code size: 1621 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 79 / 196
Version history: 21 change(s)
Referenced in: [show references]