Here’s some code to draw a Hilbert curve. You can use the sliders to change the screen size and the order number. The higher the order number, the more intricate the curve. Increasing the order by one increases the curve complexity by 4. After changing the order number, press the process box. This displays the FPS, so the higher the order, the slower the FPS. Best viewed in landscape orientation.
--- Hilbert curve
viewer.mode=STANDARD
function setup()
parameter.watch("fps")
s=require("socket")
stroke(255)
strokeWidth(2)
positions={[0]=vec2(0,0),vec2(0,1),vec2(1,1),vec2(1,0)}
parameter.integer("size",1,50,25)
parameter.integer("order",1,12)
parameter.action("process",process)
process()
end
function process()
tab={}
N=2^order
st=s:gettime()
for i=0,N*N do
hindex2xy(i,N)
end
en=s:gettime()
output.clear()
print("Order "..order)
print("Number of points "..#tab)
print("Time "..en-st)
end
function draw()
fps=1/DeltaTime
background(0)
for z=1,#tab-1 do
if z>1 then
line(tab[z].x*size+10,tab[z].y*size+50,curr.x*size+10,curr.y*size+50)
end
curr=tab[z]
end
end
function hindex2xy(hindex,N)
tmp=positions[hindex&3]
x=tmp.x
y=tmp.y
hindex=hindex//4
n=4
while n<=N do
n2=n/2
ss=hindex&3
if ss==0 then
tmp=x
x=y
y=tmp
elseif ss==1 then
y=y+n2
elseif ss==2 then
x=x+n2;
y=y+n2;
elseif ss==3 then
tmp=y
y=(n2-1)-x
x=(n2-1)-tmp
x=x+n2
end
hindex=hindex//4
n=n*2
end
table.insert(tab,vec2(x,y))
end