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()
相关推荐
devilnumber1 小时前
Java 30 组高频技术 / 知识点多角度对比
java·开发语言
何以解忧,唯有..1 小时前
Python 异常处理完全指南:从基础到高级实践
开发语言·前端·python
DLYSB_1 小时前
让 PLC“开口说话”:基于 Modbus TCP 的声光语音告警设计与 Python 调试
python·网络协议·tcp/ip·报警灯
今日热点1 小时前
微信小程序主体变更公证全流程实操解析:场景条件、材料规范、驳回避坑与Python校验脚本实现
python·微信小程序·小程序
O。O蛋黄酥啊1 小时前
GraphRAG 和 LightRAG 详解与对比
人工智能·python·算法·rag·graphrag·lightrag
Buke..1 小时前
【小程序逆向】某游快爆 AI 逆向 sign参数:AI 辅助分析与 MCP 工具链实战
前端·人工智能·爬虫·python·小程序·notepad++
gugucoding1 小时前
57. 【Java】日志框架:SLF4J与Logback
java·开发语言·logback
此生决int2 小时前
深入理解C++系列(15)——AVL树
开发语言·c++
一只旭宝2 小时前
预约系统版本2(pyhton+flask可视化版本)
服务器·数据库·c++·笔记·python·flask
筱璦2 小时前
PHP+ VUE3机构级股票分仓管理交易系统源码可出
开发语言·php·vue3·量化·股票分仓