Today I tried harder to do it myself and I had success.
Because I found a solution and I think that someone else perhaps
searching for it, I am posting the solution here. This solution was
tested, it works and it's very simple. It fills an array of NxN (where
N is a power of 3) with numbers arranged on the Peano curve (Hilbert
II).
Hope it helps anyone who want a recursive function of peano curve:
Example result:
N=3
[[0 1 2]
[5 4 3]
[6 7 8]]
N=9
[[ 0 1 2 15 16 17 18 19 20]
[ 5 4 3 14 13 12 23 22 21]
[ 6 7 8 9 10 11 24 25 26]
[47 46 45 44 43 42 29 28 27]
[48 49 50 39 40 41 30 31 32]
[53 52 51 38 37 36 35 34 33]
[54 55 56 69 70 71 72 73 74]
[59 58 57 68 67 66 77 76 75]
[60 61 62 63 64 65 78 79 80]]
Here is the python code (hope that tabs are preserved :)
#!/usr/bin/python
from Numeric import *
#Python implementation of the peano curve (hilbert type 2)
#by Nasca Octavian Paul, Tg. Mures, Romania
#released under Public Domain
#
#a is an array of size NxN
#x0,y0 are the points of the up-left corner
#xm,ym have value 0 for non-reversed and 1 for reversed
# (xm for the x direction, and ym for y direction)
#N is the size of the array (it must be power of 3 like 3,9,27,81,729)
#K is a list of size 1 (it's used to pass it by refference); K must be
equal to [0]
#verical is True for vertical mode and False for horizontal mode of the
arrangement of the array
def peano(a,x0,y0,xm,ym,N,K,vertical):
if N<=1:
if vertical:
a[x0,y0]=K[0];
else:
a[y0,x0]=K[0];
K[0]+=1;
else:
N/=3;
peano(a,x0+2*xm*N,y0+2*ym*N,xm,ym,N,K,vertical);
peano(a,x0+2*xm*N,y0+1*N,1-xm,ym,N,K,vertical);
peano(a,x0+2*xm*N,y0+2*(1-ym)*N,xm,ym,N,K,vertical);
peano(a,x0+1*N,y0+2*(1-ym)*N,xm,1-ym,N,K,vertical);
peano(a,x0+1*N,y0+1*N,1-xm,1-ym,N,K,vertical);
peano(a,x0+1*N,y0+2*ym*N,xm,1-ym,N,K,vertical);
peano(a,x0+2*(1-xm)*N,y0+2*ym*N,xm,ym,N,K,vertical);
peano(a,x0+2*(1-xm)*N,y0+1*N,1-xm,ym,N,K,vertical);
peano(a,x0+2*(1-xm)*N,y0+2*(1-ym)*N,xm,ym,N,K,vertical);
#example
N=9 #always must be power of 3, otherwise you'll get bad results
a=zeros((N,N));
peano(a,0,0,0,0,N,[0],False);
print (transpose(a));