Python&Matlab实现樱花的绘制


Posted in Python onApril 07, 2022

1.锦短情长

为什么选择这个标题,借鉴了一封情书里面的情长纸短,还吻你万千

Python&Matlab实现樱花的绘制

锦短情长

都只谓人走茶凉,怎感觉锦短情长?

一提起眼泪汪汪,是明月人心所向?

2. 一场樱花雨(Matlab)

Python&Matlab实现樱花的绘制

function pingba
hold on,axis equal
axis(0.5+[-10,50,0,50])
set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
set(gca,'color',[0.5020    0.5020    0.5020])
 
length_trunk=6;
width_trunk=4;
k1=0.9;
k2=0.8;
number_branch=15;
alp=pi/10;
length_branch=k1*length_trunk;
width_branch=k2*width_trunk;
trunk=[12,0;12,length_trunk];
plot(trunk(:,1),trunk(:,2),'color',[0 0 0],'Linewidth',width_trunk)
begins=[trunk(2,:),pi/2,1];
grow=begins;
plotdata=[0 0 0 0 0 0 0 0];
plotdata(1,:)=[];
for i=1:number_branch
    control=randi(25,[length(grow(:,1)),1])>=10;
    ag=grow(:,3);
    l=length(ag);
    parta=[length_branch.*k1.^grow(:,4).*cos(ag+ones(l,1)*alp),length_branch.*k1.^grow(:,4).*sin(ag+ones(l,1)*alp),ones(l,1)*alp,ones(l,1)];
    partb=[length_branch.*k1.^grow(:,4).*cos(ag-ones(l,1)*alp),length_branch.*k1.^grow(:,4).*sin(ag-ones(l,1)*alp),-ones(l,1)*alp,ones(l,1)];
    parta2=[0.8.*length_branch.*k1.^grow(:,4).*cos(ag),0.8.*length_branch.*k1.^grow(:,4).*sin(ag),zeros(l,1),ones(l,1)];
    partb2=[0.8.*length_branch.*k1.^grow(:,4).*cos(ag),0.8.*length_branch.*k1.^grow(:,4).*sin(ag),zeros(l,1),ones(l,1)];
    parta=control.*parta+(1-control).*parta2;
    partb=control.*partb+(1-control).*partb2;
    parta=parta+grow;
    partb=partb+grow;
    congress=[parta;partb];
    grow=[grow;grow];
    judge=[grow,congress];
    judge=unique(judge,'rows');
    grow=judge(:,5:end);
    plotdata=[plotdata;judge];
end
for i=1:number_branch
    temp_w=width_branch*0.8^i;
    temp_branch=plotdata(plotdata(:,4)==i,:);
    plx=[temp_branch(:,1),temp_branch(:,5)];
    ply=[temp_branch(:,2),temp_branch(:,6)];
    plx=plx';ply=ply';
    plot(plx,ply,'color',[0 0 0]+i*[0.3020 0.3020 0.3020]./number_branch,'Linewidth',temp_w)
end
 
bloom_pos=plotdata(plotdata(:,8)==number_branch+1,[5,6]);
scatter(bloom_pos(:,1),bloom_pos(:,2),10,'CData',[0.8549    0.6824    0.6824])
bloom_pos=plotdata(plotdata(:,8)==number_branch,[5,6]);
scatter(bloom_pos(:,1),bloom_pos(:,2),8,'CData',[0.7451    0.5961    0.5961].*0.97)
end

3.樱花树(Python)

Python&Matlab实现樱花的绘制

import turtle as T
import random
import time
 
#=======画樱花的躯干(60,t)===============
T.title('凋落的樱花')
def Tree(branch, t):
    time.sleep(0.0005)
    if branch > 3:
        if 8 <= branch <= 12:
            if random.randint(0, 2) == 0:
                t.color('snow')  # 白
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 3)
        elif branch < 8:
            if random.randint(0, 1) == 0:
                t.color('snow')
            else:
                t.color('lightcoral')  # 淡珊瑚色
            t.pensize(branch / 2)
        else:
            t.color('sienna')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        t.forward(branch)
        a = 1.5 * random.random()
        t.right(20 * a)
        b = 1.5 * random.random()
        Tree(branch - 10 * b, t)
        t.left(40 * a)
        Tree(branch - 10 * b, t)
        t.right(20 * a)
        t.up()
        t.backward(branch)
        t.down()
 
#=============掉落的花瓣===================
def Petal(m, t):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')  # 淡珊瑚色
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)
 
#=======绘图区域============
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle()  # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')  # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
 
#=====画樱花的躯干===========
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()

到此这篇关于Python&Matlab实现樱花的绘制的文章就介绍到这了,更多相关Python Matlab樱花内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python语言描述KNN算法与Kd树
Dec 13 Python
python Flask 装饰器顺序问题解决
Aug 08 Python
代码详解django中数据库设置
Jan 28 Python
Python List列表对象内置方法实例详解
Oct 22 Python
pygame编写音乐播放器的实现代码示例
Nov 19 Python
python读取ini配置的类封装代码实例
Jan 08 Python
python opencv 图像边框(填充)添加及图像混合的实现方法(末尾实现类似幻灯片渐变的效果)
Mar 09 Python
Django自定义YamlField实现过程解析
Nov 11 Python
python实现发送QQ邮件(可加附件)
Dec 23 Python
聊聊Python中关于a=[[]]*3的反思
Jun 02 Python
Python torch.flatten()函数案例详解
Aug 30 Python
Python字符串的转义字符
Apr 07 Python
Python OpenCV形态学运算示例详解
4种方法python批量修改替换列表中元素
Apr 07 #Python
Python+OpenCV实现图片中的圆形检测
Python中文分词库jieba(结巴分词)详细使用介绍
基于Python实现对比Exce的工具
Apr 07 #Python
pytorch分类模型绘制混淆矩阵以及可视化详解
Python OpenCV之常用滤波器使用详解
You might like
PHPEXCEL 使用小记
2013/01/06 PHP
基于PHP文件操作的详解
2013/06/05 PHP
PHP laravel中的多对多关系实例详解
2017/06/07 PHP
YII2框架使用控制台命令的方法分析
2020/03/18 PHP
使用Js让Html中特殊字符不被转义
2013/11/05 Javascript
javascript中全局对象的parseInt()方法使用介绍
2013/12/19 Javascript
Select标签下拉列表二级联动级联实例代码
2014/02/07 Javascript
jquery通过load获取文件的内容并跳到锚点的方法
2015/01/29 Javascript
如何让一个json文件显示在表格里【实现代码】
2016/05/09 Javascript
详解Vue底部导航栏组件
2019/05/02 Javascript
七行JSON代码把你的网站变成移动应用过程详解
2019/07/09 Javascript
js+springMVC 提交数组数据到后台的实例
2019/09/21 Javascript
vue 通过 Prop 向子组件传递数据的实现方法
2020/10/30 Javascript
Python中使用item()方法遍历字典的例子
2014/08/26 Python
Python内建数据结构详解
2016/02/03 Python
python在Windows下安装setuptools(easy_install工具)步骤详解
2016/07/01 Python
Python cookbook(数据结构与算法)找到最大或最小的N个元素实现方法示例
2018/02/13 Python
Python实现接受任意个数参数的函数方法
2018/04/21 Python
Python使用matplotlib实现的图像读取、切割裁剪功能示例
2018/04/28 Python
解决django后台样式丢失,css资源加载失败的问题
2019/06/11 Python
安装好Pycharm后如何配置Python解释器简易教程
2019/06/28 Python
Jupyter notebook快速入门教程(推荐)
2020/05/18 Python
Python如何将装饰器定义为类
2020/07/30 Python
Python如何读写字节数据
2020/08/05 Python
python 匿名函数与三元运算学习笔记
2020/10/23 Python
Django中的DateTimeField和DateField实现
2021/02/24 Python
html5小程序飞入购物车(抛物线绘制运动轨迹点)
2020/10/19 HTML / CSS
豪华床上用品 :Jennifer Adams
2019/09/15 全球购物
CK澳大利亚官网:Calvin Klein澳大利亚
2020/12/12 全球购物
银行会计业务的个人自我评价
2013/11/02 职场文书
中专毕业生自荐信范文
2013/11/28 职场文书
维修工先进事迹
2014/05/29 职场文书
大学生自我评价范文
2015/03/03 职场文书
销售内勤岗位职责范本
2015/04/13 职场文书
观看禁毒宣传片后的感想
2015/08/11 职场文书
环保建议书作文500字
2015/09/14 职场文书