Moving point with fixed distance and random direction

10 ビュー (過去 30 日間)
Hon Man LAU
Hon Man LAU 2019 年 11 月 3 日
回答済み: Neeraj Rajpurohit 2021 年 4 月 7 日
Hi guys. I am doing simulation of user distribution. I have generated some random points to represent the users.
But now, I want to make the points move so that the users are walking with constant speed.
I want to make moving points animiation with
  • Fixed distance (distance between point A and point B = point B to point C)
  • Random direction
And I want to record all the coordinates of points. Thank you.
  2 件のコメント
Daniel M
Daniel M 2019 年 11 月 3 日
編集済み: Daniel M 2019 年 11 月 3 日
Are you saying you have user 1 at point A and you want the coordinates of all possible points to a fixed distance point B? That would just be the surface of a sphere with radius B and origin A.
Where is user 2 located? We need more information.
Adam Danz
Adam Danz 2019 年 11 月 3 日
編集済み: Adam Danz 2019 年 11 月 3 日
Perhaps an illustration would help to define the problem.
"If I had only one hour to save the world, I would spend fifty-five minutes defining the problem, and only five minutes finding the solution." -Albert Einstein

サインインしてコメントする。

回答 (1 件)

Neeraj Rajpurohit
Neeraj Rajpurohit 2021 年 4 月 7 日
DISCLAIMER: These are my own views and in no way depict those of MathWorks.
Greeings,
The following code might help you get started. It shows 10 users moving in random directions with a constant velocity. Note that I used ‘findobj’ and ‘set’ functions to get update plot coordinates. Please find the relevant documentation link below.
clear;
hold off;
box on;
users = 10;
xValues = uint8(rand(1,users) * 100);
yValues = uint8(rand(1,users) * 100);
xdValues = uint8(rand(1,users) * 100);
ydValues = uint8(rand(1,users) * 100);
rt = scatter(yValues, xValues, 'blue');
xlabel('size');
ylabel('size');
axis([0 100 0 100]);
step_size = 1;
while 1
y_t = double(ydValues)-double(yValues);
x_t = double(xdValues)-double(xValues);
x = zeros(1,users);
y = zeros(1,users);
for i=1:users
if(x_t(i) == 0)
y(i) = double(yValues(i))+ sign(y_t(i)) * step_size;
else
m = y_t(i)./x_t(i);
sine = m./sqrt(1+m.^2);
cosine = 1./sqrt(1+m.^2);
x(i) = double(xValues(i)) + cosine.*sign(x_t(i))*step_size;
y(i) = double(yValues(i))+ (abs(sine) .* sign(y_t(i))) * step_size;
end
end
xValues = x;
yValues = y;
d = sqrt(y_t.^2 + x_t.^2);
h = findobj(rt);
pause(.23);
prevX = get(h,'XData');
prevY = get(h,'YData');
set(h,'XData',xValues);
set(h,'YData',yValues);
end

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by