在 MATLAB 中沿曲线移动的点 [英] 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
推荐答案
这里介绍了如何添加另一个点,该点从独立于第一个点的末端开始滑动.
Here's how you can add another point that starts sliding from the end independent of the first point.
在您的代码中,在行 %#Infinite loop
之前,添加以下内容:
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');
在循环内,在 drawnow
命令之前,添加以下内容:
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)]))
所以你的第二点向下滑动,第一点向上滑动.您可以在 hLine2
和 hTxt2
的定义中根据需要定义点的轨迹
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
这篇关于在 MATLAB 中沿曲线移动的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!