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

53
LINES

< > BotCompany Repo | #1033938 // regionBorder_innerPoints - find region outline

JavaX fragment (include)

1  
// should return outer outline first and then outline of holes
2  
// (without a separator)
3  
4  
scope regionBorder_innerPoints.
5  
6  
static Pt[] #directions = {
7  
  pt(1, 0), pt(0, 1), pt(-1, 0), pt(0, -1) // r, d, l, u
8  
};
9  
10  
static L<Pt> regionBorder_innerPoints(BWImage_FastRegions regions, int iRegion) {
11  
  new PtBuffer out;
12  
  int w = regions.w;
13  
  var it = regions.regionIterator(iRegion);
14  
  new BitSet seen;
15  
  
16  
  while (it.next()) {
17  
    int x = it.x(), y = it.y();
18  
    
19  
    // is it a border pixel?
20  
    if (regions.inRegion(iRegion, x, y-1)
21  
      && regions.inRegion(iRegion, x-1, y)
22  
      && regions.inRegion(iRegion, x+1, y)
23  
      && regions.inRegion(iRegion, x, y+1))
24  
      continue;
25  
  
26  
    //print("border pixel: " + x + "/" + y);
27  
    int dir = 3; // start with direction up (any direction should do)
28  
  
29  
    loop: while (true) {
30  
      int pos = y*w+x;
31  
      if (seen.get(pos))
32  
        break;
33  
      seen.set(pos);
34  
        
35  
      out.add(x, y);
36  
      
37  
      for (int turn = 3; turn <= 6; turn++) { // try left, straight, right, back
38  
        int newDir = (dir+turn) & 3;
39  
        Pt d = directions[newDir];
40  
        int x2 = x+d.x, y2 = y+d.y;
41  
        if (regions.inRegion(iRegion, x2, y2)) {
42  
          x = x2; y = y2; dir = newDir;
43  
          continue loop;
44  
        }
45  
      }
46  
      
47  
      // if we get here, the region is just 1 pixel (so we're done)
48  
      break;
49  
    }
50  
  }
51  
  
52  
  ret out;
53  
}

Author comment

Began life as a copy of #1033758

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033938
Snippet name: regionBorder_innerPoints - find region outline
Eternal ID of this version: #1033938/1
Text MD5: 5e5e608e36c5dde17023b6b166913c1b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-14 14:41:01
Source code size: 1429 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 164
Referenced in: [show references]