the Barnsley’s Fern with Base Tools in R

3 minute read

After learning about the Mandelbrot set, I wanted to play around with the very a basic fractal- Barnsley’s Fern. Again, fractal geometry is based on the idea of scale invariance which means that a figure is the same, or is invariant, no matter on what scale it is observed. In other words, the figure is constructed by repeating the same pattern at smaller and smaller scales.

The fern was first represented into a mathametical context by Dr. Michael Barnsley. He first described it in his book “Fractals Everywhere.” The fern is one of the basic examples of self-similar sets, i.e. it is a pattern that can be reproducible at any magnification or reduction. The fern code developed by Dr. Barnsley is an example of an iterated function system (IFS) to create a fractal. On a quick note, an IFS fractals, as they are normally called, can be of any number of dimensions, but are commonly computed and drawn in 2D. This type of fractal is made up of the union of several copies of itself, each copy being transformed by a function (hence “function system”).

Anyhow, let’s talk about how I drew this fractal in open-source R. Also, please feel free to look through the source code by going here.

To begin with, to make this kind of fractal, you would need some sort of 2D space, like a plane, and a container to hold transformations, like a matrix. You would start at (0,0), then the new points are iteratively computed by randomly applying one of the four following linear algebraic coordinate transformation-

f: xn = 0, yn = 0 (start and iterate from this point)

(f1 is chosen 1% of the time)
f1: xn+1 = 0, yn+1 = 0.16 yn

(f2 ischosen 7% of the time)
f2: xn+1 = -0.15 xn + 0.28 yn, yn+1 = 0.26 xn + 0.24 yn + 0.44

(f3 is chosen 14% of the time)
f3: xn+1 = 0.2 xn - 0.26 yn, yn+1 = 0.23 xn + 0.22 yn + 1.6

(f4 is chosen 85% of the time)
f4: xn+1 = 0.85 xn + 0.04 yn, yn+1 = -0.04 xn + 0.85 yn + 1.6

Underneath these matrix transformation, there is one common rule: first you do the dot product, and then you add and that gives you a next point which you store in a matrix. The resultant point would be in a 1 column, 2 rows matrix. Rather than storing the next points in the matrix, I have made a two vector x & y. Just like the matrix, vectors are arrays and works in similar fashion.

In R, it looks something like this-

x <- rep(0, times=max_itr) #make vector of 0s
y <- x #copy the vector

# This creats f(x1,y1) = (0,0)
#run the iteration to generate next points

for (n in 2:(max_itr)) { 

# I have subtracted 1 at every iteration from n so I dont 
# have to add 1 to f(x), basically subtracted it from both 
# sides.

  #generate a random number between 0-100
  rand_num=runif(1, 0, 100) 
  
  if (rand_num < 1) { 
    x[n]<- 0
    y[n]<- 0.16*y[n-1]
  }
  else if (rand_num < 7){
    x[n]<- -0.15*x[n-1]+0.28*y[n-1]
    y[n]<- 0.26*x[n-1]+0.24*y[n-1]+0.44
  }
  else if (rand_num < 14){
    x[n]<- 0.2*x[n-1]-0.26*y[n-1]
    y[n]<- 0.23*x[n-1]+0.22*y[n-1]+1.6
  }
  else {
    x[n]<- 0.85*x[n-1]+0.04*y[n-1]
    y[n]<- -0.04*x[n-1]+0.85*y[n-1]+1.6
  }
  
}

As you can see, the process itself is very simple because unlike matrix, you can store points in linear manner, or “Row-major-order”, in these vectors. In matrix however, storing happens in “Column-major-order” by default, meaning, points will be in order of the first column in memory first, and then the second, and so forth. This method gets tricky.

One important thing to note is that, the random numbers are generated between -2.1820 < x < 2.6558 and 0 ≤ y < 9.9983. This give you the correct Rachis (stalk) arch of the fern.

The plot is as simple as this-

plot(x,y, pch='.', xlab= "", ylab = "", col= color)

Here is mine, looks good I think-

For visual people who want to see how the fern is made, please checkout the link here.

And if you want to learn more about fern fractal, please go here to read more about its process and matrix representation.

NOTE: Thank you very much for reading! If you discover any mistakes or want to offer any feedback, please feel free to email me.