%HungDung6
%L系统植物----顶花
clear;clc;
%%%(一)
for Q=1:3
QQ=(Q-1)*2
X1=[1 1 1 1]*QQ;
Y1=[1 1 1 1]*QQ;
Z1=[0 0 0 0];
n=1.5;
JJ=5;
for J=1:JJ %J画1层
X3=zeros(1,4^J);Y3=X3;Z3=X3;
if J<3;TT=2;else TT=1;end
for K=1:4^(J-1) %K画1支
if K==1 & J==1;
plot3([X1(1) X1(1)],[Y1(1) Y1(1)],[-4 Z1(1)],....
'g-','linewidth',7-J);hold on;
end
X2(1)=X1(K)+1/J^n;
X2(2)=X1(K)-1/J^n;
X2(3)=X1(K);
X2(4)=X1(K);
%
Y2(1)=Y1(K);
Y2(2)=Y1(K);
Y2(3)=Y1(K)+1/J^n;
Y2(4)=Y1(K)-1/J^n;
%
Z2(1:4)=Z1(1:4)+1/J*(n^2)*[1 1 1 1];
%
if TT==1
for L=1:4
if J>4
plot3([X1(K) X2(L)],[Y1(K) Y2(L)],[Z1(K) Z2(L)],....
'y-','linewidth',6-J);hold on;
else
plot3([X1(K) X2(L)],[Y1(K) Y2(L)],[Z1(K) Z2(L)],....
'g-','linewidth',6-J);hold on;
end
if J==JJ
plot3([X1(K) X2(L)],[Y1(K) Y2(L)],[Z1(K) Z2(L)],....
'r-','linewidth',6-J+1);hold on;
end
end
end%TT=1
%
if TT==2
XP=[X2(1) X1(K) X2(2)];
YP=[Y2(3) Y1(K) Y2(4)];
ZP=[Z2(1) Z1(K) Z2(2)];
%
P2X=polyfit(XP,ZP,2);
%P2Y=polyfit(YP,ZP,2);
P2Y=P2X;
%
XPP=X2(1):-0.01:X2(2);
SI=size(XPP);Y1PP=Y1(K)*ones(SI(2));
Z22X=polyval(P2X,XPP);
%
YPP=Y2(3):-0.01:Y2(4);
SI=size(YPP);X1PP=X1(K)*ones(SI(2));
%Z22Y=polyval(P2Y,YPP);
Z22Y=Z22X;
%
if J>4 && J<JJ
plot3(XPP,Y1PP,Z22X, 'y-','linewidth',6-J);hold on;
plot3(X1PP,YPP,Z22Y, 'y-','linewidth',6-J);hold on
else
plot3(XPP,Y1PP,Z22X, 'g-','linewidth',6-J);hold on;
plot3(X1PP,YPP,Z22Y, 'g-','linewidth',6-J);hold on
end
if J==JJ
plot3(XPP,Y1PP,Z22X, 'r-','linewidth',J);hold on;
plot3(X1PP,YPP,Z22Y, 'r-','linewidth',J);hold on
end
end%TT=2
%
X3(4*(K-1)+1:4*(K-1)+4)=X2(1:4);
Y3(4*(K-1)+1:4*(K-1)+4)=Y2(1:4);
Z3(4*(K-1)+1:4*(K-1)+4)=Z2(1:4);
end%K
X1=X3;
Y1=Y3;
Z1=Z3;
end%J
end%Q
grid;
评论