Created
July 29, 2010 03:35
-
-
Save tanghaibao/497160 to your computer and use it in GitHub Desktop.
fern leaf fractal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Iterated_function_system to draw fern leaf | |
# http://en.wikipedia.org/wiki/Iterated_function_system | |
funcs = ( | |
lambda x, y: (0, .16*y), | |
lambda x, y: (.2*x-.26*y, .23*x+.22*y+1.6), | |
lambda x, y: (-.15*x+.28*y, .26*x+.24*y+.44), | |
lambda x, y: (.85*x+.04*y, -.04*x+.85*y+1.6), | |
) | |
# probability to choose re-write functions | |
t = (.01,.07,.07,.85) | |
# cumulative probability | |
func_probs = [sum(t[:i+1]) for i in range(len(t))] | |
import matplotlib.pyplot as plt | |
from random import random | |
from bisect import bisect | |
ITERATIONS = 100000 | |
x, y = 0., 0. | |
dots = [] | |
for i in range(ITERATIONS): | |
dots.append((x, y)) | |
r = bisect(func_probs, random()) | |
f = funcs[r] | |
x, y = f(x, y) # rewrite rules | |
xs, ys = zip(*dots) | |
plt.figure(1, (4,5)) | |
ax = plt.gca() | |
ax.set_axis_off() | |
ax.plot(xs, ys, "g.", ms=1) | |
ax.text(.05,.05, "My dear frond", transform=ax.transAxes) | |
plt.savefig("ifs.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment