Hilbert curve: Difference between revisions
Content added Content deleted
Line 400: | Line 400: | ||
Should work up to Hilbert(12) if your console is big enough for that. |
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 |
<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) |
function string.hilbert(s, n) |
||
for i=1,n do |
for i=1,n do |
||
Line 415: | Line 418: | ||
return s |
return s |
||
end |
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) |
function draw(s) |
||
local char={ |
local char={ |
||
Line 425: | Line 438: | ||
local move={{x=1,y=0},{x=0,y=1},{x=-1,y=0},{x=0,y=-1}} |
local move={{x=1,y=0},{x=0,y=1},{x=-1,y=0},{x=0,y=-1}} |
||
local x,y=1,1 |
local x,y=1,1 |
||
local minx,maxx,miny,maxy=1,1,1,1 |
|||
local dir,turn=0,0 |
local dir,turn=0,0 |
||
s=s.."F" |
s=s.."F" |
||
Line 430: | Line 444: | ||
if c=="F" then |
if c=="F" then |
||
if scr[y]==nil then scr[y]={} end |
if scr[y]==nil then scr[y]={} end |
||
scr[y][x]=char[dir+1][turn%#char[1]+1] |
scr[y][x]=charor(char[dir+1][turn%#char[1]+1],scr[y][x] or " ") |
||
dir = (dir+turn) % #move |
dir = (dir+turn) % #move |
||
x, y = x+move[dir+1].x,y+move[dir+1].y |
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 |
turn=0 |
||
elseif c=="-" then |
elseif c=="-" then |
||
turn=1 |
turn=turn+1 |
||
elseif c=="+" then |
|||
turn=-1 |
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 |
||
end |
end |
||
for i= |
for i=maxy,miny,-1 do |
||
local oneline={} |
|||
⚫ | |||
for x=minx,maxx do -- draw full canvas fill in nil with " " |
|||
oneline[1+x-minx]=scr[i] and scr[i][x] or " " |
|||
end |
|||
⚫ | |||
io.write(line, "\n") |
|||
end |
end |
||
end |
end |
||
Line 449: | Line 474: | ||
draw(str:hilbert(n)) |
draw(str:hilbert(n)) |
||
</lang> |
</lang> |
||
{{output}} |
{{output}} lua hilbert.lua 4 AF-FFFF++FF-FF-FF++FFFF-F-FFFF+FF+FF+FF++FFF+FF+FFF |
||
<pre> |
<pre> |
||
┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐ |
|||
┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐┌┐ |
|||
│└┘││└┘││└┘││└┘│ |
|||
│└┘││└┘││└┘││└┘││└┘││└┘││└┘││└┘│ |
|||
└┐┌┘└┐┌┘└┐┌┘└┐┌┘ |
|||
└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘└┐┌┘ |
|||
┌┘└──┘└┐┌┘└──┘└┐ |
|||
┌┘└──┘└┐┌┘└──┘└┐┌┘└──┘└┐┌┘└──┘└┐ |
|||
│┌─┐┌─┐││┌─┐┌─┐│ |
|||
│┌─┐┌─┐││┌─┐┌─┐││┌─┐┌─┐││┌─┐┌─┐│ |
|||
└┘┌┘└┐└┘└┘┌┘└┐└┘ |
|||
└┘┌┘└┐└┘└┘┌┘└┐└┘└┘┌┘└┐└┘└┘┌┘└┐└┘ |
|||
┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐ |
|||
┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐┌┐└┐┌┘┌┐ |
|||
│└─┘└─┘└┘└─┘└─┘│ |
|||
│└─┘└─┘└┘└─┘└─┘││└─┘└─┘└┘└─┘└─┘│ |
|||
└┐┌─┐┌─┐┌─┐┌─┐┌┘ |
|||
└┐┌─┐┌─┐┌─┐┌─┐┌┘└┐┌─┐┌─┐┌─┐┌─┐┌┘ |
|||
┌┘└┐└┘┌┘└┐└┘┌┘└┐ |
|||
┌┘└┐└┘┌┘└┐└┘┌┘└┐┌┘└┐└┘┌┘└┐└┘┌┘└┐ |
|||
│┌┐│┌┐└┐┌┘┌┐│┌┐│ |
|||
│┌┐│┌┐└┐┌┘┌┐│┌┐││┌┐│┌┐└┐┌┘┌┐│┌┐│ |
|||
└┘└┘│└─┘└─┘│└┘└┘┐ ┐┌─┐ |
|||
└┘└┘│└─┘└─┘│└┘└┘└┘└┘│└─┘└─┘│└┘└┘ |
|||
┌┐┌┐│┌─┐┌─┐│┌┐┌┐│ ││ │ |
|||
┌┐┌┐│┌─┐┌─┐│┌┐┌┐┌┐┌┐│┌─┐┌─┐│┌┐┌┐ |
|||
│└┘│└┘┌┘└┐└┘│└┘│├─┤├─┴┐ |
|||
│└┘│└┘┌┘└┐└┘│└┘││└┘│└┘┌┘└┐└┘│└┘│ |
|||
└┐┌┘┌┐└┐┌┘┌┐└┐┌┘│ ││ │ |
|||
└┐┌┘┌┐└┐┌┘┌┐└┐┌┘└┐┌┘┌┐└┐┌┘┌┐└┐┌┘ |
|||
─┘└─┘└─┘└─┘└─┘└─┘ └┴──┘ |
|||
┌┘└─┘└─┘└─┘└─┘└──┘└─┘└─┘└─┘└─┘└┐ |
|||
│┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐│ |
|||
└┘┌┘└┐└┘┌┘└┐└┘┌┘└┐└┘┌┘└┐└┘┌┘└┐└┘ |
|||
┌┐└┐┌┘┌┐│┌┐│┌┐└┐┌┘┌┐│┌┐│┌┐└┐┌┘┌┐ |
|||
│└─┘└─┘│└┘└┘│└─┘└─┘│└┘└┘│└─┘└─┘│ |
|||
└┐┌──┐┌┘┌┐┌┐│┌─┐┌─┐│┌┐┌┐└┐┌──┐┌┘ |
|||
┌┘└┐┌┘└┐│└┘│└┘┌┘└┐└┘│└┘│┌┘└┐┌┘└┐ |
|||
│┌┐││┌┐│└┐┌┘┌┐└┐┌┘┌┐└┐┌┘│┌┐││┌┐│ |
|||
└┘└┘└┘└┘┌┘└─┘└─┘└─┘└─┘└┐└┘└┘└┘└┘ |
|||
┌┐┌┐┌┐┌┐└┐┌─┐┌─┐┌─┐┌─┐┌┘┌┐┌┐┌┐┌┐ |
|||
│└┘││└┘│┌┘└┐└┘┌┘└┐└┘┌┘└┐│└┘││└┘│ |
|||
└┐┌┘└┐┌┘│┌┐│┌┐└┐┌┘┌┐│┌┐│└┐┌┘└┐┌┘ |
|||
┌┘└──┘└┐└┘└┘│└─┘└─┘│└┘└┘┌┘└──┘└┐ |
|||
│┌─┐┌─┐│┌┐┌┐│┌─┐┌─┐│┌┐┌┐│┌─┐┌─┐│ |
|||
└┘┌┘└┐└┘│└┘│└┘┌┘└┐└┘│└┘│└┘┌┘└┐└┘ |
|||
┌┐└┐┌┘┌┐└┐┌┘┌┐└┐┌┘┌┐└┐┌┘┌┐└┐┌┘┌┐ |
|||
┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└ |
|||
</pre> |
</pre> |
||