calculate the distance between 2 points

hi every one i want to calculate the distance between 2 points(p1 & p2), so which of the following equation choose to do that:-
d = (x(p1)-x(p2))^2+(y(p1)-y(p2))^2;
or
d = sqrt((x(p1)-x(p2))^2+(y(p1)-y(p2))^2);
or any one from the above equations
thanks

 Accepted Answer

The common use distance formula is the second one:
d = sqrt((x(p1)-x(p2))^2+(y(p1)-y(p2))^2);

11 Comments

but whew i try first one
d = (x(p1)-x(p2))^2+(y(p1)-y(p2))^2; instead of second one
d = sqrt((x(p1)-x(p2))^2+(y(p1)-y(p2))^2);
i felt that is useful for me, can i use the first one???
thank you dear
Of Course you can use it, but you have to remember that it won't be an Euclidean norm.
There are a lot of distance function in mathematics, e.g.:
http://en.wikipedia.org/wiki/E-statistic
http://en.wikipedia.org/wiki/Hamming_distance
http://en.wikipedia.org/wiki/Levenshtein_distance
http://en.wikipedia.org/wiki/Chebyshev_distance
http://en.wikipedia.org/wiki/Canberra_distance
http://en.wikipedia.org/wiki/Lee_distance
I like the distance in the Minkowski space: http://en.wikipedia.org/wiki/Minkowski_distance
It can be *negative* with the obvious unexpected effects.
thank you dear friends for your advices
Please accept this answer if you think it solved your question.
The hypot function in MATLAB provides a more compact way to code the Euclidean distance that is equivalent algebraically, but less susceptible to overflow, and probably more efficient:
d = hypot(x(p1)-x(p2), y(p1)-y(p2));
thank you dear Rob
dear
after calculating the distance (d) between (p1 and p2), i want to change the distance (for example suppose that i found the distance 10 unit and i want to change it into 12, that means i must move the points according this changing so
by which equation can i do that
You can either move P1, or P2 or both. There is an infinite number of possible end positions. Which one do you prefer?
dear jan...my suggestion is as the following:-
find the difference between old distance(10) and new distance(12) which will be 2... then move each point by half of distance(1)
@majid, but in what direction?
one point to the left and another to the right

Sign in to comment.

More Answers (1)

Jan
Jan on 22 Mar 2012
You can simply try it, if you are not familiar with the Euclidean Norm (hint: use this term to ask Google or WikiPedia):
Set the variables to coordinates, whichj have a known distance, e.g. [0,0] and [2,0].

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!