animition of solar system
利用matlab生成太阳系运动的动画
code
clc,clear,close all
x0=0;y0=0;r0=80;Lmin0=0;Lmax0=0;T0=2160;w0=0*pi/T0;q0=0;
x1=0;y1=0;r1=40;Lmin1=25;Lmax1=30;T1=1080;w1=pi/T1;q1=0;
x2=0;y2=0;r2=20;Lmin2=8;Lmax2=10;T2=180;w2=pi/T2;q2=0;
r3=10;Lmin3=3;Lmax3=5;T3=30;w3=pi/T3;q3=0;
hh=figure('numbertitle','off','name','Solar System by Joe
zhouman','Color',[1 1 1]);
sun=line(0,0,'color','r','marker','.','markersize',r0);
earth=line(x0,y0,'color','k','marker','.','markersize',r1);
moon=line(x1,y1,'color','m','marker','.','markersize',r2);
satellite=line(x2,y2,'color','g','marker','.','markersize',r3);
axis off
title({'Solar System','Sun--Earth--Moon--Satellite','by Joe,zhouman'}...
,'fontname','Segoe
Script','FontSize',12,'FontWeight','normal','Color','black');
text(-50,50,'sun');
line(-55,50,'color','r','marker','.','markersize',r0);
text(-50,40,'earth');
line(-55,40,'color','k','marker','.','markersize',r1);
text(-50,30,'moon');
line(-55,30,'color','m','marker','.','markersize',r2);
text(-50,20,'satellite');
line(-55,20,'color','g','marker','.','markersize',r3);
text(33,50,'the orbit of the moon');
line([27.5 32.5],[50 50],'linestyle','-.','color','r','markersize',8);
text(33,40,'the orbit of the satellite');
line([27.5 32.5],[40 40],'linestyle','--','color','b','markersize',4);
text(33,30,'the orbit of the earth');
line([27.5 32.5],[30 30],'linestyle',':');
s1=0:.01:2*pi;
line(Lmax1*cos(s1),Lmin1*sin(s1),'linestyle',':');
x0=Lmax0*cos(q0);y0=Lmin0*sin(q0);
x1=x0+Lmax1*cos(q1);y1=y0+Lmin1*sin(q1);
x2=x1+Lmax2*cos(q2);y2=y1+Lmin2*sin(q2);
x3=x2+Lmax3*cos(q3);y3=y2+Lmin3*sin(q3);
mlx=x2;mly=y2;
slx=x3;sly=y3;
moonline=line('xdata',mlx,'ydata',mly,'linestyle','-.','color','r','markersize',8);
satelliteline=line('xdata',slx,'ydata',sly,'linestyle','--','color','b','markersize',4);
axis([-60,60,-60,60]);
t=0;
TERM=4500;
for idx=1:TERM
if not(ishandle(hh))
return
end
q0=t*w0;q1=t*w1;q2=t*w2;q3=t*w3;t=t+1;
if t>=4500
t=0;
end
x0=Lmax0*cos(q0);y0=Lmin0*sin(q0);
x1=x0+Lmax1*cos(q1);y1=y0+Lmin1*sin(q1);
x2=x1+Lmax2*cos(q2);y2=y1+Lmin2*sin(q2);
x3=x2+Lmax3*cos(q3);y3=y2+Lmin3*sin(q3);
mlx=[mlx x2];mly=[mly y2];
slx=[slx x3];sly=[sly,y3];
set(sun,'xdata',x0,'ydata',y0);
set(earth,'xdata',x1,'ydata',y1);
set(moon,'xdata',x2,'ydata',y2);
set(satellite,'xdata',x3,'ydata',y3);
set(moonline,'xdata',mlx,'ydata',mly);
set(satelliteline,'xdata',slx,'ydata',sly);
drawnow
frame=getframe(hh);
im{idx}=frame2im(frame);
end
filename = 'solar_system_by_joe_zhouman.gif';
for idx=1:TERM
[A,map]=rgb2ind(im{idx},256);
if idx == 1
imwrite(A,map,filename,'gif','LoopCount',0,'DelayTime',0.01);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.01);
end
end