Moving point along a rectangular path
7 次查看(过去 30 天)
显示 更早的评论
Dear all,
I would like create a point that moves along a path (as in the figure) knowing only the initial and the final position of that point.
After that I would like to plot that movement in a figure.
Thanks in advance for your help.
Best wishes
6 个评论
采纳的回答
Adam Danz
2021-5-19
There are a few coding problems and then there are a few conceptual problems
Coding problems. See inline comments below
% This line plots the blue line and the next line
% holds the plot which is fine....
plot(S(:,1),S(:,2),'b','linewidth',2) ;
hold on
% Move this line out of the loop. You don't need
% to set the axis lims every iteration.
xlim([x1-1 x2+1]); ylim([y1-1 y3+1]);
for i = 1:length(S)
% Why would you need to re-plot the lline on
% every iteration??? Remove this line
%plot(S(:,1),S(:,2),'b','linewidth',2) ;
%xlim([x1-1 x2+1]); ylim([y1-1 y3+1]); % Move out of loop
% no need for this, you already put the plot on hold
% hold on
% I'll discuss this line later....
plot(S(i,1),S(i,2),'Or','markersize',10);
% If you want to turn off the hold, move this after
% the loop or just delete it., but it doesn't
% belong here
% hold off
% These two lines are fine.
drawnow
pause(0.05)
end
Conceptual problem #1
Instead of plotting the circle on each iteration, just move its position using this general idea:
h = plot(0,0,'Or','markersize',10);
for i = 1:n
set(h,'XData',S(i,1),'YData',S(i,2))
drawnow
pause(.05)
end
Conceptual problem #2
This is where you have thing about the data you're plotting. There are two reasons the cirlce moves slower on the vertical sections than on the horizontal sections.
- The aspect ratio of your x and y axes are not equal. Look at the range of values along the x and y axes. The x axis range is about twice as large as the y axis range yet that doesn't show in the ratio of the width and height of the blue path. To fix that, equate the aspect ratio using axis equal.
- Each line segment has 50 points no matter what length the line is. The long horizontal lines have 50 segments and the short vertical lines have 50 points. Your animation moves one coordinate at a time so of course it will be slower on vertical segments than horizontal segments because the coordinates are a lot more dense on vertical segments.
How to proceed
I'd be happy to steer you in the right direction.
First, instead of using linspace to create the coordinates, use a fixed interval. For example, instead of
linspace(x2,x1,N)
use
interval = 0.5; % meters (use whatever interval you want)
x2 : interval : x1
and do that for all line segements so they all have the same interval.
Then the animation should smoothly travel along the path, provided that your aspect ratios are equal as mentioned above.
5 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!