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!