0

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:

maybe a picture describe it better

zx485
  • 28,498
  • 28
  • 50
  • 59
Mairos
  • 13
  • 1
  • The best way to solve this kind of problem is to sit down with a pencil and a piece of graph paper, draw your snake, and then examine the line lengths to derive the pattern. For example, you go left 1, then up 1, then right two, down two, etc. You can figure this out in less than an hour if you sit down and concentrate. – Jim Mischel Jan 21 '19 at 20:40

2 Answers2

0

how do you think about run from edge to center? It really easy to code, just run from (0;0) and if you hit a edge or a pixel already visited just turn right 90*

Hai
  • 73
  • 1
  • 10
0

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]])
NaN
  • 2,212
  • 2
  • 18
  • 23