基于Matlab模拟圣诞树

简介: 基于Matlab模拟圣诞树

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法       神经网络预测       雷达通信      无线传感器        电力系统

信号处理              图像处理               路径规划       元胞自动机        无人机

⛄ 内容介绍

基于Matlab模拟圣诞树

⛄ 代码

%Draws a present with the given coordinate + size in a random color%Note:Given coordinates apply to the lower front + left corner of the%present (the one closest to the viewer) as seen in the plotfunction drawPresent(dx,dy,dz,scalex,scaley,scalez) %the standard present coordinatespresentX=[0.5 0.5 0.5 0.5 0.5; 0 1 1 0 0; 0 1 1 0 0; 0 1 1 0 0; 0.5 0.5 0.5 0.5 0.5];presentY=[0.5 0.5 0.5 0.5 0.5; 0 0 1 1 0; 0 0 1 1 0; 0 0 1 1 0; 0.5 0.5 0.5 0.5 0.5];presentZ=[0 0 0 0 0; 0 0 0 0 0; 0.5 0.5 0.5 0.5 0.5; 1 1 1 1 1; 1 1 1 1 1]; %draw some presents with random colors%scale present and move it to the right place and get the plot handlemyHandle=surf((presentX*scalex+dx),(presentY*scaley+dy), (presentZ*scalez+dz));%some random color maprandColorMap(:,:,1)=repmat(rand,[5,5]);%r componentrandColorMap(:,:,2)=repmat(rand,[5,5]);%g componentrandColorMap(:,:,3)=repmat(rand,[5,5]);%b component%Assign colormap just to the plot handle object of the present, so the tree%does not change colorset(myHandle,'CData',randColorMap)shading interp %Nice shding + without grid end % of function
close all;clear;clc% setupsnow=450;    % number of snow flakes [0 .. 5000]% draw treeh=0:0.2:25; %vertical grid[X,Y,Z] = cylinder(tree(h)); %produce a tree formed cylinderZ=Z*25; %scale to the right heigth%Add some diffusion to the surface of the tree to make it look more realtreeDiffusion=rand(126,21)-0.5;%some horizontal diffusion data%add diffusion to the grid pointsfor cnt1=1:21        for cnt2=16:126%starting above the trunk        %get the angle to always diffuse in direction of the radius        angle=atan(Y(cnt2,cnt1)/X(cnt2,cnt1));        %split the diffusion in the two coordinates, depending on the angle        X(cnt2,cnt1)=X(cnt2,cnt1)+cos(angle)*treeDiffusion(cnt2,cnt1);        Y(cnt2,cnt1)=Y(cnt2,cnt1)+sin(angle)*treeDiffusion(cnt2,cnt1);        %some Vertical diffusion for each point        Z(cnt2,cnt1)=Z(cnt2,cnt1)+(rand-0.5)*0.5;    end    end%draw the treeh0  = figure('Units','inches');pos = h0.Position;pos(1) = 1; pos(2) = 1;pos(3) = 7; pos(4) = 7;h0.Position = pos;surfl(X,Y,Z,'light')%% View and format%Use as nice green color map (darker at the bottom, lighter at the top)r=(0.0430:(0.2061/50):0.2491)';%red componentg=(0.2969:(0.4012/50):0.6981)';%green componentb=(0.0625:(0.2696/50):0.3321)';%blue componentmap=[r,g,b];%join in a mapfor cnt=1:6    %change the lower part to brown for the trunk    map(cnt,:)=[77,63,5]/265;endcolormap(map)%set the mapview([-37.5,4])%Change the view to see a little more of the Actual 3D treelighting phong %some nice lightingshading interp %remove grid and smoothen the surface coloraxis equal %takes care of display in the right proportionaxis([-10 10 -10 10 0 30]) %give some more axis space (for the snow later)axis off %but don't show axishold on %to draw the resttitle('Merry Christmas To 罗雨欣','color','w',...    'fontsize',25,...    'fontweight','Bold')%self explainingset(gcf,'color',[22 32 51]./255)% Presents%Draw some presents around the tree (each with random color)drawPresent(2,-4,0,3,3,2);drawPresent(-4,3,0,2,3,1.5);drawPresent(5,3,0,4,3,3);drawPresent(-14,-5,0,6,3,1);drawPresent(-9,-10,0,2,2,2);drawPresent(0,4,0,4,3,3);drawPresent(-6,-13,0,3,3,3);%% Snow%create some random 3D coordinates for the snow (amount as in setup above)snowX=(rand(snow-100,1)*25-12.5);snowY=(rand(snow-100,1)*25-12.5);snowZ=(rand(snow-100,1)*27);color0 = jet(length(snowX));%Note:Some flakes will end up IN the tree but just can't be seen thenfor ii = 1:length(snowX)    plot3(snowX(ii),snowY(ii),snowZ(ii),'*','color',color0(ii, :),'markersize',randi(15))%plot coordinates as white snow flakes    %     plot3(snowX(ii),snowY(ii),snowZ(ii),'*','color',color0(ii, :))%plot coordinates as white snow flakesendh=plot3(snowX,snowY,snowZ,'w*');im = {};for ii = 1:180    if mod(ii,3) == 0        h.Visible = 'off';        snowX=(rand(snow,1)*25-12.5);        snowY=(rand(snow,1)*25-12.5);        snowZ=(rand(snow,1)*27);        h=plot3(snowX,snowY,snowZ,'w*');        %         pause(0.25)    else        view([ii,4])        %         pause(0.1)    end    if ii > 85        frame = getframe(gcf);        im{ii} = frame2im(frame);    endendhold off%Doneim(cellfun(@isempty,im))=[];% end % of functionfile2write = 'chris.gif';for ii = 1:length(im)    [A, map] = rgb2ind( im{ii}, 256);    if ii == 1        imwrite(A, map, file2write, 'gif','LoopCount',Inf,'DelayTime', 0.12);    else        imwrite(A, map, file2write, 'gif','WriteMode','append','DelayTime', 0.12);    endend
%% ============= private functions function r=tree(h)%Gives a profile for the treefor cnt=1:length(h)         if(h(cnt)==0)%no Width at the bottom. Ensures a "closed" trunk        r(cnt)=0;    end    %smaller radius for the trunk    if (h(cnt)>0 && h(cnt)<=3)        r(cnt)=1.5;    end         %reduce radius gradually from 8 to 0. Note: will only work with a trunk heigth    %of 3 and a whole tree heigth of 25. Scale the height of the tree in    %the "draw tree" section, since the cylinder command will return a 1    %unit high cylinder anyway    if(h(cnt)>3)        r(cnt)=8-(h(cnt)-3)*0.3636;    end     end end % of function

⛄ 运行结果

⛳️ 代码获取关注我

❤️部分理论引用网络文献,若有侵权联系博主删除
❤️ 关注我领取海量matlab电子书和数学建模资料


相关文章
基于混沌集成决策树的电能质量复合扰动识别(matlab代码)
基于混沌集成决策树的电能质量复合扰动识别(matlab代码)
|
4月前
|
机器学习/深度学习 算法 数据可视化
Matlab决策树、模糊C-均值聚类算法分析高校教师职称学历评分可视化
Matlab决策树、模糊C-均值聚类算法分析高校教师职称学历评分可视化
|
4月前
|
机器学习/深度学习 数据可视化
Matlab决策树对空气质量和天气温度及天气数据做交通出行推荐预测|数据分享
Matlab决策树对空气质量和天气温度及天气数据做交通出行推荐预测|数据分享
|
机器学习/深度学习 数据采集 自然语言处理
基于机器学习的情绪识别算法matlab仿真,对比SVM,LDA以及决策树
基于机器学习的情绪识别算法matlab仿真,对比SVM,LDA以及决策树
|
4月前
|
机器学习/深度学习 存储
matlab使用分位数随机森林(QRF)回归树检测异常值
matlab使用分位数随机森林(QRF)回归树检测异常值
|
机器学习/深度学习 算法
基于机器学习之模型树短期负荷预测(Matlab代码实现)
基于机器学习之模型树短期负荷预测(Matlab代码实现)
103 1
|
编解码 算法 新能源
基于混沌集成决策树的电能质量复合扰动识别(Matlab代码实现)
基于混沌集成决策树的电能质量复合扰动识别(Matlab代码实现)
|
机器学习/深度学习 算法 数据库
基于mnist手写数字数据库识别算法matlab仿真,对比SVM,LDA以及决策树
基于mnist手写数字数据库识别算法matlab仿真,对比SVM,LDA以及决策树
|
机器学习/深度学习 算法
【MATLAB第42期】基于MATLAB的贝叶斯优化决策树分类算法与网格搜索、随机搜索对比,含对机器学习模型的评估度量介绍
【MATLAB第42期】基于MATLAB的贝叶斯优化决策树分类算法与网格搜索、随机搜索对比,含对机器学习模型的评估度量介绍
|
算法
语音信号的哈夫曼编码压缩解压缩算法matlab仿真,输出编码后数据大小,编码树等指标
语音信号的哈夫曼编码压缩解压缩算法matlab仿真,输出编码后数据大小,编码树等指标
197 0

热门文章

最新文章