关于使用Python的turtle画树叶的代码。
感兴趣的可以自己了解一下分形树叶的数学几何原理——含概率的迭代函数系统。代码不难,关键是数学原理哦~还有,绘制过程有助于对代码和数学原理理解,大家动手自己试试吧!很有意思。
from numpy import *
from random import random
import turtle
turtle.reset()
x = array([[.5],[.5]])
p = [0.85,0.92,0.99,1.00]
A1 = array([[.85, 0.04],
[-0.04,.85]])
b1 = array([[0],[1.6]])
A2 = array([[0.20,-0.26],
[0.23,0.22]])
b2 = array([[0],[1.6]])
A3 = array([[-0.15,0.28],
[0.26,0.24]])
b3 = array([[0],[0.44]])
A4 = array([[0,0],
[0,0.16]])
#含概率的迭代函数系统
turtle.color("blue")
cnt = 1
while True:
cnt += 1
if cnt == 2000:
break
r = random()
if r < p[0]:
x = dot(A1 , x) + b1
elif r < p[1]:
x = dot(A2 , x) + b2
elif r < p[2]:
x = dot(A3 , x) + b3
else:
x = dot(A4 , x)
#print x[1]
turtle.up()
turtle.goto(x[0][0] * 50,x[1][0] * 40 - 240)
turtle.down()
turtle.dot()
效果图如下: