脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python&Matlab实现樱花的绘制

Python&Matlab实现樱花的绘制

2022-11-24 10:13电力系统与算法之美 Python

正值樱花飘落的季节,本文将利用Python和Matlab分别绘制一颗樱花树,文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编一起动手尝试一下

1.锦短情长

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

Python&Matlab实现樱花的绘制

锦短情长

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

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

2. 一场樱花雨(Matlab)

Python&Matlab实现樱花的绘制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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实现樱花的绘制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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樱花内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_46039719/article/details/124020770

延伸 · 阅读

精彩推荐
  • Python对Python3中的input函数详解

    对Python3中的input函数详解

    下面小编就为大家分享一篇对Python3中的input函数详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Allen-Liu6502021-02-04
  • Python简单的Python人脸识别系统

    简单的Python人脸识别系统

    这篇文章主要介绍了Python人脸识别系统的实现,文中讲解非常详细,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    鹏懿如斯8572020-07-14
  • Python用python对excel进行操作(读,写,修改)

    用python对excel进行操作(读,写,修改)

    这篇文章主要介绍了用python对excel进行操作(读,写,修改),帮助大家更好的利用python处理表格,感兴趣的朋友可以了解下...

    nancy055892021-08-18
  • PythonPython3 max()函数基础用法

    Python3 max()函数基础用法

    在本篇文章中我们给大家讲述了关于Python3 max()函数的基本用法以及相关知识点内容,需要的朋友们学习下。...

    Python教程网8712021-05-30
  • Python利用Python实现数值积分的方法

    利用Python实现数值积分的方法

    这篇文章主要介绍了利用Python实现数值积分。本文主要用于对比使用Python来实现数学中积分的几种计算方式,并和真值进行对比,加深大家对积分运算实现...

    赵卓不凡5572022-09-16
  • Pythonscrapy中如何设置应用cookies的方法(3种)

    scrapy中如何设置应用cookies的方法(3种)

    这篇文章主要介绍了scrapy中如何设置应用cookies的方法(3种),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    不屑哥7822020-09-23
  • PythonPython 的类、继承和多态详解

    Python 的类、继承和多态详解

    本文通过实例给大家详细解释了Python 的类、继承和多态的定义和用法,非常实用,有需要的小伙伴可以参考下...

    adam1q845412020-11-26
  • Pythonpython实现AHP算法的方法实例(层次分析法)

    python实现AHP算法的方法实例(层次分析法)

    这篇文章主要给大家介绍了关于python实现AHP算法(层次分析法)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    今夜月-半弯11382020-09-10