how can I plot a trajectory of moving point ?

4 ビュー (過去 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 Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by