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()
相关推荐
IVEN_15 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang16 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮16 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling16 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮19 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽20 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞2 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽2 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers