Distance between two points - use ginput!
Show older comments
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
Accepted Answer
More Answers (1)
Matt Tearle
on 26 Mar 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) ...]
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!