
A Barnsley fern is a fractal named after British mathematician Michael Barnsley and can be created using an iterated function system (IFS).



Task

Create this fractal fern, using the following transformations:

 ƒ1   (chosen 1% of the time)
        xn + 1 = 0
        yn + 1 = 0.16 yn

 ƒ2   (chosen 85% of the time)
        xn + 1 = 0.85 xn + 0.04 yn
        yn + 1 = −0.04 xn + 0.85 yn + 1.6

 ƒ3   (chosen 7% of the time)
        xn + 1 = 0.2 xn − 0.26 yn
        yn + 1 = 0.23 xn + 0.22 yn + 1.6

 ƒ4   (chosen 7% of the time)
        xn + 1 = −0.15 xn + 0.28 yn
        yn + 1 = 0.26 xn + 0.24 yn + 0.44.

Starting position: x = 0, y = 0




int  N, R;
real NX, NY, X, Y;
[SetVid($12);           \set 640x480x4 VGA graphics (on PC or RPi)
X:= 0.0;  Y:= 0.0;
for N:= 0 to 200_000 do
        [R:= Ran(100);  \0..99
        case of
        R < 1:  [NX:= 0.0;              NY:= 0.16*Y];
        R < 8:  [NX:= 0.20*X - 0.26*Y;  NY:= 0.23*X + 0.22*Y + 1.60];
        R < 15: [NX:=-0.15*X + 0.28*Y;  NY:= 0.26*X + 0.24*Y + 0.44]
        other   [NX:= 0.85*X + 0.04*Y;  NY:=-0.04*X + 0.85*Y + 1.60];
        X:= NX;  Y:= NY;
        Point(320+fix(X*40.0), 440-fix(Y*40.0), 2\green\);
        ]
]