title: Radially Iterating from a given point in a Cartesian Grid
body: <p>Assuming I have a point (i, j) which are the indices of some maximum point in a 2D grid, I am trying to figure out how to get points of approximate constant radius and iterate through them. Effectively, I am currently doing a cartesian cluster algorithm, but I know the clusters are circular or at least elliptical,</p>
  <p>An example of this grid could be</p>
  <pre><code>im = [[0,0,0,0,0,0,0,0,0],
         [0,0,1,2,2,2,1,0,0],
         [0,1,2,3,3,3,1,1,0],
         [0,1,2,3,4,3,2,1,0],
         [0,1,3,5,5,5,3,1,0],
         [0,1,3,7,9,7,3,1,0],
         [0,1,2,3,5,3,2,1,0],
         [0,0,1,2,2,2,1,0,0],
         [0,0,0,0,0,0,0,0,0]]
  
  </code></pre>
  <p>Though in my application, these grids are variable in size. And the internal points could be any shape. This is a comparatively 'nice' example, because it is more circular. The zero points describe the exterior of the 'cluster'</p>
  <pre><code>
  im = some 2x2 grid
  
   ind = lambda i, j: ((i + 1, j + 1), (i - 1, j + 1), (i + 1, j - 1), (i - 1, j - 1), (
          i, j + 1), (i, j - 1), (i + 1, j), (i - 1, j))
  
  maxij = np.unravel_index(np.argmax(im), np.array(im).shape)
  i, j = maxij[0], maxij[1]
  counts_out = [im[i][j]]
  im[i][j] = 0
  index_list = [(i, j)] #the point I want to iterate from within the grid
  while True:
      current_counts = []
      cind = index_list.copy()
      index_list = []
      for (ip, jp) in cind:
          indlst = ind(ip, jp)
          for (ik, jk) in indlst:
              try:
                  if im[ik][jk] &gt; 0:
                      current_counts.append(im[ik][jk])
                      im[ik][jk] = 0
                      index_list.append((ik, jk))
              except IndexError:
                  pass
       counts_out.append(sum(current_counts))
       if len(index_list) == 0:
           break
  </code></pre>
  <p>My current algorithm has an output, <code>counts_out</code>, if I start at grid point 9, of</p>
  <pre><code>counts_out = [9, 40, 47, 22, 8, 0]
  </code></pre>
  <p>I would ideally like this to sample approximate 'circular' boxes around the initial point. I am not sure exactly what the output of this would be.</p>