Distance between two points - use ginput!
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Matt Tearle
2012년 3월 26일
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) ...]
카테고리
도움말 센터 및 File Exchange에서 Data Exploration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!