Calculate distance between 2 points (read)?

1 Ansicht (letzte 30 Tage)
Erwin Folkesang
Erwin Folkesang am 27 Mär. 2014
Bearbeitet: Star Strider am 27 Mär. 2014
I was asked to write a simple program that reads the coordinates of 2 points and then calculates the distance between them.
I really don't know where to start im a complete rookie so a thorough explanation is appreciated.
Thank you!

Antworten (2)

Image Analyst
Image Analyst am 27 Mär. 2014
How about using imdistline() or improfile() if you have the Image Processing Toolbox.
See attached spatial calibration demo.
  2 Kommentare
Erwin Folkesang
Erwin Folkesang am 27 Mär. 2014
But how do you first create 2 points in the middle of nowhere and then use imdistline() to calculate the distance between them? (sorry for rookie question)
Image Analyst
Image Analyst am 27 Mär. 2014
This is for when you are displaying an image. You click on each end of the line. Click one end and get a rubber band line, then click on the other end. What kind of display are you wanting to click on????

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 27 Mär. 2014
Bearbeitet: Star Strider am 27 Mär. 2014
You can use the hypot function, or write a version of your own:
x(1,:) = rand(2,1);
x(2,:) = rand(2,1)*10;
dx = diff(x,1)
d = hypot(dx(1), dx(2))
figure(1)
plot(x(1,1), x(1,2), 'pb')
hold on
plot(x(2,1), x(2,2), 'pb')
plot([x(1,1) x(2,1)], [x(1,2) x(2,2)], '-r')
hold off
text(x(1,1),x(1,2), sprintf(' (%.2f, %.2f)', x(1,:)), 'FontSize',8)
text(x(2,1),x(2,2), sprintf(' (%.2f, %.2f)', x(2,:)), 'FontSize',8)
mnx = mean(x,1);
text(mnx(1),mnx(2), sprintf(' Distance = %.2f',d), 'FontSize',8)
xlabel('x \rightarrow')
ylabel('y \rightarrow')
axis([xlim+[-.2 .2] ylim+[-.2 .2]])
grid
EDIT: Added plot

Kategorien

Mehr zu Graphics Object Properties 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