Libraryless. Click here for Pure Java version (6721L/37K).
1 | // backtracks lead to endless loops - must rework the architecture |
2 | sclass SimpleOutlineWalker is Steppable { |
3 | IIntIntPred pixelIsInside; |
4 | int x, y; |
5 | settable bool processBacktracks = false; |
6 | int startX, startY; |
7 | NESWDirection direction = NESWDirection.east; |
8 | gettable int nPixels; |
9 | new PtBuffer pixels; |
10 | new L<BacktrackPoint> backtrackStack; |
11 | |
12 | // seen points as intPairToLong |
13 | new CompactLongSet seen; |
14 | |
15 | event foundPoint(int x, int y); |
16 | |
17 | srecord BacktrackPoint(int x, int y, NESWDirection dir, int turn) {} |
18 | |
19 | *(IIntIntPred pixelIsInside, Pt p) { |
20 | this(pixelIsInside, p.x, p.y); |
21 | } |
22 | |
23 | // x, y must be inside the shape |
24 | *(IIntIntPred *pixelIsInside, int *x, int *y) { |
25 | if (!pixelIsInside.get(x, y)) |
26 | fail("pixel is not inside shape: " + x + "/" + y); |
27 | |
28 | // go north until we hit the border |
29 | while (pixelIsInside.get(x, y-1)) |
30 | --y; |
31 | |
32 | startX = x; startY = y; this.y = y; |
33 | } |
34 | |
35 | public bool step() { |
36 | ++nPixels; |
37 | pixels?.add(x, y); |
38 | foundPoint(x, y); |
39 | |
40 | if (walkOneStep()) true; |
41 | |
42 | if (!processBacktracks) false; |
43 | while (nempty(backtrackStack)) |
44 | if (processBacktrack()) true; |
45 | false; |
46 | } |
47 | |
48 | bool walkOneStep(int turn default 3) { |
49 | // try left, straight, right, back |
50 | for (; turn <= 6; turn++) { |
51 | NESWDirection newDirection = direction.plus(turn); |
52 | int x2 = x+newDirection.x, y2 = y+newDirection.y; |
53 | |
54 | bool inside = pixelIsInside.get(x2, y2); |
55 | ifdef SimpleOutlineWalker_debug |
56 | printVars(+x, +y, +direction, +turn, +newDirection, +x2, +y2, +inside); |
57 | endifdef |
58 | if (inside) { |
59 | if (turn < 6) |
60 | backtrackStack.add(new BacktrackPoint(x, y, direction, turn+1)); |
61 | |
62 | x = x2; y = y2; direction = newDirection; |
63 | |
64 | break; |
65 | } |
66 | } |
67 | |
68 | ret seen.add(intPairToLong(x, y)); |
69 | //ret x != startX || y != startY; |
70 | } |
71 | |
72 | bool processBacktrack() { |
73 | if (empty(backtrackStack)) false; |
74 | var bt = popLast(backtrackStack); |
75 | x = bt.x; y = bt.y; direction = bt.dir; |
76 | |
77 | ret walkOneStep(bt.turn); |
78 | } |
79 | |
80 | Pt currentPt() { ret pt(x, y); } |
81 | } |
Began life as a copy of #1034903
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034908 |
Snippet name: | SimpleOutlineWalker [dev.] |
Eternal ID of this version: | #1034908/21 |
Text MD5: | f8afc3d2ed8789b29e8ba949a66df5bc |
Transpilation MD5: | edeb589480bf5576acac1cc2db3123dc |
Author: | stefan |
Category: | javax / image recognition |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-15 17:54:26 |
Source code size: | 2204 bytes / 81 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 163 / 329 |
Version history: | 20 change(s) |
Referenced in: | [show references] |