【问题标题】:Barnsley Fern Python script巴恩斯利蕨 Python 脚本
【发布时间】:2016-07-29 05:30:16
【问题描述】:

我已经设法用下面的代码生成了一个相当不错的 Barnsley Fern,但有些地方不太对劲。 “茎”有多余的线条,我一直没能找到问题所在。谁能解决这个问题? Barnsley Fern 尽力而为:

from PIL import Image
import random
import matplotlib.pyplot as plt
A=[]
mat=[[0.0,0.0,0.0,0.6,0.0,0.0,0.01],
    [0.85,0.04,-0.04,0.85,0.0,1.6,0.85],
    [0.2,-0.26,0.23,0.22,0.0,1.6,0.07],
    [-0.15,0.28,0.26,0.24,0.0,0.44,0.07]]
x=0.0
y=0.0
for k in range(0,100000):
    p=random.random()
    if p <= mat[0][6]:
        i=0
    elif p <= mat[0][6] + mat[1][6]:
        i=1
    elif p <= mat[0][6] + mat[1][6] + mat[2][6]:
        i=2
    else:
        i=3

    x0 = x * mat[i][0] + y * mat[i][1] + mat[i][4]
    y  = x * mat[i][2] + y * mat[i][3] + mat[i][5]
    x = x0

    ptn=[x,y]

    A.append(ptn)

plt.figure(figsize=(20,30))
plt.scatter( *zip(*A),marker='o', color='g',s=0.1)
plt.show()

【问题讨论】:

  • 我不熟悉这个分形背后的算法,但一个好的调试起点可能是在你的 if 语句中为不同的情况分配不同的颜色。

标签: python matplotlib plot fractals


【解决方案1】:

矩阵中有错字:

mat=[[0.0,0.0,0.0,0.16,0.0,0.0,0.01],
[0.85,0.04,-0.04,0.85,0.0,1.6,0.85],
[0.2,-0.26,0.23,0.22,0.0,1.6,0.07],
[-0.15,0.28,0.26,0.24,0.0,0.44,0.07]]

这解决了它Grrr...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2014-08-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2021-09-11
    • 2022-11-05
    • 2015-01-01
    相关资源
    最近更新 更多