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

persistable sclass FloodFill is Steppable {
  // input
  
  int w, h;
  swappable bool isConnectable(Pt a, Pt b) { throw unimplemented(); }
  void visited(Pt p) {}
  
  // internal
  
  new L<Pt> stack;
  BitMatrix visited;
  
  *(int *w, int *h) { visited = new BitSetMatrix(w, h); }
  *(int *w, int *h, BitMatrix *visited) {}
  
  selfType useQuadTree() { visited = new QuadTreeBitMatrix(w, h); this; }
  
  bool isPointVisited(Pt p) {
    ret visited.get(p);
  }

  void startAt aka addPoint(Pt p) {
    if (!isAddable(p)) ret;
    stack.add(p);
    addedPoint(p);
  }
  
  bool isValidPoint(Pt p) {
    ret p != null && !(p.x < 0 || p.y < 0 || p.x >= w || p.y >= h);
  }

  bool isAddable(Pt p) {  
    if (!isValidPoint(p)) false;
    if (isPointVisited(p)) false;
    true;
  }
  
  void addedPoint(Pt p) {}
  
  public bool step() {
    if (empty(stack)) false;
    
    Pt p = popLast(stack);
    int x = p.x, y = p.y;
  
    visited.set(p, true);
    visited(p);
  
    possibleBridge(p, new Pt(x-1, y));
    possibleBridge(p, new Pt(x+1, y));
    possibleBridge(p, new Pt(x, y-1));
    possibleBridge(p, new Pt(x, y+1));
    
    true;
  }
  
  void possibleBridge(Pt a, Pt b) {
    if (isValidPoint(b) && isConnectable(a, b)) {
      addPoint(b);
      createBridge(a, b);
    }
  }
  
  void createBridge(Pt a, Pt b) {}

  void addAllPoints {  
    for (p : allPointsInRect_virtual(w, h))
      addPoint(p);
  }
  
  void doAllPoints {
    for (p : allPointsInRect_virtual(w, h)) {
      addPoint(p);
      stepAll(this);
    }
  }
}

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: 80 / 196
Version history: 21 change(s)
Referenced in: #1003674 #1033583