1

The problem is that I don't really know how to start writing the code. I need a starting point. I do know how to apply an rewriting production rule to one single parametre, for instance the Koch curve. The problem comes when the number of production rules are more than one, I mean for instance:

(Sierpinski's Gasket)

G = {V, ω, P}
V = {F, X, +, -}
P1 (Production rule 1) = X --> F + X + F
P2 (Production rule 2) = F --> X - F - X

My question is: how do you integrate this into a python turtle script? How do you tell to python to replace the X set of commands, recursively? I tried creating two procedures, one for X and one for F, but I cannot get it right. How can you replace an X by other words such as (F + X + F) in the example? Is there any straight-forward way yo translate this?

I am so sorry guys that I am so lost, it is still quite abstract for me when I think about the recursive programming and the fractal elements...

Thanks in advance!

1 Answer 1

2

Well, for implementing in any language, you have to translate into the terms of the language. One way is to use strings of characters, iterate through and transform the strings directly. This results in a large string for higher iterations. Then iterate through this resulting string and interpret the characters as turtle commands.

A variant of the string-of-chars, would be to use an array-of-some-kind-of-symbol-type. This same macro-expansion approach will still work.

Another way would be to translate the productions into functions which make the graphics calls directly and make (mutually-)recursive calls to other productions. You need some way of controlling the depth of the recursion. Easiest is to pass an extra variable that gets incremented at each call and check this against a global max_depth variable in each production function.

Another way would be to hybridize the above two approaches, use a set of strings or arrays to model the L-system and perform the recursion in a separate handler function (which can be the same for many different L-systems, ie. it's abstracted, de-coupled). When recursive handle_production() function detects depth == max_depth it interprets the symbol as a turtle command instead of recursing.

HTH. There are a number of different approaches exhibited in the answers to my code-golf question: “Sorry, young man, but it's Turtles all the way down!”

1
  • Cheers man, this is such a useful approach. I will work on it. This question you attached me is full of interesting information I can use, that is very good, thanks. Jan 31, 2014 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.