static ItIt pixelSpiral(int sx, int sy, int imageWidth, int imageHeight) { ret pixelSpiral(sx, sy, imageWidth, imageHeight, Int.MAX_VALUE); } // thanks to https://stackoverflow.com/questions/398299/looping-in-a-spiral static ItIt pixelSpiral(int sx, int sy, int imageWidth, int imageHeight, int maxPixels) { ret iff_endOnNull(new F0 { int x, y, dx, dy = -1; int maxPixels2 = min(maxPixels, sqr(max4(sx+1, imageWidth-sx, sy+1, imageHeight-sy)*2)); int i; Pt get() { 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 pt(px, py); } null; } }); }