Endlich konnte ich an "Programmgesteuert Farne zeichnen" teilnehmen!
Erstellt basierend auf dem Code von @ noc06140728. Vielen Dank.
# coding: utf-8
import random
N = 1000000
WIDTH = 500
HEIGHT = 500
fern = [
[1.0, [+0.836, +0.044, 0.0, -0.044, +0.836, 0.169]],
[0.3, [-0.141, +0.302, 0.0, +0.302, +0.0141,0.127]],
[0.3, [+0.141, -0.302, 0.0, +0.302, +0.141, 0.169]],
[0.3, [0.0, 0.0, 0.0, 0.0, 0.175337, 0.0]],
]
p_table = []
def W(n,v):
c = fern[n][1]
return (c[0] * v[0] + c[1] * v[1] + c[2], c[3] * v[0] + c[4] * v[1] + c[5])
# Python 2.7 zu itertools.Da es keine Akkumulation gibt, wird dies durch Ausleihen aus dem Folgenden vereinfacht
# http://docs.python.jp/3.3/library/itertools.html#itertools.accumulate
def accumulate(iterable):
it = iter(iterable)
total = next(it)
yield total
for e in it:
total = total + e
yield total
def g(v, n):
it = 0
while it < n:
it += 1
yield v
r = random.random()
for i, p in enumerate(p_table):
if r < p:
v = W(i,v)
break;
if __name__ == '__main__':
#Glätten Sie den Wert der Wahrscheinlichkeit
p_total = sum(f[0] for f in fern)
p_table = [a/p_total for a in accumulate([f[0] for f in fern])]
#Ein Puffer, der die Flugbahn von Punkten speichert
acc = [0] * 3 * WIDTH * HEIGHT
#Erster Punkt
v = (0.0, 0.0)
acc_max = 1
for p in g(v, N):
x, y = (int((p[0] + 0.5) * WIDTH), HEIGHT - int(p[1] * HEIGHT))
if x >=0 and x < WIDTH and y >= 0 and y < HEIGHT:
acc_index = x + y * HEIGHT
acc[acc_index] += 1
if acc[acc_index] > acc_max:
acc_max = acc[acc_index]
from math import pow
from PIL import Image
im = Image.new("RGB", (WIDTH, HEIGHT), (255, 255, 255))
pix = im.load()
for i in xrange(WIDTH * HEIGHT):
#if acc[i]:
#Im Dunkeln heben
d = int(pow(float(acc[i]) / float(acc_max),1.0/15.0) * 255.0)
pix[i % WIDTH, i / WIDTH] = (0, d, 0)
im.show()
war Spaß!