首頁 > 軟體

利用Matlab製作一個賊簡單的粒子聖誕樹

2022-12-19 14:01:09

上一期聖誕樹有些人說程式碼太複雜,這期來個賊簡單的水晶球裡的聖誕樹:

這期就是非常簡單的三個圖形組合:

聖誕樹主體

% 聖誕樹形狀生成函數
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粒子聖誕樹的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com