Find path between two points with X and Y coordinates

Hi all, I want to draw path between points, but the paths cannor be diagonal. Therefore the paths should have corners. Think it as a rectangle, you want to move from a corner to another corner, but cannot move diagonally. How can I show that on matlab? The coordinates are attached.
Thanks

2 件のコメント

darova
darova 2019 年 12 月 29 日
Can you show your attempts? What have you tried?
Erkin Karatas
Erkin Karatas 2019 年 12 月 29 日
clear all
clc
p = xlsread('coordinates');
for k =1:length(p)
for i = 1:length(p)
x = [p(k,1) p(i+1,1) p(i+1,1) p(i+1,1)];
y = [p(k,2) p(1,2) p(1,2) p(i+1,2)];
x1 = [p(k,1) p(k,1) p(k,1) p(i+1,1)];
y1 = [p(k,2) p(i+1,2) p(i+1,2) p(i+1,2)];
plot(x,y,x1,y1)
i = i+1;
hold on
end
k=k+1;
end
when k is larger than 1, the code does not print the thing I want.

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

 採用された回答

darova
darova 2019 年 12 月 29 日

1 投票

I reached a succes
p = xlsread('coordinates');
x = p(:,1);
y = p(:,2);
plot(x,y,'.r')
hold on
for i = 1:length(x)-1
plot([x(i) x(i+1)],[y(i) y(i)])
plot([x(i+1) x(i+1)],[y(i) y(i+1)])
pause(0.5)
end
hold off

5 件のコメント

Erkin Karatas
Erkin Karatas 2019 年 12 月 29 日
編集済み: Erkin Karatas 2019 年 12 月 29 日
Thank you.
Actually, I want to connect all points and I want to print all X-Y coordinates. Does this connect all points? I want to calculate manhattan distance between all points
Image Analyst
Image Analyst 2019 年 12 月 29 日
Do you mean to draw a line from every point on the graph to every other point on the graph?
Erkin Karatas
Erkin Karatas 2019 年 12 月 29 日
thanks again.
Yes, and I want to store the length of every path. While visiting the points in the file 'coordinate' if I the line crosses an obstacle(which is attached as file), I am going to assign the length of that path as infinite.
clear all
clc
close all
p = xlsread('coordinates');
x = p(:,1);
y = p(:,2);
plot(x,y,'.r')
obst = xlsread('engel');
xo = obst(:,1); %x coordinate
yo = obst(:,2); %y coordinate
wo = obst(:,3); %width
ho = obst(:,4); %height
for i= 1:length(obst)
Rec = rectangle('Position', [xo(i) yo(i) wo(i) ho(i)]); %plotting obstacles
i = i+1;
end
hold on
for i = 1:length(x)-1
plot([x(i) x(i+1)],[y(i) y(i)])
plot([x(i+1) x(i+1)],[y(i) y(i+1)])
pause(0.5)
end
hold off
thank you
Erkin Karatas
Erkin Karatas 2019 年 12 月 29 日
In the end I should have a 50x50 matrix which stores the lengths of each path
clc
clearvars
close all
XY = xlsread('coordinates');
result1=pdist2(XY,XY,'cityblock')
This is how I store my lengths, as I said earlier, if any path crosses any obstacle then the length of that path should be inf or something very large(the value actually does not matter)
darova
darova 2019 年 12 月 29 日
  • While visiting the points in the file 'coordinate' if I the line crosses an obstacle(which is attached as file)
It's too complicated for this forum. You should incorporate your own script
To connect each point with each points use this code:
for i = 1:length(x)-1
for j = i+1:length(x)-1
plot([x(i) x(j)],[y(i) y(i)])
plot([x(j) x(j)],[y(i) y(j)])
pause(0.01)
end
end

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

その他の回答 (0 件)

タグ

質問済み:

2019 年 12 月 29 日

コメント済み:

2019 年 12 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by