5

曲線に沿って移動するポイントを表示するために与えられたコードを編集することができました。

この曲線に沿って移動する 2 つの独立したポイントを作成するか、グラフに沿って移動する別のポイントを示す 2 番目の図を作成するために、これを編集する方法を見つけようとしています。要点は、アルゴリズムをポイントに適用できるように、ポイントが互いに独立している必要があるということです。

私は現在、曲線に沿って移動する単一のポイントを与える次のコードを持っています:

%# 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
4

1 に答える 1

2

最初のポイントとは関係なく、端からスライドを開始する別のポイントを追加する方法は次のとおりです。

コードの行の前に%#Infinite loop、次を追加します。

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');  

ループ内で、drawnowコマンドの前に次を追加します。

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)]))   

つまり、2番目のポイントが下にスライドし、最初のポイントが上にスライドします。hLine2およびの定義で、ポイントの軌道を自由に定義できます。hTxt2 ここに画像の説明を入力してください

于 2011-04-16T15:26:36.167 に答える