MATLAB | 一起来绘制有雪花飘落的动态圣诞树叭~~

文摘   2024-12-14 00:01   英国  
请尊重原创劳动成果
转载请注明本文链接
及文章作者:slandarer

hey, 大家又双叒叕又双叒叕,好久不见,研究生考试和圣诞节都快到了,发点放松的东西,这期又是两棵圣诞树,大概长这样:

这期圣诞树专门做了改名字的地方,代码中有注释在哪可以改圣诞树下面的文本,闲话少叙,一起来看看叭~

水晶圣诞树

function XmasTree2024_1
fig = figure('Units','normalized''Position',[.1,.1,.5,.8],...
    'Color',[0,9,33]/255'UserData',40 + [60,65,75,72,0,59,64,57,74,0,63,59,57,0,1,6,45,75,61,74,28,57,76,57,1,1]);
axes('Parent',fig, 'Position',[0,-1/6,1,1+1/3], 'UserData',97 + [18,11,0,13,3,0,17,4,17],...
      'XLim',[-1.5,1.5], 'YLim',[-1.5,1.5], 'ZLim',[-.2,3.8], 'DataAspectRatio', [1,1,1], 'NextPlot','add',...
      'Projection','perspective''Color',[0,9,33]/255'XColor','none''YColor','none''ZColor','none')

F = [1,3,4;1,4,5;1,5,6;1,6,3;...
    2,3,4;2,4,5;2,5,6;2,6,3];
dP = @(V) patch('Faces',F, 'Vertices',V, 'FaceColor',[0 71 177]./255,...
    'FaceAlpha',rand(1).*0.2+0.1'EdgeColor',[0 71 177]./255.*0.8,...
    'EdgeAlpha',0.6'LineWidth',0.5'EdgeLighting','gouraud''SpecularStrength',0.3);
r = .1; h = .8
V0 = [0,0,00,0,10,r,h; r,0,h; 0,-r,h; -r,0,h];

Rx = @(V, theta) V*[1 0 00 cos(theta) sin(theta); 0 -sin(theta) cos(theta)];
Rz = @(V, theta) V*[cos(theta) sin(theta) 0;-sin(theta) cos(theta) 00 0 1];
N = 180; Vn = zeros(N, 3); eval(char(fig.UserData))
for i = 1:N
    tV = Rz(Rx(V0.*(1.2 - .8.*i./N + rand(1).*.1./i^(1/5)), pi/3.*(1 - .6.*i./N)), i.*pi/8.1 + .001.*i.^2) + [0,0,.016.*i];
    dP(tV); Vn(i,:) = tV(2,:);
end
% scatter3(Vn(:,1),Vn(:,2),Vn(:,3), 100, 'filled', 'w', 'Marker','o', 'MarkerFaceAlpha',.05, 'MarkerEdgeColor','none')
scatter3(Vn(:,1).*1.02,Vn(:,2).*1.02,Vn(:,3).*1.0130'w''Marker','*''MarkerEdgeAlpha',.5)

w = .3; R = .62; r = .4; T = (1/8:1/8:(2 - 1/8)).'.*pi;
V8 = [ 00, w;  00,-w;
       100;  010-100;  0,-1,0;
       R, R, 0; -R, R, 0; -R,-R, 0;  R,-R,0;
       cos(T).*r, sin(T).*r, T.*0];
F8 = [1,3,251,3,112,3,252,3,111,7,111,7,132,7,112,7,13;
      1,4,131,4,152,4,132,4,151,8,151,8,172,8,152,8,17;
      1,5,171,5,192,5,172,5,191,9,191,9,212,9,192,9,21;
      1,6,211,6,232,6,212,6,231,10,231,10,252,10,232,10,25];
V8 = Rx(V8.*.3pi/2) + [0,0,3.5];
patch('Faces',F8, 'Vertices',V8, 'FaceColor',[255,223,153]./255,...
      'EdgeColor',[255,223,153]./255'FaceAlpha'.2)
sXYZ = rand(200,3).*[4,4,5] - [2,2,0];
sHdl1 = plot3(sXYZ(1:90,1),sXYZ(1:90,2),sXYZ(1:90,3), '*''Color',[.8,.8,.8]);
sHdl2 = plot3(sXYZ(91:200,1),sXYZ(91:200,2),sXYZ(91:200,3), '.''Color',[.6,.6,.6]);
%                                                                        在这改名字 ↓
annotation(fig,'textbox',[0,.05,1,.09], 'Color',[1 1 1], 'String','Merry Christmas XXX',...
    'HorizontalAlignment','center''FontWeight','bold''FontSize',48,...
    'FontName','华文宋体''FontAngle','italic''FitBoxToText','off','EdgeColor','none');
for i=1:1e8
    sXYZ(:,3) = sXYZ(:,3) - [.05.*ones(90,1); .06.*ones(110,1)];
    sXYZ(sXYZ(:,3)<03) = sXYZ(sXYZ(:,3) < 03) + 5;
    sHdl1.ZData = sXYZ(1:90,3); sHdl2.ZData = sXYZ(91:200,3);
    view([i,30]); drawnow; pause(.05)
end
end

曲面圣诞树

function XmasTree2024_2
fig = figure('Units','normalized''Position',[.1,.1,.5,.8],...
    'Color',[0,9,33]/255'UserData',40 + [60,65,75,72,0,59,64,57,74,0,63,59,57,0,1,6,45,75,61,74,28,57,76,57,1,1]);
axes('Parent',fig, 'Position',[0,-1/6,1,1+1/3], 'UserData',97 + [18,11,0,13,3,0,17,4,17],...
      'XLim',[-6,6], 'YLim',[-6,6], 'ZLim',[-161], 'DataAspectRatio', [1,1,1], 'NextPlot','add',...
      'Projection','perspective''Color',[0,9,33]/255'XColor','none''YColor','none''ZColor','none')

[X,T] = meshgrid(.4:.1:10:pi/50:2*pi);
XM = 1 + sin(8.*T).*.05;
X = X.*XM; R = X.^(3).*(.5 + sin(8.*T).*.02);
dF = @(R, T, X) surf(R.*cos(T), R.*sin(T), -X, 'EdgeColor',[20,107,58]./255,...
    'FaceColor', [20,107,58]./255'FaceAlpha',.2'LineWidth',1);
CList = [254,103,110255,191,11557,120,164]./255;
for i = 1:5
    tR = R.*(2 + i); tT = T+i; tX = X.*(2 + i) + i;
    SFHdl = dF(tR, tT, tX);
    [~, ind] = sort(SFHdl.ZData(:)); ind = ind(1:8);
    C = CList(randi([1,size(CList,1)], [8,1]), :);
    scatter3(tR(ind).*cos(tT(ind)), tR(ind).*sin(tT(ind)), -tX(ind), 120'filled',...
        'CData', C, 'MarkerEdgeColor','none''MarkerFaceAlpha',.3)
    scatter3(tR(ind).*cos(tT(ind)), tR(ind).*sin(tT(ind)), -tX(ind), 60'filled''CData', C)
end
Rx = @(V, theta) V*[1 0 00 cos(theta) sin(theta); 0 -sin(theta) cos(theta)];
% Rz = @(V, theta) V*[cos(theta) sin(theta) 0;-sin(theta) cos(theta) 0; 0 0 1];
w = .3; R = .62; r = .4; T = (1/8:1/8:(2 - 1/8)).'.*pi;
V8 = [ 00, w;  00,-w;
       100;  010-100;  0,-1,0;
       R, R, 0; -R, R, 0; -R,-R, 0;  R,-R,0;
       cos(T).*r, sin(T).*r, T.*0];
F8 = [1,3,251,3,112,3,252,3,111,7,111,7,132,7,112,7,13;
      1,4,131,4,152,4,132,4,151,8,151,8,172,8,152,8,17;
      1,5,171,5,192,5,172,5,191,9,191,9,212,9,192,9,21;
      1,6,211,6,232,6,212,6,231,10,231,10,252,10,232,10,25];
V8 = Rx(V8.*.8pi/2) + [0,0,-1.3];
patch('Faces',F8, 'Vertices',V8, 'FaceColor',[255,223,153]./255,...
      'EdgeColor',[255,223,153]./255'FaceAlpha'.2)
view(3,30)
%                                                                        在这改名字 ↓
annotation(fig,'textbox',[0,.05,1,.09], 'Color',[1 1 1], 'String','Merry Christmas XXX',...
    'HorizontalAlignment','center''FontWeight','bold''FontSize',48,...
    'FontName','华文宋体''FontAngle','italic''FitBoxToText','off','EdgeColor','none');

sXYZ = rand(200,3).*[12,12,17] - [6,6,16];
sHdl1 = plot3(sXYZ(1:90,1),sXYZ(1:90,2),sXYZ(1:90,3), '*''Color',[.8,.8,.8]);
sHdl2 = plot3(sXYZ(91:200,1),sXYZ(91:200,2),sXYZ(91:200,3), '.''Color',[.6,.6,.6]);
for i=1:1e8
    sXYZ(:,3) = sXYZ(:,3) - [.1.*ones(90,1); .12.*ones(110,1)];
    sXYZ(sXYZ(:,3)<-163) = sXYZ(sXYZ(:,3) < -163) + 17.5;
    sHdl1.ZData = sXYZ(1:90,3); sHdl2.ZData = sXYZ(91:200,3);
    view([i,30]); drawnow; pause(.05)
end
end

祝各位得偿所愿

祝各位学业工作顺利,祝各位准研究生们一举成名天下知,大家一起快乐MATLAB哇!!

以上所有代码及往年圣诞树都已经传到以下Gitee平台,之后应该也会在评论区给出网盘链接,Gitee链接如下:

  • https://gitee.com/slandarer/matlab---XMAS


slandarer随笔
slandarer个人公众号,目前主要更新MATLAB相关内容。
 最新文章