How do I animate my Christmas Tree?

조회 수: 4 (최근 30일)
Catayoun Lissa Eleonore Azarm
Catayoun Lissa Eleonore Azarm 2021년 1월 13일
답변: Raynier Suresh 2021년 1월 19일
Hey, i have programmed a christmas tree (See pic) and now i want the snowflakes to move in an animation. The goal is to save this as a GIF. Does anyone have an idea?
set(gca,'color',[0.25, 0.25, 0.25])
totSlices = 101; %slices to define the contour
rad(1) = 0; %tree root
rad(2:10) = 1.5; % redefine trunk basis
rad(11:totSlices) = 8:-8/90:0; % tree crown radious (from 8 to 0)
plot(rad,1:101)
% Form
[X,Y,Z] = cylinder(rad); %produce a tree formed cylinder
Z = Z*25; %scale the heigth to have a higher tree (25m)
surf(X,Y,Z) %show the cone tree
view([-37.5,4]) %Change the 3D camera view to see better the 3D tree
%Surface
numContHorizt = 21;
numContVert = totSlices;
treeDiffusion = rand(numContVert,numContHorizt)-0.5; %some horizontal diffusion data computed for all points together
%add this diffusion to the grid points
for contH = 1:numContHorizt
for contV = 11:numContVert %starting above the trunk
%get the angle to always diffuse in direction of the radius
angle = atan(Y(contV,contH)/X(contV,contH));
%split the diffusion in the two coordinates, depending on the angle
X(contV,contH) = X(contV,contH)+cos(angle)*treeDiffusion(contV,contH);
Y(contV,contH) = Y(contV,contH)+sin(angle)*treeDiffusion(contV,contH);
%some small Vertical diffusion for each point
Z(contV,contH) = Z(contV,contH)+(rand-0.5)*0.5;
end
end
%draw the present tree
surfl(X,Y,Z,'light')
view([-37.5,4]) %Change the 3D camera view to see better the 3D tree
%colors
r = (0.0430:(0.2061/50):0.2491)'; %red component
g = (0.2969:(0.4012/50):0.6981)'; %green component
b = (0.0625:(0.2696/50):0.3321)'; %blue component
map = [r,g,b]; %join in a color map table
for cnt = 1:5 %change the lower part to brown for the trunk
map(cnt,:) = [77,63,5]/265;
end
colormap(map); %set the map
view([-37.5,4])%Change the view to see a little more of the Actual 3D tree
lighting phong %some nice lighting
shading interp %remove grid and smoothen the surface color
axis equal %takes care of display in the right proportion
axis([-10 10 -10 10 0 30]) %give some more axis space (for the snow later)
axis off %don't show axis
hold on %remain open to draw the rest
%Presents
drawPresent(2,-4,0,3,3,2);
drawPresent(-4,3,0,2,3,1.5);
drawPresent(5,3,0,4,3,3);
drawPresent(-14,-5,0,6,3,1);
drawPresent(-9,-10,0,2,2,2);
drawPresent(0,4,0,4,3,3);
drawPresent(-6,-13,0,3,3,3);
% % Decoration
% annotation('ellipse',[0.3,0.5,0.05,0.05],'Color','red','FaceColor','red')
% annotation('ellipse',[0.6,0.3,0.05,0.05],'Color','red','FaceColor','red')
% annotation('ellipse',[0.55,0.6,0.05,0.05],'Color','red','FaceColor','red')
%Snow
snow = 800; % number of snowflakes
snowX = (rand(snow,1)*25-12.5);
snowY = (rand(snow,1)*25-12.5);
snowZ = (rand(snow,1)*27);
plot3(snowX,snowY,snowZ,'w*') %plot coordinates as white snowflakes
hold off %the scene is done

답변 (1개)

Raynier Suresh
Raynier Suresh 2021년 1월 19일
Using the below code you can create an animated gif for Christmas tree.
filename = 'testAnimated.gif'; % Animated GIF file name
for i = 1:5
set(gca,'color',[0.25, 0.25, 0.25])
totSlices = 101; %slices to define the contour
rad(1) = 0; %tree root
rad(2:10) = 1.5; % redefine trunk basis
rad(11:totSlices) = 8:-8/90:0; % tree crown radious (from 8 to 0)
plot(rad,1:101)
% Form
[X,Y,Z] = cylinder(rad); %produce a tree formed cylinder
Z = Z*25; %scale the heigth to have a higher tree (25m)
surf(X,Y,Z) %show the cone tree
view([-37.5,4]) %Change the 3D camera view to see better the 3D tree
%Surface
numContHorizt = 21;
numContVert = totSlices;
treeDiffusion = rand(numContVert,numContHorizt)-0.5; %some horizontal diffusion data computed for all points together
%add this diffusion to the grid points
for contH = 1:numContHorizt
for contV = 11:numContVert %starting above the trunk
%get the angle to always diffuse in direction of the radius
angle = atan(Y(contV,contH)/X(contV,contH));
%split the diffusion in the two coordinates, depending on the angle
X(contV,contH) = X(contV,contH)+cos(angle)*treeDiffusion(contV,contH);
Y(contV,contH) = Y(contV,contH)+sin(angle)*treeDiffusion(contV,contH);
%some small Vertical diffusion for each point
Z(contV,contH) = Z(contV,contH)+(rand-0.5)*0.5;
end
end
%draw the present tree
surfl(X,Y,Z,'light')
view([-37.5,4]) %Change the 3D camera view to see better the 3D tree
%colors
r = (0.0430:(0.2061/50):0.2491)'; %red component
g = (0.2969:(0.4012/50):0.6981)'; %green component
b = (0.0625:(0.2696/50):0.3321)'; %blue component
map = [r,g,b]; %join in a color map table
for cnt = 1:5 %change the lower part to brown for the trunk
map(cnt,:) = [77,63,5]/265;
end
colormap(map); %set the map
view([-37.5,4])%Change the view to see a little more of the Actual 3D tree
lighting phong %some nice lighting
shading interp %remove grid and smoothen the surface color
axis equal %takes care of display in the right proportion
axis([-10 10 -10 10 0 30]) %give some more axis space (for the snow later)
axis off %don't show axis
hold on %remain open to draw the rest
%Presents
drawPresent(2,-4,0,3,3,2);
drawPresent(-4,3,0,2,3,1.5);
drawPresent(5,3,0,4,3,3);
drawPresent(-14,-5,0,6,3,1);
drawPresent(-9,-10,0,2,2,2);
drawPresent(0,4,0,4,3,3);
drawPresent(-6,-13,0,3,3,3);
% % Decoration
% annotation('ellipse',[0.3,0.5,0.05,0.05],'Color','red','FaceColor','red')
% annotation('ellipse',[0.6,0.3,0.05,0.05],'Color','red','FaceColor','red')
% annotation('ellipse',[0.55,0.6,0.05,0.05],'Color','red','FaceColor','red')
%Snow
snow = 800; % number of snowflakes
snowX = (rand(snow,1)*25-12.5);
snowY = (rand(snow,1)*25-12.5);
snowZ = (rand(snow,1)*27);
plot3(snowX,snowY,snowZ,'w*') %plot coordinates as white snowflakes
hold off %the scene is done
frame = getframe(gcf);
im = frame2im(frame);
[A,map] = rgb2ind(im,256);
if i == 1
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1);
end
end
function drawPresent(dx,dy,dz,scalex,scaley,scalez)
%the standard present coordinates
presentX=[0.5 0.5 0.5 0.5 0.5; 0 1 1 0 0; 0 1 1 0 0; 0 1 1 0 0; 0.5 0.5 0.5 0.5 0.5];
presentY=[0.5 0.5 0.5 0.5 0.5; 0 0 1 1 0; 0 0 1 1 0; 0 0 1 1 0; 0.5 0.5 0.5 0.5 0.5];
presentZ=[0 0 0 0 0; 0 0 0 0 0; 0.5 0.5 0.5 0.5 0.5; 1 1 1 1 1; 1 1 1 1 1];
%draw some presents with random colors
%scale present and move it to the right place and get the plot handle
myHandle=surf((presentX*scalex+dx),(presentY*scaley+dy), (presentZ*scalez+dz));
%some random color map
randColorMap(:,:,1)=repmat(rand,[5,5]);%r component
randColorMap(:,:,2)=repmat(rand,[5,5]);%g component
randColorMap(:,:,3)=repmat(rand,[5,5]);%b component
%Assign colormap just to the plot handle object of the present, so the tree
%does not change color
set(myHandle,'CData',randColorMap)
shading interp %Nice shding + without grid
end % of function
Refer the link for more information on creating GIF :https://www.mathworks.com/help/matlab/ref/imwrite.html#btv452g-1

카테고리

Help CenterFile Exchange에서 Christmas / Winter에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by