matlab画颗圣诞树给自己的对象或者朋友

一、matlab代码

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 radiusangle=atan(Y(cnt2,cnt1)/X(cnt2,cnt1));%split the diffusion in the two coordinates, depending on the angleX(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 pointZ(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')
% View and format
%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 trunkmap(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')%self explaining
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);
% 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:720if mod(ii,3) == 0h.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)elseview([ii,4])%         pause(0.1)endif ii > 85frame = getframe(gcf);im{ii} = frame2im(frame);end
end
hold off%Done
im(cellfun(@isempty,im))=[];
% end % of function
file2write = 'chris.gif';
for ii = 1:length(im)[A, map] = rgb2ind( im{ii}, 256);if ii == 1imwrite(A, map, file2write, 'gif','LoopCount',Inf,'DelayTime', 0.12);elseimwrite(A, map, file2write, 'gif','WriteMode','append','DelayTime', 0.12);end
end% ============= private functionsfunction r=tree(h)%Gives a profile for the tree
for cnt=1:length(h)if(h(cnt)==0)%no Width at the bottom. Ensures a "closed" trunkr(cnt)=0;end%smaller radius for the trunkif (h(cnt)>0 && h(cnt)<=3)r(cnt)=1.5;end%reduce radius gradually from 8 to 0. Note: will only work with a trunk heigth%of 3 and a whole tree heigth of 25. Scale the height of the tree in%the "draw tree" section, since the cylinder command will return a 1%unit high cylinder anywayif(h(cnt)>3)r(cnt)=8-(h(cnt)-3)*0.3636;endendend % of function%Draws a present with the given coordinate + size in a random color
%Note:Given coordinates apply to the lower front + left corner of the
%present (the one closest to the viewer) as seen in the plot
function drawPresent(dx,dy,dz,scalex,scaley,scalez)%the standard present coordinates
presentX=[0.5 0.5 0.5 0.5 0.5; 0 1 1 0 0; 0 1 1 0 0; 0 1 1 0 0; 0.5 0.5 0.5 0.5 0.5];
presentY=[0.5 0.5 0.5 0.5 0.5; 0 0 1 1 0; 0 0 1 1 0; 0 0 1 1 0; 0.5 0.5 0.5 0.5 0.5];
presentZ=[0 0 0 0 0; 0 0 0 0 0; 0.5 0.5 0.5 0.5 0.5; 1 1 1 1 1; 1 1 1 1 1];%draw some presents with random colors
%scale present and move it to the right place and get the plot handle
myHandle=surf((presentX*scalex+dx),(presentY*scaley+dy), (presentZ*scalez+dz));
%some random color map
randColorMap(:,:,1)=repmat(rand,[5,5]);%r component
randColorMap(:,:,2)=repmat(rand,[5,5]);%g component
randColorMap(:,:,3)=repmat(rand,[5,5]);%b component
%Assign colormap just to the plot handle object of the present, so the tree
%does not change color
set(myHandle,'CData',randColorMap)
shading interp %Nice shding + without gridend % of function

二、圣诞树图像

三、此代码圣诞树只可以旋转,也可以添加背景音乐,更完整的代码请关注微信公众号海洋纪回复“圣诞快乐”即可获得。

四、给对象或者朋友画指定对象的圣诞树
可以更改第47行代码title(‘Merry Christmas’,‘color’,‘w’,…)在Merry Christmas后面加上自己对象或者朋友的名字,比如Merry Chirstmas to Cao DR.效果如下图所示:

就是给指定对象画的圣诞树啦!也提前祝各位CSDNer圣诞节快乐!
郑重声明:matlab代码非原创

MATLAB画颗圣诞树相关推荐

  1. 【程序猿的浪漫】教你如何用代码给她画棵圣诞树~

    在这圣诞来临的时刻, 各行各业都有自己的表达爱的方式. 还不知道给自己喜欢的人准备什么惊喜的, 赶快看过来! 学会这一招,今年圣诞,你就是朋友圈最靓的那个崽! 优麒麟研发小哥哥熬夜为大家送上宝典, 教 ...

  2. Python画棵圣诞树 ~ Merry Christmas ~

    圣诞节快到了,用python.turtle画棵圣诞树吧~_Ding2langdang的博客-CSDN博客 转载于Ding2langdang 最近圣诞节快到啦,CSDN的热搜也变成了"代码画颗 ...

  3. 搞个气氛 用MATLAB画一棵精致的圣诞树

    2021-12-19 出续集啦!! 在本文的基础上,实现Matlab制作圣诞树和圣诞快乐歌!欢迎试用! 接着奏乐接着舞 Matlab制作圣诞树和圣诞快乐歌_liu08_13的博客-CSDN博客 0.前 ...

  4. 如何用java画一颗圣诞树

    如何用java画一颗圣诞树 哈喽 ,今天教各位一个有趣的代码,利用for循环简单地画一个圣诞树. 代码如下: ```java public class Dan {public static void ...

  5. 圣诞节用Python画一颗圣诞树

    圣诞节用Python画一颗圣诞树 前言 一.初级圣诞树 二.中级圣诞树 三.高级圣诞树 四.超高级圣诞树 总结 前言 正在学Python的你是不是很想在圣诞节给女朋友一个惊喜? 哦!忘了,或许你压根儿 ...

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

    0.前言 马上圣诞节了,今天推一篇用MATLAB画圣诞树的,效果如下图所示: 1.准备工作 因为这次用的是MATLAB,不像PYTHON一样需要装一些依赖库,要实现本文的效果,只需安装MATLAB即可 ...

  7. HTML 画一颗圣诞树

    可以用 HTML 画一颗圣诞树的方法是: 使用 HTML 的 div 元素来表示圣诞树的主体. 使用 HTML 的 span 元素来表示圣诞树的枝干和叶子. 使用 CSS 的背景颜色和边框属性来给圣诞 ...

  8. 用MATLAB画圣诞树的源代码

    女神要我给她画圣诞树?高情商这样画! 画圣诞树的代码都在这里啰!由于期末时间紧张,这里就不对代码做详细解释了(源代码有一定的注释)!请见谅! 第一个是水彩画滤镜: 参考了(图像滤镜算法--水彩滤镜.漫 ...

  9. 圣诞节!教你用Python画棵圣诞树

    作者 | 糖甜甜甜,985高校经管研二,擅长用 Python.R.tableau 等工具结合统计学和机器学习模型做数据分析. 来源 | 经管人学数据分析(ID:DAT-2017) 如何用Python画 ...

  10. MATLAB画高斯曲线

    MATLAB画高斯曲线 高斯曲线   均值不同 高斯曲线  方差不同 高斯曲线  方差sigma=1,改变均值a(-6, 0,+6) 高斯曲线  均值a=0,改变方差sigma (0.5, 1, 2, ...

最新文章

  1. 回到未来 – 大胆畅想如何追赶并超越腾讯模式
  2. Oracle 11g dataguard主库坏块修复
  3. 百度翻译十年:语种全球首破200大关,质量提升30个百分点,每天翻译超千亿字符...
  4. 单词缩写(abbr.cpp)每日一题
  5. java注释日志打印_java 日志文件打印
  6. java复用类_java复用类
  7. html5 多页面共享数据库,可以跨页面使用HTML5 Web SQL数据库吗?(Can HTML5 Web SQL databases be used across pages?)...
  8. php使用ftp远程上传文件类(解决主从文件同步问题的简单方法)
  9. Conference Related to social network.
  10. java 读取manifest_使用JAVA从jar文件读取MANIFEST.MF文件
  11. Python提示错误 module 'request' has no attribute 'urlretrieve'
  12. Ionic生命周期与注意点
  13. 刘济舟:《基于IAST交互式安全测试实践的初步探索》
  14. 移植fastboot到2440
  15. daterangepicker 清空_Date Range Picker 中文网
  16. 测角误差估计算法matlab,Harris角点检测 及 Matlab实验
  17. Java 电商订单管理设计,基于Java的电商网站的设计与实现
  18. el-table纵向和横向都有滚动条的时候,右下角会出现白色方块区域
  19. 第一次书写标书和述标经历
  20. 个人电脑重装WINDOWN XP 论坛

热门文章

  1. 三角脉冲信号的表达式_脉冲发生器产生一个单三角脉冲,其波形如图所示,例1写出电压U 与.pdf...
  2. 图片去水印的原理_图片中的水印怎么去除
  3. 多项式计算的Horner 方法
  4. KY-RTI分布仿真技术:第二章 系统安装
  5. 搜狐财报:年度盈利,长跑依旧
  6. Linux代理服务器 Centos Nginx安装、反向代理配置、Nginx开机自启动及日志每天自动分割压缩
  7. 珍大户 认知世界的经济学 经济学核心原理 思维导图
  8. mm游戏大全HTML5小游戏,HTML5小游戏——看你有多色(示例代码)
  9. 小程序账号注册完整流程
  10. 计算机网络的分类 ppt,《计算机网络》PPT课件.ppt