MATLAB | 圣诞节快到了,一起画两款动态圣诞树吧~

发布时间:2023年12月20日

hey, 圣诞节又双要到了,提前祝各位MATLABer,各位准研究生们,圣诞快乐,旗开得胜!~

今天带来两款圣诞树哈,都是可以旋转的:

原理和我去年发的圣诞树非常像,大家如果感兴趣可以去自行搜一下哈,这里就不浪费大篇幅写原理啦,此外,金色圣诞树的外形参考了以下两部分工作:

  • Hanchu Wang (2023). Rotating Christmas Tree(https://www.mathworks.com/matlabcentral/fileexchange/103980-rotating-christmas-tree)
  • Weisstein, Eric W. “Conical Spiral.” From MathWorld–A Wolfram Web Resource. https://mathworld.wolfram.com/ConicalSpiral.html

以下给出完整代码:


金色圣诞树

在这里插入图片描述

function XmasTree2023_1
fig = figure('Units','normalized','Position',[.1,.1,.5,.8],'Color',[0,9,33]/255);
ax  = axes('Parent',fig,'Position',[-1/4,-1/3,1+1/2,1+2/3],'UserData',[18,11,0,13,3,0,17,4,17]+97,...
      'XLim',[-7,7],'YLim',[-7,7],'ZLim',[-6,18],'Color',[0,9,33]/255); 
hold on; view(-40,15); disp(char(ax.UserData)); % rng(5)
ax.DataAspectRatio=[1,1,1.2];

XMT = @(h,r,a,z) deal((h-z)./h.*r.*cos(a.*z), (h-z)./h.*r.*sin(a.*z), z);
OFS = @(x,y,z,p) deal(x+rand(size(x)).*p-p/2, y+rand(size(y)).*p-p/2, z+rand(size(z)).*p-p/2);
%% ========================================================================
% 绘制圣诞树主体,参考:Hanchu Wang (2023). Rotating Christmas Tree 
% (https://www.mathworks.com/matlabcentral/fileexchange/103980-rotating-christmas-tree)
h = 10.05;
r = h/(1+sqrt(5));
[X,Y,Z] = XMT(h,r,100*pi,rand(3000,1).*h);
[X,Y,Z] = OFS(X,Y,Z,.1);
scatter3(X,Y,Z,12,[213,194,100]./255,'filled','MarkerFaceAlpha',.9,'MarkerEdgeAlpha',.9)

[X,Y,Z] = XMT(h,r,100*pi,rand(500,1).*h);
[X,Y,Z] = OFS(X,Y,Z,1.5);
scatter3(X,Y,Z,8,[255,255,255]./255,'filled','MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2)

[X,Y,Z] = XMT(h,r,20*pi,linspace(0,1,1000)*h);
scatter3(X,Y,Z,90,[213,194,100]./255,'filled','MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2)
scatter3(X,Y,Z,12,[255,255,255]./255,'filled','MarkerFaceAlpha',.9,'MarkerEdgeAlpha',.9)

%% 绘制星星 ================================================================
plot3(0,0,10.5,'p', 'MarkerSize',25,'MarkerFaceColor',[255,223,153]/255,'MarkerEdgeColor','none','LineWidth', 1);
starLightHdl = scatter3(0,0,10.5,8000,'o','MarkerFaceColor','w','MarkerEdgeColor','w','MarkerEdgeAlpha',0,'MarkerFaceAlpha', 0.1);

%% 绘制地面 ================================================================
a = 100;
b = (.5:a)'/a;
c = (-cos(b*2*pi)+1).^.2;
f = b-.5;
r = f'.^2+f.^2;
h = abs(ifftn(exp(5i*rand(a))./r.^1.05)).*(c*c')./2-2;
[X,Y] = meshgrid(linspace(-10,10,a)); [X,Y,h] = OFS(X,Y,h,.1);
scatter3(X(:),Y(:),h(:),8,[91,131,253]./255,'filled','MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2)
% surf(X,Y,h,'EdgeColor',[91,131,253]./255,'AlphaData',h,'FaceColor',[91,131,253]./255,'FaceAlpha','flat')
h = abs(ifftn(exp(5i*rand(a))./r.^1.05)).*(c*c')./1.5-2.5;
[X,Y] = meshgrid(linspace(-10,10,a)); [X,Y,h] = OFS(X,Y,h,.2);
scatter3(X(:),Y(:),h(:),8,[91,131,253]./255,'filled','MarkerFaceAlpha',.1,'MarkerEdgeAlpha',.1)
%% 绘制雪花 ================================================================
snowXYZ1=rand(90,3);
snowXYZ1(:,1:2)=snowXYZ1(:,1:2).*16-8;
snowXYZ1(:,3)=snowXYZ1(:,3).*20;
snowXYZ2=rand(120,3);
snowXYZ2(:,1:2)=snowXYZ2(:,1:2).*16-8;
snowXYZ2(:,3)=snowXYZ2(:,3).*20;
snowHdl1=plot3(snowXYZ1(:,1),snowXYZ1(:,2),snowXYZ1(:,3),'*','Color',[.8,.8,.8]);
snowHdl2=plot3(snowXYZ2(:,1),snowXYZ2(:,2),snowXYZ2(:,3),'.','Color',[.6,.6,.6]);
%% 旋转图像、雪花飘落 =======================================================
for i=1:1e8
    starLightHdl.SizeData=8000+sin(i/5).*1600;
    snowXYZ1(:,3)=snowXYZ1(:,3)-.05;snowXYZ2(:,3)=snowXYZ2(:,3)-.005;
    snowXYZ1(snowXYZ1(:,3)<0,3)=snowXYZ1(snowXYZ1(:,3)<0,3)+20;
    snowXYZ2(snowXYZ2(:,3)<0,3)=snowXYZ2(snowXYZ2(:,3)<0,3)+20;
    snowHdl1.XData=snowXYZ1(:,1);snowHdl1.YData=snowXYZ1(:,2);snowHdl1.ZData=snowXYZ1(:,3);
    snowHdl2.XData=snowXYZ2(:,1);snowHdl2.YData=snowXYZ2(:,2);snowHdl2.ZData=snowXYZ2(:,3);
    view([i,15]);
    drawnow;pause(.05)
end
end  

线条圣诞树

在这里插入图片描述

function XmasTree2023_2
fig = figure('Units','normalized','Position',[.1,.1,.5,.8],'Color',[0,9,33]/255);
ax  = axes('Parent',fig,'Position',[0,-1/6,1,1+1/3],'UserData',[18,11,0,13,3,0,17,4,17]+97,...
      'XLim',[-5,5],'YLim',[-5,5],'ZLim',[-3,14],'Color',[0,9,33]/255,...
      'XColor','none','YColor','none','ZColor','none'); 
hold on; view(-47.5,33); disp(char(ax.UserData));
ax.DataAspectRatio=[1,1,1.2];
XMT = @(h,r,a,z) deal((h-z)./h.*r.*cos(a.*z), (h-z)./h.*r.*sin(a.*z), z);
OFS = @(x,y,z,p) deal(x+rand(size(x)).*p-p/2, y+rand(size(y)).*p-p/2, z+rand(size(z)).*p-p/2);


% 绘制圣诞树主体 ===========================================================
h = 10.05;
r = h/(1+sqrt(5));
[X,Y,Z] = XMT(h,r,100*pi,linspace(0,1,200)*h);
plot3(X,Y,Z,'Color',[76,103,86,250]./255,'LineWidth',1)

[X,Y,Z] = XMT(h,r,90*pi,linspace(0,1,50)*h);
[X,Y,Z] = OFS(X,Y,Z,.1);
scatter3(X,Y,Z,20,[213,194,100]./255,'filled','MarkerFaceAlpha',.9,'MarkerEdgeAlpha',.9)
scatter3(X,Y,Z,150,[213,194,100]./255,'filled','MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2)


[X,Y,Z] = XMT(h,r,150*pi,linspace(0,1,50)*h);
[X,Y,Z] = OFS(X,Y,Z,.1);
scatter3(X,Y,Z,20,[114,146,184]./255,'filled','MarkerFaceAlpha',.9,'MarkerEdgeAlpha',.9)
scatter3(X,Y,Z,150,[114,146,184]./255,'filled','MarkerFaceAlpha',.2,'MarkerEdgeAlpha',.2)


%% 绘制星星 ================================================================
plot3(0,0,10.5,'p', 'MarkerSize',25,'MarkerFaceColor',[255,223,153]/255,'MarkerEdgeColor','none','LineWidth', 1);
starLightHdl = scatter3(0,0,10.5,8000,'o','MarkerFaceColor','w','MarkerEdgeColor','w','MarkerEdgeAlpha',0,'MarkerFaceAlpha', 0.1);

%% 绘制地面 ================================================================
[X,Y]=meshgrid(linspace(-6,6,60));
Z=cos(hypot(X,Y).*5)./2-2; [X,Y,Z] = OFS(X,Y,Z,.1);
scatter3(X,Y,Z,3,'o','filled','CData',[91,131,253]./255,...
    'MarkerFaceAlpha',.4,'MarkerEdgeColor','none');
%% 绘制雪花 ================================================================
snowXYZ1=rand(120,3);
snowXYZ1(:,1:2)=snowXYZ1(:,1:2).*16-8;
snowXYZ1(:,3)=snowXYZ1(:,3).*20;
snowXYZ2=rand(150,3);
snowXYZ2(:,1:2)=snowXYZ2(:,1:2).*16-8;
snowXYZ2(:,3)=snowXYZ2(:,3).*20;
snowHdl1=plot3(snowXYZ1(:,1),snowXYZ1(:,2),snowXYZ1(:,3),'*','Color',[.8,.8,.8]);
snowHdl2=plot3(snowXYZ2(:,1),snowXYZ2(:,2),snowXYZ2(:,3),'.','Color',[.6,.6,.6]);
%% 旋转图像、雪花飘落 =======================================================
for i=1:1e8
    starLightHdl.SizeData=8000+sin(i/5).*1600;
    snowXYZ1(:,3)=snowXYZ1(:,3)-.05;snowXYZ2(:,3)=snowXYZ2(:,3)-.005;
    snowXYZ1(snowXYZ1(:,3)<0,3)=snowXYZ1(snowXYZ1(:,3)<0,3)+20;
    snowXYZ2(snowXYZ2(:,3)<0,3)=snowXYZ2(snowXYZ2(:,3)<0,3)+20;
    snowHdl1.XData=snowXYZ1(:,1);snowHdl1.YData=snowXYZ1(:,2);snowHdl1.ZData=snowXYZ1(:,3);
    snowHdl2.XData=snowXYZ2(:,1);snowHdl2.YData=snowXYZ2(:,2);snowHdl2.ZData=snowXYZ2(:,3);
    view([i,33]);
    drawnow;pause(.05)
end
end  

祝各位准研究生们春风得意马蹄疾,早考完早一起快乐MATLAB~祝各位MATLABer圣诞快乐!

文章来源:https://blog.csdn.net/slandarer/article/details/135106166
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。