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()
相关推荐
Data_Journal41 分钟前
如何使用 Python 抓取 Google Flights:分步指南
大数据·开发语言·数据库·python·scrapy
你我一见如故1 小时前
仿QQ音乐桌面客户端——测试报告
python·selenium
一直C1 小时前
【数据结构】哈希表+算法复杂度与经典排序查找(C语言)
java·linux·开发语言·数据结构·算法·ubuntu·散列表
Kismet_nvi1 小时前
Python 基础核心知识点总结
开发语言·windows·python
2401_844582951 小时前
冷门技巧:AI模型可靠性测试的实用技巧:数
python
爱学习的小邓同学1 小时前
Golang语言入门
开发语言·后端·golang
you鬰1 小时前
datawhale--llm-algo-leetcode5️⃣
python·datawhale
λqaq72 小时前
MySQL 数据库基础(2)约束、用户权限、远程连接、外键与 TCP/UDP 理解
linux·数据库·python·tcp/ip·mysql
人工干智能2 小时前
科普:pandas 时间偏移别名:ts.resample(“5Min“).sum()
python·pandas
开源量化GO2 小时前
最新AI辅助量化表达:先理清规则,再按需求选工具
人工智能·python