Task

Produce a zig-zag array.



A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the

numbers increase sequentially as you zig-zag along the array's   anti-diagonals.

For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images).



For example, given   5,   produce this array:

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



Related tasks

   Spiral matrix
   Identity matrix
   Ulam spiral (for primes)


See also

   Wiktionary entry:   anti-diagonals


100 S = 5
110 S2 = S ^ 2 : REM SQUARED
120 H = S2 / 2 : REM HALFWAY
130 S2 = S2 - 1
140 DX = 1 : REM INITIAL
150 DY = 0 : REM DIRECTION
160 N = S - 1
170 DIM A%(N, N)
 
200 FOR I = 0 TO H
210     A%(X, Y) = I
220     A%(N - X, N - Y) = S2 - I
230     X = X + DX
240     Y = Y + DY
250     IF Y = 0 THEN DY = DY + 1 : IF DY THEN DX = -DX
260     IF X = 0 THEN DX = DX + 1 : IF DX THEN DY = -DY
270 NEXT I
 
300 FOR Y = 0 TO N
310     FOR X = 0 TO N
320         IF X THEN PRINT TAB(X * (LEN(STR$(S2)) + 1) + 1);
330         PRINT A%(X, Y);
340     NEXT X
350     PRINT
360 NEXT Y