title: Matrix and algorithm "spiral"
body: <p>i wanted ask if there some algorithm ready, that allowed me to do this:   i have a matrix m (col) x n (row) with m x n elements. I want give position to this element starting from center and rotating as a spiral, for example, for a matrix 3x3 i have 9 elements so defined:</p>
  
  <pre><code> 5  6  7
   4  9  8
   3  2  1 
  </code></pre>
  
  <p>or for una matrix 4 x 3 i have 12 elements, do defined:</p>
  
  <pre><code>  8   9  10  1
    7  12  11  2
    6   5   4  3
  </code></pre>
  
  <p>or again, a matrix 5x2 i have 10 elements so defined:</p>
  
  <pre><code>    3  4
      7  8
     10  9
      6  5 
      2  1
  </code></pre>
  
  <p>etc.
  I have solved basically defining a array of integer of m x n elements and loading manually the value, but in generel to me like that matrix maked from algorithm automatically.
  Thanks to who can help me to find something so, thanks very much. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This code, do exactely about i want have, but not is in delphi; just only i need that start from 1 and not from 0. Important for me is that it is valid for any matrics m x n. Who help me to translate it in delphi?</p>
  
  <pre><code>(defun spiral (rows columns)  
    (do ((N (* rows columns))       
      (spiral (make-array (list rows columns) :initial-element nil))      
      (dx 1) (dy 0) (x 0) (y 0)    
     (i 0 (1+ i)))     
   ((= i N) spiral)   
   (setf (aref spiral y x) i)
      (let ((nx (+ x dx)) (ny (+ y dy)))  
      (cond       
         ((and (&lt; -1 nx columns)       
         (&lt; -1 ny rows)           
         (null (aref spiral ny nx)))    
      (setf x nx 
            y ny))  
       (t (psetf dx (- dy)                 
         dy dx)       
      (setf x (+ x dx)             
       y (+ y dy))))))) 
  
  
  &gt; (pprint (spiral 6 6))
  
  #2A ((0  1  2  3  4  5)
      (19 20 21 22 23  6)
      (18 31 32 33 24  7)
      (17 30 35 34 25  8)
      (16 29 28 27 26  9)
      (15 14 13 12 11 10))
  
  
  &gt; (pprint (spiral 5 3))
  
  #2A  ((0  1 2)
       (11 12 3)
       (10 13 4)
        (9 14 5)
        (8 7 6))
  </code></pre>
  
  <p>Thanks again very much.</p>