Moving point along a rectangular path

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 Comments

What have you tried so far?
What ideas do you have to conceptually address this?
For example, if you know the starting and end coordinate, you don't the height and width of the outer rectangle.
Let's say it should take 4 direction reversals as shown in your image. How would you compute the width of each reversal? (hint: outer rectangle size divided by number of reversals).
What would be the next step to solve?
I've tried this so far:
clc; clear all ;
N = 50 ;
x1=-990; %era 1
x2=-1010; %era 4
y1=4220; %era 1
y2=4215; %era 4
y3=4210; %era 7
x = [x2,x1,x1,x2,x2,x1];
y = [y3,y3,y2,y2,y1,y1];
S1 = [linspace(x2,x1,N)' y3*ones(1,N)'] ;
S2 = [x1*ones(1,N)' linspace(y3,y2,N)'] ;
S3 = [linspace(x1,x2,N)' y2*ones(1,N)'] ;
S4 = [x2*ones(1,N)' linspace(y2,y1,N)'] ;
S5 = [linspace(x2,x1,N)' y1*ones(1,N)'] ;
S = [S1 ; S2 ; S3 ; S4; S5] ;
plot(S(:,1),S(:,2),'b','linewidth',2) ;
hold on
for i = 1:length(S)
plot(S(:,1),S(:,2),'b','linewidth',2) ;
xlim([x1-1 x2+1]); ylim([y1-1 y3+1]);
hold on
plot(S(i,1),S(i,2),'Or','markersize',10);
hold off
drawnow
pause(0.05)
end
However I would like to add the time variable, which means that Im attributting a velocity to the point but Im not realizing how.
Thanks in advance,
RD
Good start.
The code breaks on the first iteration of the i-loop at this line
xlim([x1-1 x2+1]); ylim([y1-1 y3+1]);
Error using xlim (line 31)
Limits must be a 2-element vector of increasing numeric values.
Error in **** (line 228)
xlim([x1-1 x2+1]); ylim([y1-1 y3+1]);
which makes sense since [x1-1 x2+1] is [-991 -1009].
Tell me more about the goal. Is the problem that the velocity of the ball is not constant? Is the problem that you'd like to define a specific velocity? If so, we'll need a lot more information about the specifics.
Hi Adam,
The error is because it should be:
xlim([x2-1 x1+1]); ylim([y3-1 y1+1]);
And yes, I would like to define a specific velocity, for example (2m/s).
Thanks
RD
Adam Danz
Adam Danz on 19 May 2021
Edited: Adam Danz on 19 May 2021
Are both the X and Y units in meters?
No. They are in km.

Sign in to comment.

 Accepted Answer

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.
  1. 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.
  2. 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 Comments

Thank you for your help Adam, it was very usefull.
However, I would like to introduce a time variable. In other words, I want that the point movement is time dependent and not space dependent. When we set
interval = 0.5; % meters (use whatever interval you want)
x2 : interval : x1
we are admitting that the displacement is in meters, but what I need is to set the displacement in time for a specific velocity of 5m/s for example.
In fact, it is not important that the point finishes the path completely. The important thing is that the point moves along the path with a specific velocity during a certain time.
That's clearer. The horizontal line segments are 20 km judging by the x-axis limits since -990-(-1010)=20 and you mentioned that the axis units are in km. 20km==20000m and at 5m/s it would take 4000 seconds or about 1 hour and 6 min for the marker to travel across the first horizontal line.
The entire track length is 20*3 + 2*10 = 80km or 80000 meters which would require just under 4.5 hours at 5m/s.
Depending on how large the figure is, motion will be quite slow which isn't a problem. Since the animation could potentially go on for a quite a while I suggest you use a timer to control the animation. That way you can use Matlab while the timer operates in the background and updates the figures. You could use a loop with pause(_) instead but it will be much less efficient.
You'll need to create a set of coordinates with equal intervals and they do not have to be the same coordinates as the blue line's coordinates. In fact, you only need 6 coordinates to plot that line optimally. Then you'll need to compute the pause duration between each movement so that the marker moves at the desired rate.
For example, if you want it to move at 5m/s and you select an interval of 50cm, then the timer should update every 0.1 seconds since 5m/s == 500cm/s and there will be 500/50=10 intervals in 1 second so the refresh rate will be 1/10=0.1sec. Don't go lower than 0.001 seconds with the timer.
However, this does not consider overhead time. On each iteration you'll need to refresh the figure using drawnow and this takes several milliseconds depending on the graphics load in your figures. Use the FixedRate ExecutionMode in the timer object to reduce the overhead time as much as possible (nearly 0).
If you have any problems moving forward share what you've got and I can help you get it straighted out. I've used timers a lot and I still have to reference the documentation when I create a timer.
To be clear, the only thing the timer function will do is to update the coordinates of the marker as shown in my answer.
Thank you very much Adam!
I think this solve the problem.
Glad I could help!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!