Jump to content

Hilbert curve: Difference between revisions

Line 400:
Should work up to Hilbert(12) if your console is big enough for that.
 
Implemented a full line-drawing Unicode/ASCII drawing and added for the example my signature to the axiom.
<lang lua>-- any version from 5.1 to LuaJIT 2.1.0-beta3-readline
local bit=bit32 or bit
-- Hilbertcurve by Lindenmayer system
function string.hilbert(s, n)
for i=1,n do
Line 415 ⟶ 418:
return s
end
-- Or's characters for ASCII line drawing
function charor(c1, c2)
local bits={ [" "]=0, ["┌"]=0x3, ["│"]=0x5, ["└"]=0x6, ["├"]=0x7,
["┐"]=0x9, ["─"]=0xa, ["┬"]=0xb, ["┘"]=0xc, ["┤"]=0xd,
["┴"]=0xe, ["┼"]=0xf,}
local char={" ", " ", " ", "┌", " ", "│", "└", "├", " ", "┐", "─", "┬", "┘", "┤", "┴", "┼",}
local b1,b2=bits[c1] or 0,bits[c2] or 0
return char[bit.bor(b1,b2)+1]
end
-- ASCII line drawing
function draw(s)
local char={
Line 425 ⟶ 438:
local move={{x=1,y=0},{x=0,y=1},{x=-1,y=0},{x=0,y=-1}}
local x,y=1,1
local minx,maxx,miny,maxy=1,1,1,1
local dir,turn=0,0
s=s.."F"
Line 430 ⟶ 444:
if c=="F" then
if scr[y]==nil then scr[y]={} end
scr[y][x]=charor(char[dir+1][turn%#char[1]+1],scr[y][x] or " ")
dir = (dir+turn) % #move
x, y = x+move[dir+1].x,y+move[dir+1].y
maxx,maxy=math.max(maxx,x),math.max(maxy,y)
minx,miny=math.min(minx,x),math.min(miny,y)
turn=0
elseif c=="-" then
turn=turn+1
else --elseif c=="+" then
turn=turn-1
else -- allow canvas movement if any other chars like "M" are used in Axiom
x, y = x+move[dir+1].x,y+move[dir+1].y
maxx,maxy=math.max(maxx,x),math.max(maxy,y)
minx,miny=math.min(minx,x),math.min(miny,y)
end
end
for i=#scrmaxy,1miny,-1 do
local oneline={}
print(table.concat(scr[i]))
for x=minx,maxx do -- draw full canvas fill in nil with " "
oneline[1+x-minx]=scr[i] and scr[i][x] or " "
end
print(local line=table.concat(scr[i])oneline)
io.write(line, "\n")
end
end
Line 449 ⟶ 474:
draw(str:hilbert(n))
</lang>
{{output}} luajitlua hilbert.lua 54 AF-FFFF++FF-FF-FF++FFFF-F-FFFF+FF+FF+FF++FFF+FF+FFF
<pre>
┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐
┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐
│└┘││└┘││└┘││└┘│
│└┘││└┘││└┘││└┘││└┘││└┘││└┘││└┘│
└┐┌┘└┐┌┘└┐┌┘└┐┌┘
└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘
┌┘└──┘└┐┌┘└──┘└┐
┌┘└──┘└┐┌┘└──┘└┐┌┘└──┘└┐┌┘└──┘└┐
│┌─┐┌─┐││┌─┐┌─┐│
│┌─┐┌─┐││┌─┐┌─┐││┌─┐┌─┐││┌─┐┌─┐│
└┘┌┘└┐└┘└┘┌┘└┐└┘
└┘┌┘└┐└┘└┘┌┘└┐└┘└┘┌┘└┐└┘└┘┌┘└┐└┘
┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐
┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐
│└─┘└─┘└┘└─┘└─┘│
│└─┘└─┘└┘└─┘└─┘││└─┘└─┘└┘└─┘└─┘│
└┐┌─┐┌─┐┌─┐┌─┐┌┘
└┐┌─┐┌─┐┌─┐┌─┐┌┘└┐┌─┐┌─┐┌─┐┌─┐┌┘
┌┘└┐└┘┌┘└┐└┘┌┘└┐
┌┘└┐└┘┌┘└┐└┘┌┘└┐┌┘└┐└┘┌┘└┐└┘┌┘└┐
│┌┐│┌┐└┐┌┘┌┐│┌┐│
│┌┐│┌┐└┐┌┘┌┐│┌┐││┌┐│┌┐└┐┌┘┌┐│┌┐│
└┘└┘│└─┘└─┘│└┘└┘┐ ┐┌─┐
└┘└┘│└─┘└─┘│└┘└┘└┘└┘│└─┘└─┘│└┘└┘
┌┐┌┐│┌─┐┌─┐│┌┐┌┐│ ││ │
┌┐┌┐│┌─┐┌─┐│┌┐┌┐┌┐┌┐│┌─┐┌─┐│┌┐┌┐
│└┘│└┘┌┘└┐└┘│└┘│├─┤├─┴┐
│└┘│└┘┌┘└┐└┘│└┘││└┘│└┘┌┘└┐└┘│└┘│
└┐┌┘┌┐└┐┌┘┌┐└┐┌┘│ ││ │
└┐┌┘┌┐└┐┌┘┌┐└┐┌┘└┐┌┘┌┐└┐┌┘┌┐└┐┌┘
─┘└─┘└─┘└─┘└─┘└─┘ └┴──┘
┌┘└─┘└─┘└─┘└─┘└──┘└─┘└─┘└─┘└─┘└┐
│┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐│
└┘┌┘└┐└┘┌┘└┐└┘┌┘└┐└┘┌┘└┐└┘┌┘└┐└┘
┌┐└┐┌┘┌┐│┌┐│┌┐└┐┌┘┌┐│┌┐│┌┐└┐┌┘┌┐
│└─┘└─┘│└┘└┘│└─┘└─┘│└┘└┘│└─┘└─┘│
└┐┌──┐┌┘┌┐┌┐│┌─┐┌─┐│┌┐┌┐└┐┌──┐┌┘
┌┘└┐┌┘└┐│└┘│└┘┌┘└┐└┘│└┘│┌┘└┐┌┘└┐
│┌┐││┌┐│└┐┌┘┌┐└┐┌┘┌┐└┐┌┘│┌┐││┌┐│
└┘└┘└┘└┘┌┘└─┘└─┘└─┘└─┘└┐└┘└┘└┘└┘
┌┐┌┐┌┐┌┐└┐┌─┐┌─┐┌─┐┌─┐┌┘┌┐┌┐┌┐┌┐
│└┘││└┘│┌┘└┐└┘┌┘└┐└┘┌┘└┐│└┘││└┘│
└┐┌┘└┐┌┘│┌┐│┌┐└┐┌┘┌┐│┌┐│└┐┌┘└┐┌┘
┌┘└──┘└┐└┘└┘│└─┘└─┘│└┘└┘┌┘└──┘└┐
│┌─┐┌─┐│┌┐┌┐│┌─┐┌─┐│┌┐┌┐│┌─┐┌─┐│
└┘┌┘└┐└┘│└┘│└┘┌┘└┐└┘│└┘│└┘┌┘└┐└┘
┌┐└┐┌┘┌┐└┐┌┘┌┐└┐┌┘┌┐└┐┌┘┌┐└┐┌┘┌┐
┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.