Borrar filtros
Borrar filtros

Multiple moving points in an area with the given velocity

2 visualizaciones (últimos 30 días)
Anu Sharma
Anu Sharma el 30 de Ag. de 2023
Respondida: Voss el 30 de Ag. de 2023
How to write a code for random number of points moving in an area at a given velocity.
There are random number of points ( 50) in an area of 600*600, moving from one point to another at given velocity (10m/s).
The code for moving one point in an area of 25*25 is given below. Similarly I want to move random number (>=50) of points in the given area (600*600).
clc
close all
figure;axis square;
set(gca,'XLim',[0,25],'YLim',[0,25]);
MOVINGPOINT = rectangle('Parent',gca,'Position',[4.5,4.5,1,1],'Curvature',[1,1]);
startPos = [2,2];
endPos = [20,20];
vel = 10;
[x_dis] = endPos(1)-startPos(1);
[y_dis] = endPos(2)-startPos(2);
Dis = sqrt(x_dis^2+y_dis^2);
Ttime = Dis/vel;
x_vel = (vel*x_dis)/Dis;
y_vel = (vel*y_dis)/Dis;
t = 0.01:0.01:Ttime;
X =startPos(1)+x_vel*t;
Y = startPos(1)+y_vel*t;
trajectory = [X;Y];
for frameNo = 1:length(t)
set(MOVINGPOINT,'Position',[trajectory(1,frameNo)-0.25,trajectory(2,frameNo)-0.25,0.5,0.5]);
frames(frameNo) = getframe;
end
Please help me with the random number (>=50) of points moving with the given velocity and to calculate the trajectory of the points.

Respuestas (2)

recent works
recent works el 30 de Ag. de 2023
Editada: Voss el 30 de Ag. de 2023
Moving random number of points in an area at a given velocity:
clc
close all
% Define the area of movement
x_min = 0;
x_max = 600;
y_min = 0;
y_max = 600;
% Generate random number of points
num_points = 50;
points = rand(num_points, 2)*(x_max-x_min) + x_min;
% Define the velocity
vel = 10;
% Calculate the trajectory of the points
trajectory = zeros(num_points, 3);
for i = 1:num_points
[x_dis, y_dis] = points(i, :) - points(i, 1:end-1);
Dis = sqrt(x_dis^2+y_dis^2);
Ttime = Dis/vel;
x_vel = (vel*x_dis)/Dis;
y_vel = (vel*y_dis)/Dis;
t = 0.01:0.01:Ttime;
trajectory(i, 1:end) = points(i, :) + x_vel*t;
end
Error using -
Too many output arguments.
% Plot the trajectory of the points
figure;
plot(trajectory(:, 1), trajectory(:, 2));
  1 comentario
Anu Sharma
Anu Sharma el 30 de Ag. de 2023
[x_dis, y_dis] = points(i, :) - points(i, 1:end-1);
showing error (Too much output argument)

Iniciar sesión para comentar.


Voss
Voss el 30 de Ag. de 2023
Here the start and end position of each point is random; therefore the trajectories and start-to-end distances are random, but since all points move at the same speed, the time taken to go from start position to end position is different for different points. If a point gets to its end position before other points then it stays there until all points are done moving (at which time the program is done running).
figure;
ax = gca();
vel = 10;
x_min = 0;
x_max = 600;
y_min = 0;
y_max = 600;
Npts_min = 50;
Npts_max = 100;
Npts = randi([Npts_min Npts_max]);
points = plot(NaN(2,Npts),NaN(2,Npts),'.');
set(ax,'XLim',[x_min x_max],'YLim',[y_min y_max]);
axis square
startPos = rand(Npts,2).*[x_max-x_min y_max-y_min]+[x_min y_min];
endPos = rand(Npts,2).*[x_max-x_min y_max-y_min]+[x_min y_min];
x_dis = endPos(:,1)-startPos(:,1);
y_dis = endPos(:,2)-startPos(:,2);
Dis = sqrt(x_dis.^2+y_dis.^2);
Ttime = Dis/vel;
x_vel = vel*x_dis./Dis;
y_vel = vel*y_dis./Dis;
t = 0.01:0.01:max(Ttime);
t = min(t,Ttime);
X = startPos(:,1)+x_vel.*t;
Y = startPos(:,2)+y_vel.*t;
trajectory = permute(cat(3,X,Y),[1 3 2]);
trajectory_cell = num2cell(trajectory);
for frameNo = 1:size(t,2)
set(points,{'XData','YData'},trajectory_cell(:,:,frameNo));
drawnow
end

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by