Skip to content

Instantly share code, notes, and snippets.

@adusak
Created June 2, 2015 17:18
Show Gist options
  • Save adusak/b4deebdaa817963196e7 to your computer and use it in GitHub Desktop.
Save adusak/b4deebdaa817963196e7 to your computer and use it in GitHub Desktop.
Barnsley fern
import copy
from python.common.point import Point
from python.common.svg import Svg
from python.less9.transformations import *
def fern(size, iterations):
transformations = [np.matrix([[0.849, 0.037, 0.075],
[-0.037, 0.849, 0.183],
[0, 0, 1]]),
np.matrix([[0.197, -0.226, 0.4],
[0.226, 0.197, 0.049],
[0, 0, 1]]),
np.matrix([[-0.15, 0.283, 0.575],
[0.26, 0.237, 0.084],
[0, 0, 1]]),
np.matrix([[0, 0, 0.5],
[0, 0.16, 0],
[0, 0, 1]])]
probabilities = [0.85, 0.07, 0.07, 0.01]
points = []
current = Point(0, 0)
points.append(current)
for iteration in range(iterations):
index = np.random.choice(4, 1, p=probabilities)[0]
transformation = transformations[index]
copy_p = copy.deepcopy(current)
copy_p.apply_transformation(transformation)
current = copy_p
if iteration > 200:
points.append(copy_p)
svg = Svg()
list(map(lambda y: y.draw_to_svg(svg), map(lambda x: x.apply_transformation(scaling(size, size)), points)))
svg.save("output/" + "crystal")
# mrcm(polygon, transformations, iterations, "fern")
fern(500, 10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment