# Calculate distance between two points N,W,E,S

Ok so i am stuck at this: I need to calculate distance between 2 points...

For example: I have 30x30 square and point1 is at X4,Y5 and point2 is at X30,Y23 now i need to get the shortest distance from point1 to point2. By wich way is the shortest North,East,West,South...

I know i have to do that by "pythagorean theorem" but problem is if point1 is (X4,Y5) and point2 is (X28,Y6) for example... now the shortest way would be to go East and then you come to the right side out and just go one squeare to South. And the shortest distance would be (5)squares.

I dont know exactly to say what i need, but you will probably see on image2 on the link!

Here is an example of 30x30 and here is a full image of what i am talking about

Here would the shortest be (6).

Here would the shortest be (3).

Here would the shortest be (21).

Here would the shortest be (5, something).

Here would the shortest be (4).

Thank you for any help people! :)

-
I think for your distances that 'wrap around' you want to add 1 to each. What's the distance between two squares that are adjacent? 1, right? then if there's a square between thew two, the distance should be 2. You're counting the steps going from the center of one square to another, not from the nearest edges of the two. – Mitch Feb 12 '11 at 21:34

What you're looking for is not the Euclidean distance, calculated using the Pythagorean theorem (which is good for an infinitely extending plane with a continuum of points (and a few other restrictions)), but instead, as mentioned, the taxicab or Manhattan distance, with the additional restriction that it is on a finite set of points that 'wrap around' (are on a torus).

The Manhattan distance between two points is just

$$d( p_1, p_2 ) = |x_1 - x_2| + |y_1 - y_2|.$$

(if $p_1 = (x_1,y_1)$ and $p_2 = (x_2, y_2)$).

This is the sum of the differences in each of the coordinates - the absolute value is just to make sure it doesn't matter which point you start with.

The above works if you have an infinite plane. For the additional constraint that you really have a finite area (it is on a torus) to account for the fact that it may be shorter to go South rather than North or East instead of West, take the min of all possible directions you could go, East/West, and North/South both mod the width/height of the bounded plane. So the better distance function uses that above and tries the minimum in two directions for each coordinate:

$$\begin{eqnarray} d_t(p_1, p_2) = \min(&&d(p_1, p_2) ,\\ && d((x_1+30, y_1),p_2),\\ && d((x_1, y_1+30),p_2),\\ && d((x_1+30, y_1+30),p_2)). \end{eqnarray}$$

These four possibilities are displayed in your image and the above is just taking the smallest one.

Note that this latter calculation will work on a torus also for the Euclidean or other distance measures.

-
hi... thanks for your response! Could you please take a look at new links that i have added to my question! Just to make shure we think same (you understood my question)... – FeRtoll Feb 12 '11 at 19:37
@FeRtoll: I see what you're doing with your examples, but it seems a little 'mixed' to me. That is, if the two points appear closer without having to go over the edges, you seem to want to use the Euclidean distance (the Pythagorean formula). But if it seems closer over the edge, you take the Manahattan distance. To get this, in my definition for $d_t$, you'd change $d(p_1,p_2)$ to be the Euclidean distance instead of Manhattan, and keep the rest Manhattan. I don't think you really -want- to do this, I'm just saying that that's how you would do it if you really wanted to. – Mitch Feb 12 '11 at 21:32
i am mixing yes.. i need to find the shortest distance from the center of one of those points to other ones... i need to calculate how much is distance by going from point1 to point2 WestSouth and for all the sides of world and the lowest one is my shortest distance... something like that! I havae posted a question here but i dont understand much from your examples :D i am a noob at mathematics! :P Manhattan is a city? bridege? hehe i joking i see we not talking about bridges! :) Could you write me in numbers and some examples? :( Thank you! – FeRtoll Feb 12 '11 at 22:28
Manahattan refers to the idea that in Manhattan which has mostly just east/west and north/south streets in a perfect grid, you can only follow rectilinear streets to get from any point to any other (you can't cut corners or go in diagonals). – Mitch Feb 12 '11 at 22:53
With help of my girlfriend i understand a little and it works perfectly! :) Thanks Mitch! – FeRtoll Feb 13 '11 at 14:09

If I understand correctly, you are interested in the distances computed by only moving along horizontal and vertical directions, and not diagonally. Is that right?

Next, you are assuming your square is actually the surface of a torus, so that by "going out" of the right side you re-enter from the left one and so on. Is that right too?

If both are true, then the answer to your question is pretty simple. First of all, consider the case when the two points are on a same row, one of them in column X and the other in column X'. Then their distance is the minimum between |X-X'| (where || denote the modulus, or absolute value) and 30-|X-X'| (of course in the case when the width of the square is different from 30, the new value has to be substituted). If the shortest way is "moving within" the square, the minimum is |X-X'|, while if it is shortest to take a shortcut through the sides, then 30-|X-X'| is the distance.

When the points are on different rows, you have just to add this "horizontal" distance to the "vertical" distance, computed in the same way, but using the Y-coordinates.

Hope this helps!

-
Commenting myself: if the distances are to be computed as you seem to describe (that is, moving only along orthogonal directions), then you are not actually using Pythagorean theorem (or using it in such a degenerate form as to make it almost unrecognizable). – DaG Feb 12 '11 at 13:53
In fact this goes by the name "taxicab metric" because of the analogy with streets in a city. – Ross Millikan Feb 12 '11 at 14:58
check new links i added to my question :) – FeRtoll Feb 12 '11 at 20:53
@FeRtoll: I agree with @Mitch's comment to another answer, that you are somewhat mixing things up. You use different rules to define distances in different cases. If you want to use Euclidean distance (that is, the one computed along diagonals, using Pythagorean theorem), perhaps you should do so also when both points are close to the edges. – DaG Feb 13 '11 at 11:17
@FeRtoll: To see it another way: consider your image in this page. Imagine you shift everything one place to the right (so now both points are close to the left edge); then shift both 5 places up (so they are near the bottom left corner). So now their distance is sq.root(12^2+4^2) (or something). Is this what you want? – DaG Feb 13 '11 at 11:25