声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 5643|回复: 27

[共享资源] 非常漂亮的分形树

[复制链接]
发表于 2005-10-16 21:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<P>function fractree(X,Y,length,direction)</P>
<P>%  Script draws a fractal tree for a specified<BR>%       number of branches, angle &amp; distance factor ...<BR>%<BR>%   Each branch is 'distance-factor' times the last in length<BR>%  and leans off to the left/right at the given angle. The depth<BR>%  of the tree limits the total number of branches drawn.<BR>%   Random trees can be generated from an integer seed. </P>
<P><BR>global angle distFactor branches minLen branchFac;</P>
<P>if  nargin&lt;1</P>
<P>%  For initial call, get user input for parameters</P>
<P>  branchFac = [ 1 -1 0 ];</P>
<P>  fprintf('\n  Fractal Trees\n\n');<BR>  angle=pi*(input('Enter angle (0 - 180, zero for random) : ')/180);<BR>  distFactor=input('Enter distance factor (0 - 1, zero for random) : ');<BR>  if  angle==0  |  distFactor==0<BR>    seed=input('Enter the random number seed (integer) : ');<BR>    rand('seed',seed);<BR>  end<BR>  branches=input('Enter the number of branches (2 or 3) : ');<BR>  maxDepth=input('Enter the maximum depth of the tree : ');</P>
<P>%  Thanks to Ken Crounse for the stopping criterium<BR>  if distFactor~=0<BR>    minLen=distFactor.^(maxDepth-0.5);<BR>  else<BR>    minLen=0.5.^(maxDepth-0.5);<BR>  end</P>
<P>%  Declare figure window for output<BR>  figure(1);<BR>  axis('square','equal','off');<BR>  hold on<BR>  fractree(0,0,1,0);<BR>  hold off<BR>  disp('  ');</P>
<P>else</P>
<P>%  Calculate end position of the branch<BR>  newX=X+(length*sin(direction));<BR>  newY=Y+(length*cos(direction));</P>
<P>%  Set the colour for a branch, twig or leaf<BR>  colStr='r-';<BR>  if length&lt;0.25<BR>    colStr='g-';<BR>  end<BR>  if length&lt;0.03125<BR>    colStr='w-';<BR>  end</P>
<P>%  Draw the branch<BR>  plot([X newX],[Y newY],colStr);</P>
<P><BR>%  If the branch is reasonably long...<BR>  if length&gt;minLen</P>
<P>%  extend new branches to it<BR>    for brNo=1:branches</P>
<P>%  Calculate length and direction of the next branch<BR>      if distFactor==0<BR>        newLength=length*( (rand(1)*.5) + .25 );<BR>      else<BR>        newLength=length*distFactor;<BR>      end</P>
<P>      if angle==0<BR>        newDir=direction+( ((rand(1)*1.7453)+0.1745) * branchFac(brNo) );<BR>      else<BR>        newDir=direction+(angle*branchFac(brNo));<BR>      end</P>
<P>%  Call the routine again to draw next branch<BR>      fractree(newX,newY,newLength,newDir);</P>
<P>    end</P>
<P>  end</P>
<P>end</P>

本帖被以下淘专辑推荐:

回复
分享到:

使用道具 举报

发表于 2005-10-16 21:33 | 显示全部楼层

回复:(brianlei)非常漂亮的分形树

我怎么画不出来?<BR><BR>总是出现错误<BR>Error in ==&gt; D:\MATLAB6p5\work\abc.m<BR>On line 41  ==&gt;   fractree(0,0,1,0);
发表于 2005-10-17 18:22 | 显示全部楼层
我也老是有错,画不出来
 楼主| 发表于 2005-10-21 12:45 | 显示全部楼层

回复:(mantou)我也老是有错,画不出来

<BR>在命令窗口输入:fractree回车后<BR>就有提示.按提示输入,就可以.<BR>不过,建议树深不要取过大值!否则将花费很多时间.<BR><BR>
发表于 2005-10-26 20:44 | 显示全部楼层

回复:(brianlei)非常漂亮的分形树

<P>我也画不出非常漂亮的分形树<BR>出现这样的错误呀<BR>??? Undefined function or variable 'fractree'.</P>
 楼主| 发表于 2005-10-26 22:34 | 显示全部楼层

回复:(woainiQQ)回复:(brianlei)非常漂亮的分形...

<P>确定其M文件放在MATLAB安装目录下的work文件夹里?</P>
发表于 2005-10-27 22:40 | 显示全部楼层
<P>我也没画出来啊</P>[em03]
发表于 2006-1-5 16:30 | 显示全部楼层

yeah

确实非常漂亮;
发表于 2006-1-5 22:37 | 显示全部楼层
<P>不错</P>
发表于 2006-1-6 08:48 | 显示全部楼层

回复:(brianlei)回复:(woainiQQ)回复:(brianl...

是放在那个文件夹里,没有用,出现了同样的错误报告!请问这是怎么回事?
发表于 2006-1-6 09:02 | 显示全部楼层
不好意思,确实可以,很漂亮!<BR>M文件的文件名要和函数名一样才行!惭愧呀!
发表于 2006-1-7 14:46 | 显示全部楼层
beautiful![em07][em07][em07][em07]
发表于 2006-1-8 11:02 | 显示全部楼层
  差点搞到我死机。。。。  厉害的,哈哈
发表于 2006-1-8 12:22 | 显示全部楼层

回复:(66666yy) 差点搞到我死机。。。。 厉害的,...

<DIV class=quote><B>以下是引用<I>66666yy</I>在2006-1-8 11:02:48的发言:</B><BR>  差点搞到我死机。。。。  厉害的,哈哈</DIV>
<br>选的分叉数太多了吧
发表于 2006-4-1 19:22 | 显示全部楼层
<P>very good</P>
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 21:58 , Processed in 0.060288 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表