Barnsley Fern in Swift

Sayler8182
Sep 1, 2018 · 2 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade