matlab 圣诞树,搞气氛!用MATLAB画一棵Bling Bling的圣诞树

0.前言

圣诞节了,今天推一篇用MATLAB画圣诞树的,效果如下图所示:

3542506cd0d9

chris.gif

1.准备工作

因为这次用的是MATLAB,不像PYTHON一样需要装一些依赖库,要实现本文的效果,只需安装MATLAB即可。

2.动起手来

2.1 画树

要画圣诞树,首先...画棵树,然后再想办法来点缀它。

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...这图看上去似乎有点不可言说的美....

3542506cd0d9

tree.jpg

2.2 服道化

下一步,给树打个光,填个色,角度调整一下来点三维效果,把坐标系显示去掉,然后一棵绿油油的树就出现了。

%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')

接下来,把图像底色调一下,用一个五彩斑斓的黑加点星空蓝当背景(此处在某甲方选手不断提出色彩需求后最终确定的颜色,致谢!)

然后再摆几个小礼物盒子在树下,这过节气氛杠杠的。

3542506cd0d9

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 雪花飘飘

最后,关键部分,撒点雪花,再画点彩灯到图上,调整三维图像的角度,来点动画效果,结束战斗!

3542506cd0d9

chris.jpg

% 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文件

此处有知识点!敲黑板!!如何清除cell数组中的空元素。

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.结论

大功告成。

用MATLAB实现一个Bling Bling的圣诞树,祝大家玩得开心。

3542506cd0d9

chris.gif

关注公众号 海洋纪 ,后台回复 圣诞快乐 可获取代码。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值