1. 程式人生 > >用python-turtle畫分形樹葉圖

用python-turtle畫分形樹葉圖

關於用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()

完成以後的圖大概是這個樣子:

這裡寫圖片描述