|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
<P>function fractree(X,Y,length,direction)</P>
<P>% Script draws a fractal tree for a specified<BR>% number of branches, angle & 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<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<0.25<BR> colStr='g-';<BR> end<BR> if length<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>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> |
|