# [OpenSCAD] Barnsley Fern recursive

Eric Buijs ebuijs at mac.com
Wed Sep 12 05:20:22 EDT 2018

```You're right Torsten, I've tried the latest experimental build for OSX and it
makes a huge difference in memory. I've updated the code because I hadn't
been very thoughtful about the base case (see below for updated code). One
more thing. The recursive loop refuses to go beyond 1300 (or so). After that
I get the message: ERROR: Recursion detected calling module 'BarnsleyFern'
Any thoughts on that. Thanks guys.

m1 = [[0,0],[0,0.16]];

c1 = [0,0];

m2 = [[0.85,0.04],[-0.04,0.85]];

c2 = [0, 0.16];

m3 = [[0.2,-0.26],[0.23,0.22]];

c3 = [0,1.6];

m4 = [[-0.15,0.28],[0.26,0.24]];

c4 = [0,0.44];

function f1(p) = m1 * p + c1;

function f2(p) = m2 * p + c2;

function f3(p) = m3 * p + c3;

function f4(p) = m4 * p + c4;

module plotCircle(p) {
translate(100 * p) circle(r=0.5,\$fn=50);
}

module BarnsleyFern(p,index) {
r = rands(0,1,1)[0];
plotCircle(p);
//echo(r,index);
if (r <= 0.01) {
BarnsleyFern(f1(p), index-1);
}
else if (r > 0.01 && r <=0.86) {
BarnsleyFern(f2(p),index-1);
}
else if (r > 0.86 && r <=0.94) {
BarnsleyFern(f3(p),index-1);
}
else if (index > 0) {
BarnsleyFern(f4(p),index-1);
}
}

p = [0,0];
BarnsleyFern(p,1400);

--
Sent from: http://forum.openscad.org/

```

More information about the Discuss mailing list