Task

Produce a spiral array.



A   spiral array   is a square arrangement of the first    N2   natural numbers,   where the

numbers increase sequentially as you go around the edges of the array spiraling inwards.



For example, given   5,   produce this array:

 0  1  2  3  4
15 16 17 18  5
14 23 24 19  6
13 22 21 20  7
12 11 10  9  8



Related tasks

   Zig-zag matrix 
   Identity_matrix
   Ulam_spiral_(for_primes)


 
# Project : Spiral matrix
 
load "guilib.ring"
load "stdlib.ring"
new qapp 
        {
        win1 = new qwidget() {
                   setwindowtitle("Spiral matrix")
                   setgeometry(100,100,600,400)
                   n = 5
                   result = newlist(n,n)
                   spiral = newlist(n,n)
                   k = 1 
                   top = 1
                   bottom = n
                   left = 1
                   right = n
                   while (k <= n*n)
                           for  i= left to right
                                result[top][i] = k
                                k = k + 1
                           next    
                           top = top + 1 
                           for i = top to bottom
                                result[i][right] = k
                                k = k + 1
                           next
                           right = right - 1 
                           for i = right to left step -1
                                result[bottom][i] = k
                                k = k + 1
                           next
                           bottom = bottom - 1 
                           for i = bottom to top step -1
                                result[i][left] = k
                                k = k + 1
                           next
                           left = left + 1
                   end
                   for m = 1 to n
                        for p = 1 to n
                             spiral[p][m] = new qpushbutton(win1) {
                                                  x = 150+m*40
                                                  y = 30 + p*40
                                                  setgeometry(x,y,40,40)
                                                  settext(string(result[m][p]))
                                                  }
                        next
                   next
                   show()
                   }
                   exec()
                   }
 