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

41
LINES

< > BotCompany Repo | #1029759 // pixelSpiral_longIterator

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

Libraryless. Click here for Pure Java version (184L/2K).

1  
// start from center
2  
static LongIterator pixelSpiral_longIterator(int w, int h) {
3  
  ret pixelSpiral_longIterator(w/2, h/2, w, h);
4  
}
5  
6  
// thanks to https://stackoverflow.com/questions/398299/looping-in-a-spiral
7  
// firstIntFromLong(l) = x, secondIntFromLong(l) = y
8  
static LongIterator pixelSpiral_longIterator(int sx, int sy, int imageWidth, int imageHeight, int maxPixels default Int.MAX_VALUE) {
9  
  ret new LongIterator {
10  
    int x, y, dx, dy = -1;
11  
    int maxPixels2 = min(maxPixels, sqr(max4(sx+1, imageWidth-sx, sy+1, imageHeight-sy)*2));
12  
    int i;
13  
    long pixel;
14  
    bool done;
15  
    
16  
    void fetch {
17  
      while (i++ < maxPixels2) {
18  
        int px = sx+x, py = sy+y;
19  
  
20  
        if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1-y))) {
21  
          int t=dx; dx=-dy; dy=t;
22  
        }   
23  
        x += dx; y += dy;
24  
        
25  
        if (px >= 0 && py >= 0 && px < imageWidth && py < imageHeight)
26  
          ret with pixel = twoIntsToLong(px, py);
27  
       }
28  
       set done;
29  
     }
30  
    
31  
    { fetch(); }
32  
    
33  
    public bool hasNext() { ret !done; }
34  
35  
    public long next() {
36  
      long p = pixel;
37  
      fetch();
38  
      ret p;
39  
    }
40  
  };
41  
}

Author comment

Began life as a copy of #1025244

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1029759
Snippet name: pixelSpiral_longIterator
Eternal ID of this version: #1029759/5
Text MD5: ae27db3ae72146602cc49c3eeccd4fd9
Transpilation MD5: 7fd4ca12858d3a98e711aa72ba9d5f54
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-12 01:58:31
Source code size: 1183 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 137 / 207
Version history: 4 change(s)
Referenced in: [show references]