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


 
#include <Array.au3>
$Array = ZigZag(5)
_ArrayDisplay($Array)
 
Func ZigZag($int)
	Local $av_array[$int][$int]
	Local $x = 1, $y = 1
	For $I = 0 To $int ^ 2 -1
		$av_array[$x-1][$y-1] = $I
		If Mod(($x + $y), 2) = 0 Then ;Even
			if ($y < $int) Then
				$y += 1
			Else
				$x += 2
			EndIf
			if ($x > 1) Then $x -= 1
		Else ; ODD
			if ($x < $int) Then
				$x += 1
			Else
				$y += 2
			EndIf
			If $y > 1 Then $y -= 1
		EndIf
	Next
	Return $av_array
EndFunc   ;==>ZigZag
 