I'm trying to plot a Moore curve (https://en.wikipedia.org/wiki/Moore_curve) in python. I've gotten as far as plotting a Hillbert curve, as there seems to be more resources on Hillbert vs Moore, but it's not clear to me how to edit the subsequent iterations of the curve to make it plot the Moore curve correctly.
This is the python code for the Hillbert curve:
def d2xy(n,d):
t=d
x=y=0
s=1
while s<n:
rx=1&(t/2)
ry=1&(t^rx)
s,x,y,rx,ry=rot(s,x,y,rx,ry)
x+=s*rx
y+=s*ry
t/=4
s*=2
return x,y
def rot(n,x,y,rx,ry):
if ry==0:
if rx==1:
x=n-1-x
y=n-1-y
x,y=y,x
return n,x,y,rx,ry
How can I change this to plot the Moore curve?
The Wikipedia page that you cite spells out how to do it using turtle graphics and an L-system. Just following those instructions, I came up with:
And the fun part is, having implemented our L-system, by just changing the data, not the code, we can also make the Hilbert curve:
Of course, the calculation of the
distancevariable does need a bit of work...