Distance between two points - use ginput!

13 Ansichten (letzte 30 Tage)
Peter
Peter am 26 Mär. 2012
Hi quys, please do you have any idea, how two measure distance between two points? I have my script (below) and a need measure distance between two points. Its mean - when I click (use ginput) six-times - i have six points and I need measure distance between - first+second , third+fourth and fifth+sixth. Its mean I have six points and 3 distances. I need keep my script, I just wanna edit it. Do you have please any idea how to edit it?? Thanks everybody for help!!!
MY SCRIPT:
close all
axis ([0 100 0 100]);
hold on
xy = [];
n = 0;
distance = [];
disp('SELECT POINTS LEFT BUTTON')
disp('RIGHT-CLICK SELECTED LAST POINT')
but = 1;
while but == 1
[xi,yi,but] = ginput(1);
plot(xi,yi,'ro')
n = n+1;
xy(:,n) = [xi;yi];
end

Akzeptierte Antwort

Thomas
Thomas am 26 Mär. 2012
to find the distance between the two point use
d(1)= sqrt((x_point2-x_point1)^2+(y_point2-y_point1)^2);
d(2)= sqrt((x_point4-x_point3)^2+(y_point4-y_point3)^2);
d(3)= sqrt((x_point6-x_point5)^2+(y_point6-y_point5)^2);
you can generalize this and run it in a for loop
d(i)=sqrt((x_point(2*i)-x_point(2*i-1))^2+(y_point(2*i)-y_point(2*i-1))^2);
This is not the exact code but, you should be able to code it according to your requirements.
In you example
x_point1=xy(1,1)
y_point1=xy(2,1)
and so on..

Weitere Antworten (1)

Matt Tearle
Matt Tearle am 26 Mär. 2012
Just because I can, here's a one-line version:
d = sqrt(sum([diff(reshape(xy(1,:),2,[]));diff(reshape(xy(2,:),2,[]))].^2))
In case you care, what this does is takes each x coordinate separately and reshapes them:
[x1 x2 x3 x4 ...]
to
[x1 x3 ...]
[x2 x4 ...]
Then takes the difference down the columns:
[x2-x1 x4-x3 ...]
Then the same with y, resulting in a matrix
[x2-x1 x4-x3 ...]
[y2-y1 y4-y3 ...]
Then squares, sums the columns, and takes the square root:
[sqrt((x2-x1)^2+(y2-y1)^2) sqrt((x4-x3)^2+(y4-y3)^2) ...]

Kategorien

Mehr zu Data Exploration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by