hey, 圣诞节又双要到了,提前祝各位MATLABer,各位准研究生们,圣诞快乐,旗开得胜!~
今天带来两款圣诞树哈,都是可以旋转的:
![](https://img-blog.csdnimg.cn/img_convert/5dec53d54962b0ff67e675a731782252.png)
![](https://img-blog.csdnimg.cn/img_convert/e9d97ae7871f07b9f5edd7e8fa19c0c7.png)
原理和我去年发的圣诞树非常像,大家如果感兴趣可以去自行搜一下哈,这里就不浪费大篇幅写原理啦,此外,金色圣诞树的外形参考了以下两部分工作:
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
以下给出完整代码:
金色圣诞树
![](https://img-blog.csdnimg.cn/img_convert/8a57afadd85fca1551b715d7cda66b86.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/6efd57363cdc444dbf1c7d3e6621747a.gif#pic_center)
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) |