# Calculating the distance between two points and get direction [closed]

I am programming a molecular dynamics program with periodic boundary conditions. Here, I have a problem:

I have a list of coordinates like

list = RandomReal[{0, 1}, {5, 2}]


And now, I want to have the distances.

I do that with

EuclideanDistance @@@ Subsets[list, {2}]


But now, I do not know the direction, so if the distance not squared was positive or negative. Does someone know, how I can obtain the sign? Because I need that for computing my periodic boundary conditions

## closed as off-topic by Daniel Lichtblau, m_goldberg, MarcoB, Alex Trounev, EdmundJun 26 at 21:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Alex Trounev, Edmund
If this question can be reworded to fit the rules in the help center, please edit the question.

• EuclideanDistanceis always positive. Please clarify "direction"! – Ulrich Neumann Jun 19 at 13:57
• @UlrichNeumann So you have $\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$. For me, the "direction" is the sign of $x_1 - x_2$ and $y_1 - y_2$, which can be positive or negative, so I also want to have $x_1 - x_2$ and $y_1 - y_2$ explicitly. – Armani42 Jun 19 at 14:03
• @ Armani42 What is the "direction" if x1-x2<0 and y1-y2>0 ? – Ulrich Neumann Jun 19 at 14:11

Here is a solution which gives you the distance and the difference

list = RandomReal[{0, 1}, {5, 2}]; (*points*)
sl=  Subsets[list, {2}]  (* all point pairs*)

m=Map[{Sqrt[(#[] - #[]).(#[] - #[])], #[] - #[]} &,\sl]
(*{{0.72911, {0.681672, -0.258698}}, {0.410144, {0.359548, 0.197341}}, ...}*)


The first element of m is the distance

m[[All, 1]] == EuclideanDistance @@@ sl
(*True*)


the last is the difference vector.

• Thank you, that really helped me :) – Armani42 Jun 20 at 9:24