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()
相关推荐
花酒锄作田10 小时前
Pydantic校验配置文件
python
hboot10 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi21 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python