how can I plot a trajectory of moving point ?

6 次查看(过去 30 天)
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 中查找有关 Line Plots 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by