利用Matlab制作一个贼简单的粒子圣诞树
短信预约 -IT技能 免费直播动态提醒
上一期圣诞树有些人说代码太复杂,这期来个贼简单的水晶球里的圣诞树:
这期就是非常简单的三个图形组合:
圣诞树主体
% 圣诞树形状生成函数
h0=25;r0=12;a0=.5.*pi;
XFunc=@(h,r,a,z) (h-z)./h.*r.*cos(a.*z);
YFunc=@(h,r,a,z) (h-z)./h.*r.*sin(a.*z);
ZFunc=@(h,r,z) z+sqrt(12^2-((h-z)./h.*r).^2);
% 生成最外圈散点
Z0=linspace(0,12.5,150);
X0=XFunc(h0,r0,a0,Z0);
Y0=YFunc(h0,r0,a0,Z0);
% 生成中心点
ZC=ZFunc(h0,r0,Z0);
t0=linspace(.05,1,25)';
% 生成树枝上的散点
treeXYZ=zeros([length(Z0)*length(t0),3]);
for i=1:length(Z0)
treeXYZ((i-1)*length(t0)+1:i*length(t0),:)=...
[0,0,ZC(i)]+[X0(i),Y0(i),Z0(i)-ZC(i)].*t0;
end
% 绘制圣诞树
treeXYZ=treeXYZ+rand(size(treeXYZ))./3;
treeCData=repmat([0,235,81]./255,[length(Z0)*length(t0),1]);
treeCData=treeCData+rand(size(treeXYZ))./2;
treeCData(treeCData>1)=1;
scatter3(treeXYZ(:,1),treeXYZ(:,2),treeXYZ(:,3)-3,3,'o','filled','CData',treeCData,...
'MarkerFaceAlpha',.7,'MarkerEdgeColor','none')
主体就是锥形螺旋线和中轴之间连线上取的一些散点:
背景修饰
ax=gca;
set(gcf,'Color',[22,32,51]./255);
% 坐标区域修饰
set(ax,'XLim',[-27 27],'YLim',[-27,27],'ZLim',[-9,28],'PlotBoxAspectRatio',[1,1,.8],...
'XColor','none','YColor','none','ZColor','none','Color',[22,32,51]./255,'View',[-37.5,19.5],...
'Position',[-.5,-.2,2,1.4],'Tag',char([100,105,115,112,40,39,20316,32773,...
58,115,108,97,110,100,97,114,101,114,39,41]));eval(ax.Tag)
绘制地面
这里将距离中心点的距离映射为余弦函数:
% 绘制地面
[surfX,surfY]=meshgrid(linspace(-25,25,60));
surfZ=cos(hypot(surfX,surfY))-5;
surfZ=surfZ+rand(size(surfX)).*1.2;
surfX=surfX+rand(size(surfX))./1.8;
surfY=surfY+rand(size(surfX))./1.8;
scatter3(surfX,surfY,surfZ,3,'o','filled','CData',[91,131,253]./255,...
'MarkerFaceAlpha',.4,'MarkerEdgeColor','none')
水晶球
就是一个半透明的球:
% 绘制水晶球
[ballX,ballY,ballZ]=sphere(20);
surf(ballX.*18,ballY.*18,ballZ.*15+8,'EdgeColor','none','FaceAlpha',.1);
colormap(gray);
light;lighting phong;shading interp;
添加雪花并旋转视角
% 绘制雪花
snowXYZ1=rand(27,3);
snowXYZ1(:,1:2)=snowXYZ1(:,1:2).*50-25;
snowXYZ1(:,3)=snowXYZ1(:,3).*30;
snowXYZ2=rand(60,3);
snowXYZ2(:,1:2)=snowXYZ2(:,1:2).*50-25;
snowXYZ2(:,3)=snowXYZ2(:,3).*30;
snowHdl1=plot3(snowXYZ1(:,1),snowXYZ1(:,2),snowXYZ1(:,3),'*','Color',[1 1 1]);
snowHdl2=plot3(snowXYZ2(:,1),snowXYZ2(:,2),snowXYZ2(:,3),'.','Color',[.6,.6,.6]);
% 旋转图像、雪花飘落
for i=1:1e8
snowXYZ1(:,3)=snowXYZ1(:,3)-.1;snowXYZ2(:,3)=snowXYZ2(:,3)-.01;
snowXYZ1(snowXYZ1(:,3)<-5,3)=30;snowXYZ2(snowXYZ2(:,3)<-5,3)=30;
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,19.5]);
drawnow;pause(.05)
end
完整代码
function XmasTree2022_2
% @author:slandarer
% 圣诞树形状生成函数
h0=25;r0=12;a0=.5.*pi;
XFunc=@(h,r,a,z) (h-z)./h.*r.*cos(a.*z);
YFunc=@(h,r,a,z) (h-z)./h.*r.*sin(a.*z);
ZFunc=@(h,r,z) z+sqrt(12^2-((h-z)./h.*r).^2);
% 生成最外圈散点
Z0=linspace(0,12.5,150);
X0=XFunc(h0,r0,a0,Z0);
Y0=YFunc(h0,r0,a0,Z0);
% 生成中心点
ZC=ZFunc(h0,r0,Z0);
t0=linspace(.05,1,25)';
% 生成树枝上的散点
treeXYZ=zeros([length(Z0)*length(t0),3]);
for i=1:length(Z0)
treeXYZ((i-1)*length(t0)+1:i*length(t0),:)=...
[0,0,ZC(i)]+[X0(i),Y0(i),Z0(i)-ZC(i)].*t0;
end
ax=gca;hold on;
set(gcf,'Color',[22,32,51]./255);
% 绘制圣诞树
treeXYZ=treeXYZ+rand(size(treeXYZ))./3;
treeCData=repmat([0,235,81]./255,[length(Z0)*length(t0),1]);
treeCData=treeCData+rand(size(treeXYZ))./2;
treeCData(treeCData>1)=1;
scatter3(treeXYZ(:,1),treeXYZ(:,2),treeXYZ(:,3)-3,3,'o','filled','CData',treeCData,...
'MarkerFaceAlpha',.7,'MarkerEdgeColor','none')
% 绘制水晶球
[ballX,ballY,ballZ]=sphere(20);
surf(ballX.*18,ballY.*18,ballZ.*15+8,'EdgeColor','none','FaceAlpha',.1);
colormap(gray);
light;lighting phong;shading interp;
% 绘制地面
[surfX,surfY]=meshgrid(linspace(-25,25,60));
surfZ=cos(hypot(surfX,surfY))-5;
surfZ=surfZ+rand(size(surfX)).*1.2;
surfX=surfX+rand(size(surfX))./1.8;
surfY=surfY+rand(size(surfX))./1.8;
scatter3(surfX,surfY,surfZ,3,'o','filled','CData',[91,131,253]./255,...
'MarkerFaceAlpha',.4,'MarkerEdgeColor','none')
% 坐标区域修饰
set(ax,'XLim',[-27 27],'YLim',[-27,27],'ZLim',[-9,28],'PlotBoxAspectRatio',[1,1,.8],...
'XColor','none','YColor','none','ZColor','none','Color',[22,32,51]./255,'View',[-37.5,19.5],...
'Position',[-.5,-.2,2,1.4],'Tag',char([100,105,115,112,40,39,20316,32773,...
58,115,108,97,110,100,97,114,101,114,39,41]));eval(ax.Tag)
% 绘制雪花
snowXYZ1=rand(27,3);
snowXYZ1(:,1:2)=snowXYZ1(:,1:2).*50-25;
snowXYZ1(:,3)=snowXYZ1(:,3).*30;
snowXYZ2=rand(60,3);
snowXYZ2(:,1:2)=snowXYZ2(:,1:2).*50-25;
snowXYZ2(:,3)=snowXYZ2(:,3).*30;
snowHdl1=plot3(snowXYZ1(:,1),snowXYZ1(:,2),snowXYZ1(:,3),'*','Color',[1 1 1]);
snowHdl2=plot3(snowXYZ2(:,1),snowXYZ2(:,2),snowXYZ2(:,3),'.','Color',[.6,.6,.6]);
% 旋转图像、雪花飘落
for i=1:1e8
snowXYZ1(:,3)=snowXYZ1(:,3)-.1;snowXYZ2(:,3)=snowXYZ2(:,3)-.01;
snowXYZ1(snowXYZ1(:,3)<-5,3)=30;snowXYZ2(snowXYZ2(:,3)<-5,3)=30;
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,19.5]);
drawnow;pause(.05)
end
end
以上就是利用Matlab制作一个贼简单的粒子圣诞树的详细内容,更多关于Matlab粒子圣诞树的资料请关注编程网其它相关文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341