It looks like you're new here. If you want to get involved, click one of these buttons!
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
Comments
Interesting use of bitwise and binary operators. I made a small modification to animate the drawing of the curve
@Simeon I have an animated version, but didn’t post it. I guess I don’t need to now. I was surprised when I searched for Hilbert code and didn’t find any here, I was looking thru the Rosetta link which had code in a bunch of different languages, but some didn’t make any sense and others that I understood were too long and complicated.
@Simeon I thought I’d post my animated version anyways. Adjust the size to fit the screen. It’s kind of interesting to shrink the image so there’s no spaces between the lines.
Here’s code for a 3D Hilbert curve using Craft. You can rotate it to get a better view. Change the order variable to increase/decrease the density. Going higher and it gets to dense.
@Simeon Is there a way to increase the thickness of the debug line command.
Here’s an animated 3D Hilbert curve. You can zoom in / out or rotate it. You can reduce the value of the “order” variable to see a smaller curve.
You guys amaze me.