在MATLAB中沿曲线移动的点

时间:2011-04-16 15:15:38

标签: matlab graph point

我设法编辑了一段代码,这些代码是为了显示沿曲线移动的点而给我的。

我正在尝试找到一种方法来编辑它,以便创建沿此曲线移动的两个独立点,或创建第二个图形,显示沿图形移动的另一个点。 重点是点需要彼此独立,以便可以将算法应用于它们。

我目前有以下代码,它提供了沿曲线移动的单个点:

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

1 个答案:

答案 0 :(得分: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)]))   

所以你的第二点向下滑动,第一点向上滑动。您可以在hLine2hTxt2的定义中根据需要定义点的轨迹 enter image description here

相关问题