评论

收藏

[C++] 【花式】基于matlab实现落叶动态展示【含Matlab源码 047期】

编程语言 编程语言 发布于:2021-08-07 10:11 | 阅读数:287 | 评论:0

一、源代码
function leafpile3D(N,leaftype,motion)
%leafpile3D(N,leaftype) makes a random falling pile of N leaves
% of leaftype 'oak' or 'maple' or 'mixed'.  N. Barlow 8/31/20
%leafpile3D(N) makes a falling mixed pile of N leaves.
%leafpile3D makes a falling pile of 60 mixed leaves.
%leafpile3D(N,leaftype,0) will suspend the leaves (no falling)
%There is no drag enforced. It is up to the user to incorporate a drag
%model on line 39. Have fun!
%The mathematical functions describing the leaf shapes were created by Hamid Naderi
%Yeganeh and given at https://blogs.scientificamerican.com/guest-blog/how-to-draw-with-math/
 
if nargin==0,N=60; leaftype='mixed'; motion=1; end
if nargin==1, leaftype='mixed'; motion=1; end
if nargin==2, motion=1;end
 
%types of leaves
if strcmp(leaftype,'maple')==1
  type=cell(1,N); type(:)={'maple'};
elseif strcmp(leaftype,'oak')==1
  type=cell(1,N); type(:)={'oak'};
else
  type=cell(1,N);type(:)={'maple'};
  r=rand(1,N);type(r<=0.5)={'oak'};
end
 
%make some random leaf info
for n=1:N
  xv(n)=(-1)^randi(2)*rand; yv(n)=(-1)^randi(2)*rand; %random (x,y)
  z0(n)=(-1)^randi(2)*rand; %random initial heights
  tv(n)=(-1)^randi(2)*rand;bv(n)=(-1)^randi(2)*rand; gv(n)=(-1)^randi(2)*rand; %random rotations
  cv(n)=randi(N); %random colors
end
 
g=9.81; %acceleation of gravity in m/s^2
 
if motion==1
  for t=0:0.1:1000
    %%YOUR MISSION: EDIT LINE BELOW TO INCORPORATE DRAG
    z=z0+1/2*g*t^2; %here, z is positive down
    %%%%%%%%%%%%%%%%
    makeleaves3D(N,type,xv,yv,-z,tv,bv,gv,cv)
    drawnow
    if max(-z)<=-1
      break
    end
  end
else
  makeleaves3D(N,type,xv,yv,-z0,tv,bv,gv,cv)
end
 
function makeleaves3D(N,type,xv,yv,zv,tv,bv,gv,cv)
clf; C=autumn(N);
set(gca,'color',[129/256 182/256 221/256]);
hold on; axis equal;axis([-1.2 1.2 -1.2 1.2 -1.2 1.2]); box on; view(45,30);
for n=1:N
  if strcmp(type(n),'oak')==1     
    oak(xv(n),yv(n),max(-1.1,zv(n)),0.5,tv(n)*2*pi,bv(n)*pi/10,gv(n)*pi/10,C(cv(n),:))
  elseif strcmp(type(n),'maple')==1
    maple(xv(n),yv(n),max(-1.1,zv(n)),0.12,tv(n)*2*pi,bv(n)*pi/10,gv(n)*pi/10,C(cv(n),:))
  end
end
fill3([-1.2 1.2 1.2 -1.2],[-1.2 -1.2 1.2 1.2],[-1.2 -1.2 -1.2 -1.2],[34/256 139/256 34/256])

二、运行结果
DSC0000.png


三、备注
版本:2014a


关注下面的标签,发现更多相似文章