Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

graph - Points moving along a curve within MATLAB

I have managed to edit a piece of code that was given to me in order to show a point moving along a curve.

I am trying to find a way to edit this in order to create two independent points moving along this curve or to create a second figure that shows another point moving along a graph. The main point is that the points need to be independent of one another so that an algorithm can be applied to them.

I currently have the following code which gives a single point moving along the curve:

%# control animation speed  
DELAY = 0.01;  
numPoints = 600;  

%# create data  
x = linspace(0,1,numPoints);  
f = 5;  
C = 1-exp(-f);  
y = C*(1-(exp(-f*x))); 

%# plot graph  
figure('DoubleBuffer','on')                  %# no flickering  
plot(x,y, 'LineWidth',2), grid on  
xlabel('x'), ylabel('y'), title('')  

%# create moving point + coords text  
hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...  
        'Marker','o', 'MarkerSize',6, 'LineWidth',2);  
hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...  
    'Color',[0.2 0.2 0.2], 'FontSize',8, ...  
    'HorizontalAlignment','left', 'VerticalAlignment','top');  



%# infinite loop  
i = 1;                                       %# index  
while true        
    %# update point & text  
    set(hLine, 'XData',x(i), 'YData',y(i))     
    set(hTxt, 'Position',[x(i) y(i)], ...  
        'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))          
    drawnow                                  %# force refresh  
    %#pause(DELAY)                           %# slow down animation  

    i = rem(i+1,numPoints)+1;                %# circular increment  
    if ~ishandle(hLine), break; end          %# in case you close the figure  
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's how you can add another point that starts sliding from the end independent of the first point.

In your code, before the line %#Infinite loop, add the following:

hLine2 = line('XData',x(end), 'YData',y(end), 'Color','g', ...  
        'Marker','o', 'MarkerSize',6, 'LineWidth',2);  
hTxt2 = text(x(end), y(end), sprintf('(%.3f,%.3f)',x(1),y(1)), ...  
    'Color',[0.2 0.2 0.2], 'FontSize',8, ...  
    'HorizontalAlignment','left', 'VerticalAlignment','top');  

and inside the loop, before the drawnow command, add the following:

set(hLine2, 'XData',x(end-i), 'YData',y(end-i))     
    set(hTxt2, 'Position',[x(end-i) y(end-i)], ...  
        'String',sprintf('(%.3f,%.3f)',[x(end-i) y(end-i)]))   

So your second point slides down and the first slides up. You can define the trajectory for the point as you wish in the definition of hLine2 and hTxt2 enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...