Libraryless. Click here for Pure Java version (7883L/44K).
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 PtBuffer 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 | } |
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/23 |
| Text MD5: | 8a0c1cd1caa15f25cec594d6bb829321 |
| Transpilation MD5: | 48944a56cbd7d29695aa72a58beefc41 |
| Author: | stefan |
| Category: | javax / imaging |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-03-22 18:55:16 |
| Source code size: | 1624 bytes / 77 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 540 / 783 |
| Version history: | 22 change(s) |
| Referenced in: | [show references] |