how can I plot a trajectory of moving point ?

조회 수: 18 (최근 30일)
omar th
omar th 2022년 9월 26일
답변: omar th 2022년 9월 27일
if I have random MOVING point, can I plot its trajectory as a line ?
for example when this point move from A to B, so I want to plot solid line between A and B,
when I used Linestayle as '-' to draw solid line doesnt work
Please have a look to the below code, and I would appreciate if you have any help
Thanks in advance
function MOVE(npts, v, radius, center)
npts=1; v=28.8/3.6; radius=1000; center=[0 0];
direction = rand(npts, 1) * 2 *pi;
theta = rand(npts, 1) * 2*pi;
r = radius * sqrt(rand(npts, 1));
XY = [r .* cos(theta(:)) + center(1), r .* sin(theta(:)) + center(2)];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hdots = plot(XY(:,1), XY(:,2),'Parent', hax,'Marker', '.','Color', 'k','LineStyle', '-','MarkerSize', 12);
hold(hax, 'on')
axis(hax, 'equal')
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
for i=1:100
[XY, direction] = movePoint(XY, direction, v, radius, center);
set(hdots, 'XData', XY(:,1), 'YData', XY(:,2))
drawnow
end
end
function [XYnew, direction] = movePoint(XY, direction, v, radius, center)
% Compute the next position of the points
DX = [cos(direction(:)) .* v, sin(direction(:)) .* v];
XYnew = XY + DX;
end

채택된 답변

Chunru
Chunru 2022년 9월 26일
npts=1; v=28.8/3.6; radius=1000; center=[0 0];
MOVE(npts, v, radius, center)
function MOVE(npts, v, radius, center)
direction = rand(npts, 1) * 2 *pi;
theta = rand(npts, 1) * 2*pi;
r = radius * sqrt(rand(npts, 1));
XY = [r .* cos(theta(:)) + center(1), r .* sin(theta(:)) + center(2)];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hdots = plot(XY(:,1), XY(:,2), 'Parent', hax,'Marker', '.','Color', 'k','LineStyle', '-','MarkerSize', 12);
hold(hax, 'on')
axis(hax, 'equal')
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
for i=1:100
[XY, direction] = movePoint(XY, direction, v, radius, center);
XData = [hdots.XData XY(:, 1)];
YData = [hdots.YData XY(:, 2)];
set(hdots, 'XData', XData, 'YData', YData)
drawnow
end
end
function [XYnew, direction] = movePoint(XY, direction, v, radius, center)
% Compute the next position of the points
DX = [cos(direction(:)) .* v, sin(direction(:)) .* v];
XYnew = XY + DX;
end

추가 답변 (1개)

omar th
omar th 2022년 9월 27일
Thank you so much

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by