Skip to content

Instantly share code, notes, and snippets.

@adusak
Created June 2, 2015 17:31
Show Gist options
  • Save adusak/adca04953891fa4e1db5 to your computer and use it in GitHub Desktop.
Save adusak/adca04953891fa4e1db5 to your computer and use it in GitHub Desktop.
Fern and Star
import copy
from python.common.point import Point
from python.common.svg import Svg
from python.less9.transformations import *
def common(size, iterations, transformations, probabilities, name="picture"):
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/" + name)
def star(size, iterations=1000):
transformations = [np.matrix([[0.255, 0.0, 0.3726],
[0.0, 0.255, 0.6714],
[0, 0, 1]]),
np.matrix([[0.255, 0.0, 0.1146],
[0.0, 0.255, 0.2232],
[0, 0, 1]]),
np.matrix([[0.255, 0.0, 0.6306],
[0.0, 0.255, 0.2232],
[0, 0, 1]]),
np.matrix([[0.370, -0.642, 0.6356],
[0.642, 0.370, -0.0061],
[0, 0, 1]])]
probabilities = [0.07, 0.30, 0.33, 0.30]
common(size, iterations, transformations, probabilities, "star")
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]
common(size, iterations, transformations, probabilities, "fern")
fern(500, 10000)
star(1500, 100000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment