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

67
LINES

< > BotCompany Repo | #1033582 - FloodFill

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

Libraryless. Click here for Pure Java version (5600L/31K).

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); }
  
  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 doAllPoints {  
    for (p : allPointsInRect_virtual(w, h))
      addPoint(p);
  }
}

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/17
Text MD5: cfa4f09233deabd360d3ae67ed52a9c5
Transpilation MD5: 0887e90889798bc193e9677044a7779a
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-04 06:44:53
Source code size: 1370 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 43 / 124
Version history: 16 change(s)
Referenced in: [show references]