0.前言
圣誕節(jié)了棚瘟,今天推一篇用MATLAB畫(huà)圣誕樹(shù)的现斋,效果如下圖所示:
1.準(zhǔn)備工作
因?yàn)檫@次用的是MATLAB,不像PYTHON一樣需要裝一些依賴(lài)庫(kù)偎蘸,要實(shí)現(xiàn)本文的效果庄蹋,只需安裝MATLAB即可。
2.動(dòng)起手來(lái)
2.1 畫(huà)樹(shù)
要畫(huà)圣誕樹(shù)迷雪,首先...畫(huà)棵樹(shù)蔓肯,然后再想辦法來(lái)點(diǎn)綴它。
close all;clear;clc
% setup
snow=450; % number of snow flakes [0 .. 5000]
% draw tree
h=0:0.2:25; %vertical grid
[X,Y,Z] = cylinder(tree(h)); %produce a tree formed cylinder
Z=Z*25; %scale to the right heigth
%Add some diffusion to the surface of the tree to make it look more real
treeDiffusion=rand(126,21)-0.5;%some horizontal diffusion data
%add diffusion to the grid points
for cnt1=1:21
for cnt2=16:126%starting above the trunk
%get the angle to always diffuse in direction of the radius
angle=atan(Y(cnt2,cnt1)/X(cnt2,cnt1));
%split the diffusion in the two coordinates, depending on the angle
X(cnt2,cnt1)=X(cnt2,cnt1)+cos(angle)*treeDiffusion(cnt2,cnt1);
Y(cnt2,cnt1)=Y(cnt2,cnt1)+sin(angle)*treeDiffusion(cnt2,cnt1);
%some Vertical diffusion for each point
Z(cnt2,cnt1)=Z(cnt2,cnt1)+(rand-0.5)*0.5;
end
end
%draw the tree
h0 = figure('Units','inches');
pos = h0.Position;
pos(1) = 1; pos(2) = 1;
pos(3) = 7; pos(4) = 7;
h0.Position = pos;
surfl(X,Y,Z,'light')
em...這圖看上去似乎有點(diǎn)不可言說(shuō)的美....
2.2 服道化
下一步振乏,給樹(shù)打個(gè)光,填個(gè)色秉扑,角度調(diào)整一下來(lái)點(diǎn)三維效果慧邮,把坐標(biāo)系顯示去掉,然后一棵綠油油的樹(shù)就出現(xiàn)了舟陆。
%Use as nice green color map (darker at the bottom, lighter at the top)
r=(0.0430:(0.2061/50):0.2491)';%red component
g=(0.2969:(0.4012/50):0.6981)';%green component
b=(0.0625:(0.2696/50):0.3321)';%blue component
map=[r,g,b];%join in a map
for cnt=1:6
%change the lower part to brown for the trunk
map(cnt,:)=[77,63,5]/265;
end
colormap(map)%set the map
view([-37.5,4])%Change the view to see a little more of the Actual 3D tree
lighting phong %some nice lighting
shading interp %remove grid and smoothen the surface color
axis equal %takes care of display in the right proportion
axis([-10 10 -10 10 0 30]) %give some more axis space (for the snow later)
axis off %but don't show axis
hold on %to draw the rest
title('Merry Christmas','color','w',...
'fontsize',25,...
'fontweight','Bold')
接下來(lái)误澳,把圖像底色調(diào)一下,用一個(gè)五彩斑斕的黑加點(diǎn)星空藍(lán)當(dāng)背景(此處在某甲方選手不斷提出色彩需求后最終確定的顏色秦躯,致謝R湮健)
然后再擺幾個(gè)小禮物盒子在樹(shù)下,這過(guò)節(jié)氣氛杠杠的踱承。
set(gcf,'color',[22 32 51]./255)
% Presents
%Draw some presents around the tree (each with random color)
drawPresent(2,-4,0,3,3,2);
drawPresent(-4,3,0,2,3,1.5);
drawPresent(5,3,0,4,3,3);
drawPresent(-14,-5,0,6,3,1);
drawPresent(-9,-10,0,2,2,2);
drawPresent(0,4,0,4,3,3);
drawPresent(-6,-13,0,3,3,3);
2.3 雪花飄飄
最后倡缠,關(guān)鍵部分,撒點(diǎn)雪花茎活,再畫(huà)點(diǎn)彩燈到圖上昙沦,調(diào)整三維圖像的角度,來(lái)點(diǎn)動(dòng)畫(huà)效果载荔,結(jié)束戰(zhàn)斗盾饮!
% Snow
%create some random 3D coordinates for the snow (amount as in setup above)
snowX=(rand(snow-100,1)*25-12.5);
snowY=(rand(snow-100,1)*25-12.5);
snowZ=(rand(snow-100,1)*27);
color0 = jet(length(snowX));
%Note:Some flakes will end up IN the tree but just can't be seen then
for ii = 1:length(snowX)
plot3(snowX(ii),snowY(ii),snowZ(ii),'*','color',color0(ii, :),'markersize',randi(15))%plot coordinates as white snow flakes
% plot3(snowX(ii),snowY(ii),snowZ(ii),'*','color',color0(ii, :))%plot coordinates as white snow flakes
end
h=plot3(snowX,snowY,snowZ,'w*');
im = {};
for ii = 1:180
if mod(ii,3) == 0
h.Visible = 'off';
snowX=(rand(snow,1)*25-12.5);
snowY=(rand(snow,1)*25-12.5);
snowZ=(rand(snow,1)*27);
h=plot3(snowX,snowY,snowZ,'w*');
% pause(0.25)
else
view([ii,4])
% pause(0.1)
end
if ii > 85
frame = getframe(gcf);
im{ii} = frame2im(frame);
end
end
hold off%Done
2.4 輸出gif文件
此處有知識(shí)點(diǎn)!敲黑板!丘损!如何清除cell數(shù)組中的空元素普办。
im(cellfun(@isempty,im))=[];
file2write = 'chris.gif';
for ii = 1:length(im)
[A, map] = rgb2ind( im{ii}, 256);
if ii == 1
imwrite(A, map, file2write, 'gif','LoopCount',Inf,'DelayTime', 0.12);
else
imwrite(A, map, file2write, 'gif','WriteMode','append','DelayTime', 0.12);
end
end
3.結(jié)論
大功告成。
用MATLAB實(shí)現(xiàn)一個(gè)Bling Bling的圣誕樹(shù)徘钥,祝大家玩得開(kāi)心衔蹲。
關(guān)注公眾號(hào) 海洋紀(jì) ,后臺(tái)回復(fù) 圣誕快樂(lè) 可獲取代碼吏饿。