Matlab实现 递归算法生成3维分形树ByLinking

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Matlab实现递归算法生成3维分形树

注:此算法树根在侧面,需对坐标轴进行旋转便可得到上图效果

以下代码全部粘贴到一个M文件中命名为TreeByL即可运行

为方便网友研读代码加入了大量注释

同时愿与matlab程序爱好者进行交流:********************

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Matlab实现递归算法生成3维分形树

%ByLinking

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function TreeByL

L=15;%主干长

a=0;

b=pi/3;

r=0;%r=pi/5;

%分支生成函数

makeBranch(0,0,0,L,a,b,r);

% a在XOZ平面投影与X轴夹角b与Y轴的夹角r与主干的夹角

function makeBranch (x,y,z,L,a,b,r)

B=pi/5;s1=1.5;s2=3;s3=1.2;%B枝干的倾斜度C主干的倾斜度s1细腻程度s2分支收缩速度s3主干收缩速度

% B=pi/5;s1=1.5;s2=2.4;s3=1.35;

if L>s1

x1=x+L/s2*cos(a)*cos(r);

y1=y+L/s2*sin(a);

z1=z+L/s2*cos(a)*sin(r);

x1R=x1+L/s2*cos(a-b)*cos(r);

y1R=y1+L/s2*sin(a-b);

z1R=z1+L/s2*cos(a-b)*sin(r);

x1L=x1+L/s2*cos(a+b)*cos(r);

y1L=y1+L/s2*sin(a+b);

z1L=z1+L/s2*cos(a+b)*sin(r);

x1F=x1+L/s2*cos((a-b)*sin(a))*cos(r+atan(1/cos(a)));

y1F=y1+L/s2*sin((a-b)*sin(a));

z1F=z1+L/s2*cos((a-b)*sin(a))*sin(r+atan(1/cos(a)));

x1B=x1+L/s2*cos((a-b)*sin(a))*cos(r-atan(1/cos(a)));

y1B=y1+L/s2*sin((a-b)*sin(a));

z1B=z1+L/s2*cos((a-b)*sin(a))*sin(r-atan(1/cos(a)));

%------------------------------------------------------

x2=x+L/s2*cos(a)*cos(r);

y2=y+L/s2*sin(a);

z2=z+L/s2*cos(a)*sin(r);

x2R=x2+L/s2*cos(a-b)*cos(r);

y2R=y2+L/s2*sin(a-b);

z2R=z2+L/s2*cos(a-b)*sin(r);

x2L=x2+L/s2*cos(a+b)*cos(r);

y2L=y2+L/s2*sin(a+b);

z2L=z2+L/s2*cos(a+b)*sin(r);

x2F=x2+L/s2*cos((a-b)*sin(a))*cos(r+atan(1/cos(a)));

y2F=y2+L/s2*sin((a-b)*sin(a));

z2F=z2+L/s2*cos((a-b)*sin(a))*sin(r+atan(1/cos(a)));

x2B=x2+L/s2*cos((a-b)*sin(a))*cos(r-atan(1/cos(a)));

y2B=y2+L/s2*sin((a-b)*sin(a));

z2B=z2+L/s2*cos((a-b)*sin(a))*sin(r-atan(1/cos(a)));

plot3([x,x2],[y,y2],[z,z2],'k');hold on;set(gcf,'color','w');grid on;view(pi/2,0);%axis off;xlabel('X Label');ylabel('Y Label');zlabel('Z Label');

set(gca,'xlim',[0,25],'ylim',[-15,15],'zlim',[-15,15]);

plot3([x2,x2R],[y2,y2R],[z2,z2R],'g');hold on;

plot3([x2,x2L],[y2,y2L],[z2,z2L],'g');hold on;

plot3([x2,x2B],[y2,y2B],[z2,z2B],'g');hold on;

plot3([x2,x2F],[y2,y2F],[z2,z2F],'g');hold on;

plot3([x1,x1R],[y1,y1R],[z1,z1R],'g');hold on;

plot3([x1,x1L],[y1,y1L],[z1,z1L],'g');hold on;

plot3([x1,x1B],[y1,y1B],[z1,z1B],'g');hold on;

plot3([x1,x1F],[y1,y1F],[z1,z1F],'g');hold on;

%------------------------------------------------------ makeBranch(x2,y2,z2,L/s3,a,b,r);

makeBranch(x2L,y2L,z2L,L/s2,a,b,r+B);

makeBranch(x2B,y2B,z2B,L/s2,a,b,r-B);

makeBranch(x2F,y2F,z2F,L/s2,a,b,r+B);

makeBranch(x2R,y2R,z2R,L/s2,a,b,r-B);

makeBranch(x1L,y1L,z1L,L/s2,a,b,r+B);

makeBranch(x1B,y1B,z1B,L/s2,a,b,r-B);

makeBranch(x1F,y1F,z1F,L/s2,a,b,r+B);

makeBranch(x1R,y1R,z1R,L/s2,a,b,r-B);

end

相关文档
最新文档