I'm looking for a piece of code:
From the middle, in a "circle"-way, slowly to the ends of the edges of a rectangle. And when it reaches the boundaries on one side, just skip the pixels.
I tried already some crazy for-adventures, but that was to much code.
Does anyone have any idea for a simple/ingenious way?
It's like to start the game snake from the center until the full field is used. I'll use this way to scan a picture (from the middle to find the first pixel next to center in a other color).
Maybe a picture could describe it better:
From this link requires numpy and python of course.
import numpy as np
a = np.arange(7*7).reshape(7,7)
def spiral_ccw(A):
A = np.array(A)
out = []
while(A.size):
out.append(A[0][::-1]) # first row reversed
A = A[1:][::-1].T # cut off first row and rotate clockwise
return np.concatenate(out)
def base_spiral(nrow, ncol):
return spiral_ccw(np.arange(nrow*ncol).reshape(nrow, ncol))[::-1]
def to_spiral(A):
A = np.array(A)
B = np.empty_like(A)
B.flat[base_spiral(*A.shape)] = A.flat
return B
to_spiral(a)
array([[42, 43, 44, 45, 46, 47, 48],
[41, 20, 21, 22, 23, 24, 25],
[40, 19, 6, 7, 8, 9, 26],
[39, 18, 5, 0, 1, 10, 27],
[38, 17, 4, 3, 2, 11, 28],
[37, 16, 15, 14, 13, 12, 29],
[36, 35, 34, 33, 32, 31, 30]])