Barnsley Fern in Swift

Sayler8182
2 min readSep 1, 2018

--

If you don’t know what Barnsley Fern is, You can read more about it here.
In short a Barnsley Fern is a fractal named that can be created using an iterated function system. Function correspond to the following transformations depends on probability:

  • ƒ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.
Transformation in swift

Precision of fern depends on numbers of iterations. The more repetitions, the more outlined picture.

Source

Here you can find full generation algorithm, or you can visit my example project:

Thank you for reading! If you liked this article, please clap so other people can read it too. I will also have more motivation for next article :) You can also see my other articles, maybe you will find something for you.

If you have any question or suggestion leave a comment.

--

--