python绘制蕨菜叶分形

一花一叶一世界,一草一木一浮生.

使用了四个不同的线性变换,根据概率选择其中一个变换并更新 x 和 y 坐标。然后将生成的绿色点绘制出来,形成一片蕨菜叶。

python 复制代码
import numpy as np
import matplotlib.pyplot as plt

def fern_fractal(num_points):
    # 初始化坐标
    x, y = 0, 0
    points = []

    for _ in range(num_points):
        # 生成随机数以选择变换
        r = np.random.rand()
        
        # 根据概率选择变换
        if r < 0.01:
            # 变换 1
            x_new = 0
            y_new = 0.16 * y
        elif r < 0.86:
            # 变换 2
            x_new = 0.85 * x + 0.04 * y
            y_new = -0.04 * x + 0.85 * y + 1.6
        elif r < 0.93:
            # 变换 3
            x_new = 0.2 * x - 0.26 * y
            y_new = 0.23 * x + 0.22 * y + 1.6
        else:
            # 变换 4
            x_new = -0.15 * x + 0.28 * y
            y_new = 0.26 * x + 0.24 * y + 0.44

        # 更新坐标
        x, y = x_new, y_new
        points.append((x, y))

    return np.array(points)

# 设置点的数量
num_points = 100000
points = fern_fractal(num_points)

# 绘制分形
plt.figure(figsize=(8, 12))
plt.plot(points[:, 0], points[:, 1], 'g.', markersize=0.5)  # 绘制蕨菜叶
plt.axis('off')  # 关闭坐标轴
plt.title('Fern Fractal')
plt.show()
相关推荐
小小测试开发5 小时前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想5 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波6 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
AAA大运重卡何师傅(专跑国道)6 小时前
【无标题】
开发语言·c#
copyer_xyf6 小时前
Python 异常处理
前端·后端·python
XBodhi.7 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
麻雀飞吧7 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy7 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.7 小时前
【01】Python 机器学习
开发语言·python
为爱停留7 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python