- 积分
- 30
- 注册时间
- 2004-11-17
- 仿真币
-
- 最后登录
- 1970-1-1
|
发表于 2005-6-11 04:19:18
|
显示全部楼层
来自 北京朝阳
Re:分形曲线程序
试了一下那个tree函数,发现实在是非常慢
优化了一下
并提供一个比较实用的旋转数据的函数:
rotatedata
function [newx,newy,newz]=rotatedata(xdata,ydata,zdata,azel,alpha,origin)
%在三维空间种将数据进行旋转,修改自matlab自带的rotate函数
%
% Modified by WaitingForMe
% heroaq_2002@163.com
% 2005/3/4
%
%ROTATE Rotate objects about specified origin and direction.
% ROTATEDATA(Xdata,Ydata,Zdata,[THETA PHI],ALPHA,ORIGIN) rotates the objects with handles H
% through angle ALPHA about an axis described by the 2-element
% direction vector [THETA PHI] (spherical coordinates).
% All the angles are in degrees. The handles in H must be children
% of the same axes.
%
% THETA is the angle in the xy plane counterclockwise from the
% positive x axis. PHI is the elevation of the direction vector
% from the xy plane (see also SPH2CART). Positive ALPHA is defined
% as the righthand-rule angle about the direction vector as it
% extends from the origin.
%
% ROTATEDATA(Xdata,Ydata,Zdata,[X Y Z],ALPHA,ORIGIN) rotates the objects about the direction
% vector [X Y Z] (Cartesian coordinates). The direction vector
% is the vector from the center of the plot box to (X,Y,Z).
%
% See also SPH2CART, CART2SPH.
%
%判断输入变量的个数
if nargin<6
error('Not enough input arguments! Type ''help rotatedata'' to get some help!')
end
%找到旋转的单位轴向量
if prod(size(azel)) == 2 % theta, phi
theta = pi*azel(1)/180;
phi = pi*azel(2)/180;
u = [cos(phi)*cos(theta); cos(phi)*sin(theta); sin(phi)];
elseif prod(size(azel)) == 3 % direction vector
u = azel(:)/norm(azel);
end
alph = alpha*pi/180;
cosa = cos(alph);
sina = sin(alph);
vera = 1 - cosa;
x = u(1);
y = u(2);
z = u(3);
%旋转矩阵
rot = [cosa+x^2*vera x*y*vera-z*sina x*z*vera+y*sina; ...
x*y*vera+z*sina cosa+y^2*vera y*z*vera-x*sina; ...
x*z*vera-y*sina y*z*vera+x*sina cosa+z^2*vera]';
[m,n] = size(xdata);
if isempty(zdata)
zdata=zeros(size(xdata));%在对二维数据进行旋转时,z参数输入可以是空矩阵
end
newxyz = [xdata(:)-origin(1), ydata(:)-origin(2), zdata(:)-origin(3)];
newxyz = newxyz*rot;
newx = origin(1) + reshape(newxyz(:,1),m,n);
newy = origin(2) + reshape(newxyz(:,2),m,n);
newz = origin(3) + reshape(newxyz(:,3),m,n);
这个函数修改自matlab内部函数rotate,用法是一样的,只不过返回输入输入旋转以后的数据
下面是同样层数计算的时间
这个是我的程序的运行时间:
tic;mytree(10,45,0.618);toc
elapsed_time =
0.2810
这个是上面的原程序运行的时间:
tic;fsimwe(10,pi/4,pi/4);toc
elapsed_time =
51.4680
不过程序中生成树枝的方式有一点不一样,上面程序中的树枝好象不往下面长,呵呵
========================================
function mytree(n,ang,p,isrand)
%生成一个数
%n,层数
%ang,左右旋转的角度,[ang1,ang2],如果输入的是一个标量数据,则自动修改为[-ang,ang]
%p,每次长出新芽的生长比例
rt=[0;0];
br=[0;1];
plot([0 0],[0 1]);
if nargin<4 isrand=0; elseif isrand>1 isrand=0.99; end
for i=1:n
[rt,br]=grow(rt,br,ang,p,isrand);
treeplot(rt,br);
end
function [rt1,br1]=grow(rt,br,ang,p,isrand)
l=br-rt;
brext=p.*l;
[rt1,br1]=rot(br,brext,ang);
%加入一个随机成活的控制
if size(rt1,2)>1000 & isrand
alive=rand(1,size(rt1,2));
rt1(:,alive<isrand)=[];
br1(:,alive<isrand)=[];
end
function [rt,brg]=rot(br,brext,ang)
%分别向左右偏离一个角度
if length(ang)==1
ang=[ang -ang];
end
[x,y]=rotatedata(brext(1,:),brext(2,:),[],[0 0 1],ang(1),[0 0 0]);
l=br+[x;y];
[x,y]=rotatedata(brext(1,:),brext(2,:),[],[0 0 1],ang(2),[0 0 0]);
r=br+[x;y];
brg=[l,r];
rt=[br,br];
function treeplot(r,b)
%画出树枝
line([r(1,:);b(1,:)],[r(2,:);b(2,:)]);
==========================================
后来加入了一个随机树控制生长树枝的概率,给一个图: |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
评分
-
1
查看全部评分
-
|