2点間を移動する物体​の一秒毎の位置の履歴​を作成したい.

無作為に二次元の出発点と到着点を設定し、その間を例えば毎秒20mで移動する物体を考え、その物体の一秒毎の二次元位置の履歴を作成したいです。
例えば出発点(x,y)=(2445,5679) , 到着点(x,y)=(4512,7738)と設定された時
秒数 X座標 Y座標
0 2445 5679
1  2455   5688
2 2465 5697
・   ・   ・
・   ・   ・
・   ・   ・
と返されるようなコードを作成したいです。

 채택된 답변

Kenta
Kenta 2020년 6월 27일

0 개 추천

clear;clc;close all
% create points
p1=randi(1000,[1 2]);
p2=randi(1000,[1 2]);
v=20;%velocity
% the distance between p1 and p2
distance=pdist([p1;p2]);
figure;plot([p1(1);p2(1)],[p1(2);p2(2)]);hold on
for i=1:floor(distance/v)+1
x(i)=(p2(1)-p1(1))*(v*(i-1))/distance+p1(1);
y(i)=(p2(2)-p1(2))*(v*(i-1))/distance+p1(2);
scatter(x(i),y(i),20,'r');pause(.2)
end
こんにちは、繰り返しプロットしていくためにforを、整数の乱数発生のために、randiを使えば上のようにできます。
ここでいう、「履歴」はx, yにそれぞれ格納されています。

댓글 수: 1

hironori aoki
hironori aoki 2020년 6월 29일
わかりやすい説明ありがとうございます。
参考にさせていただきます。

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2020년 6월 26일

댓글:

2020년 6월 29일

Community Treasure Hunt

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

Start Hunting!