Hilbert curve using turtle graphics and recursion











up vote
0
down vote

favorite












I'm trying to implement an L-System generated Hilbert curve ,making use of python turtle graphics and recursion. My code seems to be working for the first two levels of recursion n=1 and n=2 but beyond that , the graphics just entangled (although I´m able to observe further modules within them), and I can´t seem to grasp what might be wrong here, do I need some intermediate steps to regenerate the Hilbert modules for deeper levels of recursion? Please see my code below , its relatively simple:



import turtle

def Hilbert_curve(A,rule,t,n):

if n>=1:
if rule:
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
t.right(90)
Hilbert_curve(A, rule,t, n-1)
t.forward(A)
Hilbert_curve(A,rule,t, n-1)
t.right(90)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
else:
t.right(90)
Hilbert_curve(A,rule,t, n-1)
t.forward(A)
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
t.forward(A)
Hilbert_curve(A, rule,t, n-1)
t.right(90)

def main():
A=10
t=turtle.Turtle()
my_win=turtle.Screen()
n=2
rule=True
Hilbert_curve(A,rule,t,n)
my_win.exitonclick()

main()


Hilbert n=2



Hilbert n=3










share|improve this question
























  • if you like R, here's the one line code n=scan();a=1+1i;b=1-1i;z=0;for(k in 1:n)z=c((w<-1i*Conj(z))-a,z-b,z+a,b-w)/2;plot(z,t="s") just change n in for loop, e.g. for(k in 1:5)z=... This doesn't solve your problem here, but just in case you want to look around :)
    – Dejan Marić
    Nov 10 at 22:34












  • Well I´m not familiar with R so I don´t really understand what´s your line of code but I´ll check it out
    – JFT
    Nov 11 at 15:25















up vote
0
down vote

favorite












I'm trying to implement an L-System generated Hilbert curve ,making use of python turtle graphics and recursion. My code seems to be working for the first two levels of recursion n=1 and n=2 but beyond that , the graphics just entangled (although I´m able to observe further modules within them), and I can´t seem to grasp what might be wrong here, do I need some intermediate steps to regenerate the Hilbert modules for deeper levels of recursion? Please see my code below , its relatively simple:



import turtle

def Hilbert_curve(A,rule,t,n):

if n>=1:
if rule:
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
t.right(90)
Hilbert_curve(A, rule,t, n-1)
t.forward(A)
Hilbert_curve(A,rule,t, n-1)
t.right(90)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
else:
t.right(90)
Hilbert_curve(A,rule,t, n-1)
t.forward(A)
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
t.forward(A)
Hilbert_curve(A, rule,t, n-1)
t.right(90)

def main():
A=10
t=turtle.Turtle()
my_win=turtle.Screen()
n=2
rule=True
Hilbert_curve(A,rule,t,n)
my_win.exitonclick()

main()


Hilbert n=2



Hilbert n=3










share|improve this question
























  • if you like R, here's the one line code n=scan();a=1+1i;b=1-1i;z=0;for(k in 1:n)z=c((w<-1i*Conj(z))-a,z-b,z+a,b-w)/2;plot(z,t="s") just change n in for loop, e.g. for(k in 1:5)z=... This doesn't solve your problem here, but just in case you want to look around :)
    – Dejan Marić
    Nov 10 at 22:34












  • Well I´m not familiar with R so I don´t really understand what´s your line of code but I´ll check it out
    – JFT
    Nov 11 at 15:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to implement an L-System generated Hilbert curve ,making use of python turtle graphics and recursion. My code seems to be working for the first two levels of recursion n=1 and n=2 but beyond that , the graphics just entangled (although I´m able to observe further modules within them), and I can´t seem to grasp what might be wrong here, do I need some intermediate steps to regenerate the Hilbert modules for deeper levels of recursion? Please see my code below , its relatively simple:



import turtle

def Hilbert_curve(A,rule,t,n):

if n>=1:
if rule:
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
t.right(90)
Hilbert_curve(A, rule,t, n-1)
t.forward(A)
Hilbert_curve(A,rule,t, n-1)
t.right(90)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
else:
t.right(90)
Hilbert_curve(A,rule,t, n-1)
t.forward(A)
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
t.forward(A)
Hilbert_curve(A, rule,t, n-1)
t.right(90)

def main():
A=10
t=turtle.Turtle()
my_win=turtle.Screen()
n=2
rule=True
Hilbert_curve(A,rule,t,n)
my_win.exitonclick()

main()


Hilbert n=2



Hilbert n=3










share|improve this question















I'm trying to implement an L-System generated Hilbert curve ,making use of python turtle graphics and recursion. My code seems to be working for the first two levels of recursion n=1 and n=2 but beyond that , the graphics just entangled (although I´m able to observe further modules within them), and I can´t seem to grasp what might be wrong here, do I need some intermediate steps to regenerate the Hilbert modules for deeper levels of recursion? Please see my code below , its relatively simple:



import turtle

def Hilbert_curve(A,rule,t,n):

if n>=1:
if rule:
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
t.right(90)
Hilbert_curve(A, rule,t, n-1)
t.forward(A)
Hilbert_curve(A,rule,t, n-1)
t.right(90)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
else:
t.right(90)
Hilbert_curve(A,rule,t, n-1)
t.forward(A)
t.left(90)
Hilbert_curve(A,not rule,t, n-1)
t.forward(A)
Hilbert_curve(A,not rule,t, n-1)
t.left(90)
t.forward(A)
Hilbert_curve(A, rule,t, n-1)
t.right(90)

def main():
A=10
t=turtle.Turtle()
my_win=turtle.Screen()
n=2
rule=True
Hilbert_curve(A,rule,t,n)
my_win.exitonclick()

main()


Hilbert n=2



Hilbert n=3







python recursion turtle-graphics fractals hilbert-curve






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:07









cdlane

16.5k21042




16.5k21042










asked Nov 10 at 22:18









JFT

12




12












  • if you like R, here's the one line code n=scan();a=1+1i;b=1-1i;z=0;for(k in 1:n)z=c((w<-1i*Conj(z))-a,z-b,z+a,b-w)/2;plot(z,t="s") just change n in for loop, e.g. for(k in 1:5)z=... This doesn't solve your problem here, but just in case you want to look around :)
    – Dejan Marić
    Nov 10 at 22:34












  • Well I´m not familiar with R so I don´t really understand what´s your line of code but I´ll check it out
    – JFT
    Nov 11 at 15:25


















  • if you like R, here's the one line code n=scan();a=1+1i;b=1-1i;z=0;for(k in 1:n)z=c((w<-1i*Conj(z))-a,z-b,z+a,b-w)/2;plot(z,t="s") just change n in for loop, e.g. for(k in 1:5)z=... This doesn't solve your problem here, but just in case you want to look around :)
    – Dejan Marić
    Nov 10 at 22:34












  • Well I´m not familiar with R so I don´t really understand what´s your line of code but I´ll check it out
    – JFT
    Nov 11 at 15:25
















if you like R, here's the one line code n=scan();a=1+1i;b=1-1i;z=0;for(k in 1:n)z=c((w<-1i*Conj(z))-a,z-b,z+a,b-w)/2;plot(z,t="s") just change n in for loop, e.g. for(k in 1:5)z=... This doesn't solve your problem here, but just in case you want to look around :)
– Dejan Marić
Nov 10 at 22:34






if you like R, here's the one line code n=scan();a=1+1i;b=1-1i;z=0;for(k in 1:n)z=c((w<-1i*Conj(z))-a,z-b,z+a,b-w)/2;plot(z,t="s") just change n in for loop, e.g. for(k in 1:5)z=... This doesn't solve your problem here, but just in case you want to look around :)
– Dejan Marić
Nov 10 at 22:34














Well I´m not familiar with R so I don´t really understand what´s your line of code but I´ll check it out
– JFT
Nov 11 at 15:25




Well I´m not familiar with R so I don´t really understand what´s your line of code but I´ll check it out
– JFT
Nov 11 at 15:25












1 Answer
1






active

oldest

votes

















up vote
2
down vote













The problem is with your else clause. The rule is already inverted coming in to the function, so you need to treat the rule the same as the then clause:



    else:
t.right(90)
Hilbert_curve(A, not rule, t, n - 1)
t.forward(A)
t.left(90)
Hilbert_curve(A, rule, t, n - 1)
t.forward(A)
Hilbert_curve(A, rule, t, n - 1)
t.left(90)
t.forward(A)
Hilbert_curve(A, not rule, t, n - 1)
t.right(90)


However, if we change rule from a boolean to a number, parity, that's either 1 or -1, and then multiply parity by the angle, we can eliminate one of the two clauses of the orignal if statement:



from turtle import Screen, Turtle

def hilbert_curve(turtle, A, parity, n):

if n < 1:
return

turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)

screen = Screen()

yertle = Turtle()
yertle.speed('fastest') # because I have no patience

hilbert_curve(yertle, 10, 1, 4)

screen.exitonclick()


enter image description here






share|improve this answer





















  • Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
    – JFT
    Nov 11 at 15:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243985%2fhilbert-curve-using-turtle-graphics-and-recursion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













The problem is with your else clause. The rule is already inverted coming in to the function, so you need to treat the rule the same as the then clause:



    else:
t.right(90)
Hilbert_curve(A, not rule, t, n - 1)
t.forward(A)
t.left(90)
Hilbert_curve(A, rule, t, n - 1)
t.forward(A)
Hilbert_curve(A, rule, t, n - 1)
t.left(90)
t.forward(A)
Hilbert_curve(A, not rule, t, n - 1)
t.right(90)


However, if we change rule from a boolean to a number, parity, that's either 1 or -1, and then multiply parity by the angle, we can eliminate one of the two clauses of the orignal if statement:



from turtle import Screen, Turtle

def hilbert_curve(turtle, A, parity, n):

if n < 1:
return

turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)

screen = Screen()

yertle = Turtle()
yertle.speed('fastest') # because I have no patience

hilbert_curve(yertle, 10, 1, 4)

screen.exitonclick()


enter image description here






share|improve this answer





















  • Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
    – JFT
    Nov 11 at 15:38















up vote
2
down vote













The problem is with your else clause. The rule is already inverted coming in to the function, so you need to treat the rule the same as the then clause:



    else:
t.right(90)
Hilbert_curve(A, not rule, t, n - 1)
t.forward(A)
t.left(90)
Hilbert_curve(A, rule, t, n - 1)
t.forward(A)
Hilbert_curve(A, rule, t, n - 1)
t.left(90)
t.forward(A)
Hilbert_curve(A, not rule, t, n - 1)
t.right(90)


However, if we change rule from a boolean to a number, parity, that's either 1 or -1, and then multiply parity by the angle, we can eliminate one of the two clauses of the orignal if statement:



from turtle import Screen, Turtle

def hilbert_curve(turtle, A, parity, n):

if n < 1:
return

turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)

screen = Screen()

yertle = Turtle()
yertle.speed('fastest') # because I have no patience

hilbert_curve(yertle, 10, 1, 4)

screen.exitonclick()


enter image description here






share|improve this answer





















  • Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
    – JFT
    Nov 11 at 15:38













up vote
2
down vote










up vote
2
down vote









The problem is with your else clause. The rule is already inverted coming in to the function, so you need to treat the rule the same as the then clause:



    else:
t.right(90)
Hilbert_curve(A, not rule, t, n - 1)
t.forward(A)
t.left(90)
Hilbert_curve(A, rule, t, n - 1)
t.forward(A)
Hilbert_curve(A, rule, t, n - 1)
t.left(90)
t.forward(A)
Hilbert_curve(A, not rule, t, n - 1)
t.right(90)


However, if we change rule from a boolean to a number, parity, that's either 1 or -1, and then multiply parity by the angle, we can eliminate one of the two clauses of the orignal if statement:



from turtle import Screen, Turtle

def hilbert_curve(turtle, A, parity, n):

if n < 1:
return

turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)

screen = Screen()

yertle = Turtle()
yertle.speed('fastest') # because I have no patience

hilbert_curve(yertle, 10, 1, 4)

screen.exitonclick()


enter image description here






share|improve this answer












The problem is with your else clause. The rule is already inverted coming in to the function, so you need to treat the rule the same as the then clause:



    else:
t.right(90)
Hilbert_curve(A, not rule, t, n - 1)
t.forward(A)
t.left(90)
Hilbert_curve(A, rule, t, n - 1)
t.forward(A)
Hilbert_curve(A, rule, t, n - 1)
t.left(90)
t.forward(A)
Hilbert_curve(A, not rule, t, n - 1)
t.right(90)


However, if we change rule from a boolean to a number, parity, that's either 1 or -1, and then multiply parity by the angle, we can eliminate one of the two clauses of the orignal if statement:



from turtle import Screen, Turtle

def hilbert_curve(turtle, A, parity, n):

if n < 1:
return

turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)

screen = Screen()

yertle = Turtle()
yertle.speed('fastest') # because I have no patience

hilbert_curve(yertle, 10, 1, 4)

screen.exitonclick()


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 8:31









cdlane

16.5k21042




16.5k21042












  • Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
    – JFT
    Nov 11 at 15:38


















  • Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
    – JFT
    Nov 11 at 15:38
















Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
– JFT
Nov 11 at 15:38




Thank you , this works very well. Thank you for giving me the parity variable idea , I hadn´t considered before . My idea was to generate two cases for the two generation rules given in the L-System. Your parity solution is much more elegant
– JFT
Nov 11 at 15:38


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243985%2fhilbert-curve-using-turtle-graphics-and-recursion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues