{"id":479,"date":"2007-07-30T21:30:37","date_gmt":"2007-07-30T21:30:37","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2007\/07\/30\/l-system-fractals\/"},"modified":"2018-11-27T20:27:47","modified_gmt":"2018-11-28T01:27:47","slug":"l-system-fractals","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2007\/07\/30\/l-system-fractals\/","title":{"rendered":"L-System Fractals"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"fb.gif\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_223.gif?resize=150%2C272\" width=\"150\" height=\"272\" class=\"inset right\" \/><\/p>\n<p> In the post about Koch curves, I talked about how a grammar-rewrite system could be used to describe fractals. There&#8217;s a bit more to the grammar idea that I originally suggested. There&#8217;s something called an L-system (short for <em>Lindenmayer system<\/em>, after Aristid Lindenmayer, who invented it for describing the growth patterns of plants), which is a variant of the Thue grammar, which is extremely useful for generating a wide range of interesting fractals for describing plant growth, turbulence patterns, and lots of other things.<\/p>\n<p><!--more--><\/p>\n<p> The main difference between an L-system and a simple grammar rewrite system is that in a simple rewrite system, in each step, you replace an occurence (or sometimes all occurences) of <em>one<\/em> symbol &#8211; the left-hand side of the grammar rule &#8211; with the contents of the right-hand side of the grammar rule. In an L-system, in each step, you replace <em>all<\/em> instances of <em>all<\/em> symbols that match any rule <em>as a single atomic step.<\/em><\/p>\n<p> Both simple rewrite systems and L-systems take the same inputs &#8211; a grammar, but the behave differently. If you think of them as computation systems, L-systems have a kind of parallelism to them. Let&#8217;s look at how they&#8217;re made up, and then I&#8217;ll show you an example of how different their results can be.<\/p>\n<p> The grammar of L-systems and simple rewrite systems consist of:<\/p>\n<ol>\n<li> A set <img src='http:\/\/l.wordpress.com\/latex.php?latex=S&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='S' style='vertical-align:1%' class='tex' alt='S' \/> of symbols, called <em>non-terminal symbols<\/em><\/li>\n<li> A set <img src='http:\/\/l.wordpress.com\/latex.php?latex=T&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='T' style='vertical-align:1%' class='tex' alt='T' \/> of symbols, called <em>non-terminal symbols<\/em><\/li>\n<li> A set <img src='http:\/\/l.wordpress.com\/latex.php?latex=R&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='R' style='vertical-align:1%' class='tex' alt='R' \/> of <em>rewrite-rules<\/em> that map from a symbol <img src='http:\/\/l.wordpress.com\/latex.php?latex=l%5Cin%20S&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='l\\in S' style='vertical-align:1%' class='tex' alt='l\\in S' \/> to a sequence of symbols from <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Ctext%7Brhs%7D%20%5Cin%20%28S%5Ccup%20T%29%5E%2A&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\text{rhs} \\in (S\\cup T)^*' style='vertical-align:1%' class='tex' alt='\\text{rhs} \\in (S\\cup T)^*' \/>. Each rule in R says that the non-terminal symbol l can be replaced by the sequence of symbols in rhs.<\/li>\n<li> A special distinguished symbol in S called the <em>initiator<\/em>. Every use of the rewrite system will start with a single instance of the initiator.<\/li>\n<\/ol>\n<p> To demonstrate the difference, let&#8217;s look at a simple grammar, and its evolution as an L-system compared to a simple rewrite system. The non-terminal symbols consist of {A,B}; the terminal symbols set will be empty; and the initiator will be &#8220;A&#8221;.<\/em><\/p>\n<ul>\n<li> A &rarr; AB<\/li>\n<li> B &rarr; A<\/li>\n<\/ul>\n<p> Here&#8217;s a sequence of steps from the simple rewrite and the L-system:<\/p>\n<table>\n<tr>\n<th>Simple rewrite<\/th>\n<th>L-system<\/th>\n<\/tr>\n<tr>\n<td>AB<\/td>\n<td>AB<\/td>\n<\/tr>\n<tr>\n<td>AA<\/td>\n<td>ABA<\/td>\n<\/tr>\n<tr>\n<td>ABAB<\/td>\n<td> ABAAB<\/td>\n<\/tr>\n<tr>\n<td>AAAA<\/td>\n<td>ABAABABA<\/td>\n<\/tr>\n<tr>\n<td>ABABABAB<\/td>\n<td>ABAABABAABAAB<\/td>\n<\/tr>\n<\/table>\n<p> The parallelism of the L-system is very useful in defining fractals; it&#8217;s part of the key to why some fractals can be described so easily and naturally using L-systems: the self-similarity of the fractal in different positions corresponds to the parallel expansions. For example, here&#8217;s a version of Cantor&#8217;s dust as an L-system:<\/p>\n<ol>\n<li> Non-terminals: {B,W}; terminals={}<\/li>\n<li> Rules: B&rarr;BWB, W&rarr;WWW<\/li>\n<li> Initiator: B<\/li>\n<\/ol>\n<p> A series of steps of this are: &#8220;B&#8221;, &#8220;BWB&#8221;, &#8220;BWBWWWBWB&#8221;, &#8220;BWBWWWBWBWWWWWWWWWBWBWWWBWB&#8221;. If you think of &#8220;B&#8221; as &#8220;black point&#8221;, and &#8220;W&#8221; as &#8220;white point&#8221;, then you can easily see that the limit of this L-system is a drawing of the Cantor dust. And you can also see how the parallelism of the L-system makes it work so easily. Without it, you&#8217;d need to do something like horizontal passes across the string; and to do that, you would need to add some kind of place-holder into the rules to keep track of which symbols had been modified by a given pass. The L-system makes it much easier.<\/p>\n<p> A neater example of how we can do fractals using L-systems is a tracing of the Sierpinski gasket. This is very typical of how we do fractal curves with L-systems: the grammar generates a set of instructions for a pen or turtle. (There are other tricks for how to use L-systems for higher dimension structures, but for this post, we&#8217;ll stick with curves.<\/p>\n<p> To trace the gasket, we&#8217;ll use a set of symbols with meanings for turtle drawings:<\/p>\n<ul>\n<li> &#8220;+&#8221; is a terminal symbol which means &#8220;turn right 60 degrees&#8221;.<\/li>\n<li> &#8220;-&#8221; is a terminal symbol which means &#8220;turn left 60 degrees&#8221;.<\/li>\n<li> &#8220;1&#8221; and &#8220;2&#8221; are both <em>non-terminal<\/em> symbols which mean &#8220;go forward&#8221;. We use the two different symbols for &#8220;forward&#8221; because there are two different kinds of expansions, which should<br \/>\nalternate.\n<\/li>\n<\/ul>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"sier-trace.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_224.png?resize=206%2C157\" width=\"206\" height=\"157\" class=\"inset right\" \/><\/p>\n<ol>\n<li> Rules:\n<ul>\n<li> 1 &rarr; 2-1-2<\/li>\n<li> 2 &rarr; 1+2+1<\/li>\n<\/ul>\n<li> Initiator: 1<\/li>\n<\/ol>\n<p> Over to the right is the sierpinski tracing after 6 iterations.<\/p>\n<p> One of the more famous self-intersecting fractal curves, the dragon curve, is very natural as an L-system; it involves one more trick beyond what we&#8217;ve done so far &#8211; the instruction string is going to include no-ops to drive the expansions; all of the instructions are terminal symbols; non-terminals are interspersed, and do the actual work of generating the fractal structure.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"dragon.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_225.png?resize=195%2C198\" width=\"195\" height=\"198\" class=\"inset right\" \/><\/p>\n<ol>\n<li>Terminals:\n<ul>\n<li> +: right 90 degrees<\/li>\n<li> -: left 90 degrees<\/li>\n<\/ul>\n<li> Rules:\n<ul>\n<li> 1 &rarr; 1+2f+<\/li>\n<li> 2 &rarr; -f1-2<\/li>\n<\/ul>\n<li> Initiator: &#8220;f1&#8221;<\/li>\n<\/ol>\n<p> Over to the right is the dragon after 8 iterations.<\/p>\n<p> One last one for today &#8211; it&#8217;s quite hairy for a planar L-system, but it&#8217;s a good one! I came across this on Wikipedia; I haven&#8217;t seen it elsewhere described via an L-system. It&#8217;s a fractal fern &#8211; a pattern that comes out looking like a sprig of dill or a frond of fennel.  The complexity of it comes from the way it handles the branches in the fronds: in addition to the use of non-terminals as expansion drivers, it also uses a stack. The stack marks a position and direction that the turtle was facing at a particular point in time. There&#8217;s a terminal symbol that indicates that the current position\/heading should be pushed onto the stack, and a terminal that indicates that the stack should be popped, and the turtle set to the position heading in the popped value.<\/p>\n<ol>\n<li> Terminals:\n<ul>\n<li> <b>Fwd<\/b>: move forwards drawing a line.<\/li>\n<li> <b>Right<\/b>: rotate right 25 degrees.<\/li>\n<li> <b>Left<\/b>: rotate left 25 degrees.<\/li>\n<li> <b>[<\/b>: push turtle position\/heading.<\/li>\n<li> <b>]<\/b>: pop turtle position\/heading.<\/li>\n<\/ul>\n<\/li>\n<li> Non-terminals:\n<ul>\n<li><b>X<\/b><\/li>\n<\/ul>\n<\/li>\n<li> Rules:\n<ul>\n<li> X &rarr; Fwd,Left[[X]Right,X]Right,Fwd[Right,Fwd,X]Left,X <\/li>\n<li> Fwd &rarr; Fwd,Fwd<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p> And here&#8217;s the result of running it for 6 iterations:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"fern.png\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_226.png?resize=387%2C288\" width=\"387\" height=\"288\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the post about Koch curves, I talked about how a grammar-rewrite system could be used to describe fractals. There&#8217;s a bit more to the grammar idea that I originally suggested. There&#8217;s something called an L-system (short for Lindenmayer system, after Aristid Lindenmayer, who invented it for describing the growth patterns of plants), which is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[86],"tags":[152,159,189],"class_list":["post-479","post","type-post","status-publish","format-standard","hentry","category-fractals","tag-fern","tag-fractal","tag-l-system"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-7J","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=479"}],"version-history":[{"count":2,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/479\/revisions"}],"predecessor-version":[{"id":3672,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/479\/revisions\/3672"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=479"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}