• Remix
  • Share
  • New Entry

on 20 Nov 2023
  • 11
  • 27
  • 0
  • 0
  • 1924
drawframe(1);
function drawframe(f)
persistent hgrot
if(f == 1)
% set axis and view
ax=gca;
view(3);
hold on;
% add hgtransform for rotation
hg1=hgtransform('Parent',ax);
hgrot = @(hg,axs,ang) set(hg,'Matrix', makehgtform([axs,'rotate'],ang));
%set camera lighting
camlight('headlight','infinite');
lighting gouraud;
% ones for generating surfaces
one=ones(1,21);
mysurf=@(X,Y,Z,ea,fc) surf(X,Y,Z,EdgeAlpha=ea,FaceColor=fc,Parent=hg1);
[Xc,Yc,~]=cylinder([0.02,0.04,0.04]);
Zc=[5.5;5;1]*one;
mysurf(0.25+Xc,Yc,Zc,0,'b');
[Xs,Ys,~]=cylinder([0.04,0.07,0.07,0.05,0.05]);
Zs=[0.1;0.1;0.09;0.09;0]*one;
Xs=0.25+[Xs;flipud(Xs)];
Ys=[Ys;flipud(Ys)];
Zs=3.9+[Zs;-flipud(Zs)];
mysurf(Xs,Ys,Zs,1,'k');
[Xh,Yh,~]=cylinder([0.04,0.06,0.06]);
Zh=[1;0.9;0]*one;
Xh=0.25+Xh;
x=Xh(end,:);
y=Yh(end,:);
z=Zh(end,:);
for i = 1:12
[x,y,z]=rotate(x,y,z,[0,1,0],pi/12);
Xh=[Xh;x];
Yh=[Yh;y];
Zh=[Zh;z];
end
mysurf(Xh,Yh,Zh,0.2,'g');
t=linspace(0,pi/4,51)';
u=pi/12*linspace(-1,1,51);
R=5+2*t*u.^2;
X=R.*(sin(t)*cos(u));
Y=R.*(sin(t)*sin(u));
Z=R.*(cos(t)*ones(size(u)));
x=0.07*cos(pi/12*(1:2:23));
y=0.07*sin(pi/12*(1:2:23));
for i=1:12
xyzl=[X(:,end),Y(:,end),Z(:,end)];
xyzl=[xyzl;2*xyzl(end,:)-xyzl(end-1,:)];
xl=xyzl(:,1);
yl=xyzl(:,2);
zl= xyzl(:,3);
mysurf(0.25+X,Y,Z,0,'r');
plot3(0.25+[x(i),xl(25)],[y(i),yl(25)],[4,zl(25)],'k',LineWidth=1.5,Parent=hg1);
plot3(0.25+xl,yl,zl, 'k', LineWidth = 1.5,Parent=hg1);
plot3(0.25+X(end,:),Y(end,:),Z(end,:),'k',Parent=hg1);
[X,Y,Z] = rotate(X,Y,Z,[0,0,1],pi/6);
end
axis([-4,4,-4,4,-1,6]);
axis equal;
hold off
elseif(f<5)
hgrot(gcf().Children(1).Children(2),'y',pi/3);
elseif(f<10)
hgrot(gcf().Children(1).Children(2),'x',-pi);
elseif(f<15)
hgrot(gcf().Children(1).Children(2),'y',-pi/3);
elseif(f<20)
hgrot(gcf().Children(1).Children(2),'x',-pi/3);
else
hgrot(gcf().Children(1).Children(2),'z',(f-5)*pi/60)
end
drawnow
end
function [X,Y,Z]=rotate(X,Y,Z,U,theta)
c=cos(theta);
s=sin(theta);
ux=U(1);
uy=U(2);
uz=U(3);
M=[ux*ux*(1-c)+c,uy*ux*(1-c)-uz*s,uz*ux*(1-c)+uy*s
uy*ux*(1-c)+uz*s,uy*uy*(1-c)+c,uz*uy*(1-c)-ux*s
uz*ux*(1-c)-uy*s,uy*uz*(1-c)+ux*s,uz*uz*(1-c)+c];
for i = 1:size(X,1)
xyz= M*[X(i,:);Y(i,:);Z(i,:)];
X(i,:) = xyz(1,:);
Y(i,:) = xyz(2,:);
Z(i,:) = xyz(3,:);
end
end
Animation
Remix Tree