clear all
for n=1:1
filename=strcat("D:\2021fall\applied_math\tri_body\mat-new\mat_threebody",num2str(n),".dat");
% threebody=readtable(filename);
% threebody=table2cell(threebody);
% threebody=cell2mat(threebody);
threebody=importdata(filename);
clc
T=900;
a=zeros(T,3);
b=zeros(T,3);
c=zeros(T,3);

for i=1:T
    for j=1:3
        a(i,j)=threebody(i,j);
        b(i,j)=threebody(i,j+3);
        c(i,j)=threebody(i,j+6);

    end
end
fps=30;
filename2=strcat('mat_threebody',num2str(n),'.avi');
myvideo=VideoWriter(filename2);
myvideo.FrameRate=fps;
open(myvideo);

for i=1:T
    for k=1:3
        tm1=a(i,k);
        tm2=b(i,k);
        tm3=c(i,k);
        A=[tm1,tm2,tm3];
   xmax(k)=1.5*max(A);
   xmaxk=max(xmax);
    xmin(k)=1.5*min(A);
    xmink=min(xmin);
    end

    plot3(a(i,1),a(i,2),a(i,3),'ro','MarkerFacecolor','r','MarkerSize',6)
  hold  on
    plot3(b(i,1),b(i,2),b(i,3),'go','MarkerFacecolor','g','MarkerSize',6)
    plot3(c(i,1),c(i,2),c(i,3),'ko','MarkerFacecolor','k','MarkerSize',6)
%   if i<=200
%         p=1;
%     else if i>200                                         用于限制轨迹长度
%             p=i-180;
%
%         end
%     end
    p=1;
    plot3(a(p:i,1),a(p:i,2),a(p:i,3),'r-')

     plot3(b(p:i,1),b(p:i,2),b(p:i,3),'g-')
      plot3(c(p:i,1),c(p:i,2),c(p:i,3),'k-')

   %  xm1=xmin(1); xma1=xmax(1);xm2=xmin(2); xma2=xmax(2);xm3=xmin(3); xma3=xmax(3);


 axis([xmink xmaxk xmink xmaxk xmink xmaxk]);
       % axis([-20 70 -100 100 -10 10])
    drawnow
%      ay=i/T*180;
%    az=30+i/T*15;
%      view([ay az])
  view(2)
    hold off


    frame=getframe(gcf);
    im=frame2im(frame);
    writeVideo(myvideo,im);
end

close(myvideo);
end