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
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!