A Barnsley’s Fern In 7 Lines of Mathematica

I used to draw a Barnsley’s fern with a program written in Pascal when I was 19 years old. Yesterday someone asked about it in a forum I visited, so I drew another one using Mathematica. The code is much shorter this time. (I’m sure that many people can shorten it even more.)
Here’s the code to draw the fern with 10,000 points. You can copy and paste and run it in Mathematica:

          

ifsFern[p_] := Module[{i},
i = Random[Integer, 99];
If[i < 1, Return[{{0., 0.}, {0., .16}}.p ]];
If[i >= 1 && i < 86, Return[{{0.85, 0.04}, {-0.04, 0.85}}.p + {0., 1.6}]];
If[i >= 86 && i < 93, Return[{{0.20, -0.26}, {0.23, 0.22}}.p + {0., 1.6}]];
If[i >= 93, Return[{{-0.15, 0.28}, {0.26, 0.24}}.p + {0., 0.44}]]]


Graphics[{RGBColor[0, 0.5, 0], Point[NestList[ifsFern, {0, 0}, 10000]]}]


The result looks like this:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.