简体   繁体   中英

Drawing history of a point in a 3Dplot animation in Matlab

I have a few 3D points in Matlab and i have made an animation from these points using plot3. The points are stored in arrays and then some of them are joined with lines. The code is as follows:

tstart = 0.0;
tend = 5;
tsim = tend - tstart;
dt = 0.001;
Dn = tsim/dt + 1;
t0 = 5.0/dt;
f = 0.4;
for(i=1:Dn)
    t(i) = tstart + (i-1)*dt;
    rf_P0(i) = sin(2*pi*f*t(i));
    rf_P1(i) = cos(2*pi*f*t(i));
end
figure(1);
set(gcf,'Renderer','OpenGL');
RF_P0 = plot3(rf_P0(1,1),rf_P0(1,2),rf_P0(1,3),'o','MarkerSize',10,'MarkerFaceColor','r'); % point 1
hold on;
RF_P1 = plot3(rf_P1(1,1),rf_P1(1,2),rf_P1(1,3),'o','MarkerSize',10,'MarkerFaceColor','r'); % point 2
RF_D0 = plot3([rf_P0(1,1) rf_P1(1,1)], [rf_P0(1,2) rf_P1(1,2)], [rf_P0(1,3) rf_P1(1,3)],'LineWidth',4,'Color','k'); %line from point 1 to 2

i = 1;
while i<=size(rf_P0,1)
     set(RF_P0,'XData',rf_P0(i,1),'YData',rf_P0(i,2),'ZData',rf_P0(i,3));
     set(RF_P1,'XData',rf_P1(i,1),'YData',rf_P1(i,2),'ZData',rf_P1(i,3));
     set(RF_D0,'XData',[rf_P0(i,1) rf_P1(i,1)],'YData',[rf_P0(i,2) rf_P1(i,2)],'ZData',[rf_P0(i,3) rf_P1(i,3)]);
     drawnow;
     i=i+1;
 end

This works fine, this will animate the motion of two points and the line between them. Both points and line is erased and redrawn again in each iteration. Now what i want to do is i want to show the history of the motion of one of the points while all other points and lines should remove and redraw as usual. Lets say i want to 'hold on' the plot of point 1 throughout the animation showing the track of its motion.

thanks in advance.

If I understand what you want (and your data format), you can do this by plotting a line for all previous values up to the current point value each time, deleting the previous, eg

delete(rf_histline);
rf_histline= plot3(rf_P0(1:i,1),rf_P0(1:i,2),rf_P0(1:i,3),'-');

The other option is to redraw everything each time (instead of using the set handle method) and clear the whole plot each time and replot the new points and the history.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM