필터 지우기
필터 지우기

Moving point along a rectangular path

조회 수: 19 (최근 30일)
Ricardo Duarte
Ricardo Duarte 2021년 5월 17일
댓글: Adam Danz 2021년 5월 21일
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
Adam Danz 2021년 5월 19일
편집: Adam Danz 2021년 5월 19일
Are both the X and Y units in meters?
Ricardo Duarte
Ricardo Duarte 2021년 5월 19일
No. They are in km.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
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.
  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
Ricardo Duarte
Ricardo Duarte 2021년 5월 21일
Thank you very much Adam!
I think this solve the problem.
Adam Danz
Adam Danz 2021년 5월 21일
Glad I could help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by