Moving point along a rectangular path
14 views (last 30 days)
Show older comments
Ricardo Duarte on 17 May 2021
Commented: Adam Danz on 21 May 2021
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.
Adam Danz on 19 May 2021
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....
% 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
%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....
% 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.
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
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
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.
More Answers (0)
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!