注册 登录
编程论坛 Matlab

这个程序能不能画出一棵树出来?

andfuture 发布于 2018-04-10 18:56, 4041 次点击
我用matlab一直画不出图形,是哪里出错了吗?
function drawTree(order, theta, x0, y0, trunk, alpha)
% order:树的层数
% theta:枝条张角
% alpha:枝条与地面夹角
% x0,y0: 根的位置
% trunk:枝条长度
pos = [x0 y0];    % 枝条起始位置
posn = pos + [trunk*cos(alpha) trunk*sin(alpha)];    % 枝条末端位置
x = [pos(1) posn(1)]; y = [pos(2) posn(2)]; plot(x,y,'color',[94/256, 38/256, 18/256],'LineWidth',order);     % 画枝条                        
hold on
% Base case
if(order==1)
    % 画树叶
    t = (1/16:1/8:1)'*2*pi;
 xleaf = posn(1) + 0.2*sin(t);
    yleaf = posn(2) + 0.2*cos(t);
    fill(xleaf,yleaf,'r');
    return;
end
% Recursive call
trunk = trunk * 0.7;   % 让枝条变短
drawTree(order-1, theta, posn(1), posn(2), trunk, alpha+theta);  % 画左分支
drawTree(order-1, theta, posn(1), posn(2), trunk, alpha-theta);  % 画右分支
drawTree(order-1, theta, posn(1), posn(2), trunk, alpha);        % 画中支
clf
0 回复
1