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).

// start from center
static LongIterator pixelSpiral_longIterator(int w, int h) {
  ret pixelSpiral_longIterator(w/2, h/2, w, h);
}

// thanks to https://stackoverflow.com/questions/398299/looping-in-a-spiral
// firstIntFromLong(l) = x, secondIntFromLong(l) = y
static LongIterator pixelSpiral_longIterator(int sx, int sy, int imageWidth, int imageHeight, int maxPixels default Int.MAX_VALUE) {
  ret new LongIterator {
    int x, y, dx, dy = -1;
    int maxPixels2 = min(maxPixels, sqr(max4(sx+1, imageWidth-sx, sy+1, imageHeight-sy)*2));
    int i;
    long pixel;
    bool done;
    
    void fetch {
      while (i++ < maxPixels2) {
        int px = sx+x, py = sy+y;
  
        if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1-y))) {
          int t=dx; dx=-dy; dy=t;
        }   
        x += dx; y += dy;
        
        if (px >= 0 && py >= 0 && px < imageWidth && py < imageHeight)
          ret with pixel = twoIntsToLong(px, py);
       }
       set done;
     }
    
    { fetch(); }
    
    public bool hasNext() { ret !done; }

    public long next() {
      long p = pixel;
      fetch();
      ret p;
    }
  };
}

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: 131 / 198
Version history: 4 change(s)
Referenced in: [show references]