views:

786

answers:

4

i would like to calculate the distance between two x/y coordinates. This calculation itself isn't that difficult, however I need the corners and sides of the grid to be 'connected'. This means on a grid of 500x500 that the point after 499 becomes 0 again. The distance between e.g. 0,0 and 0,495 should be 5 then.

Is there any good mathematical way of calculating this?

+6  A: 
  • If/while the distance between x coordinates is larger than half of the grid X size, add grid X size to the smaller x coordinate.
  • Do the same for Y.
  • Then calculate the distance.
Bandi-T
Good explanation of the facts :)
Ropstah
+15  A: 

So you are looking for the Euclidean distance on the two-dimensional surface of a torus, I gather.

sqrt(min(|x1 - x2|, w - |x1 - x2|)^2 + min(|y1 - y2|, h - |y1-y2|)^2)

where w and h are the width (x) and height (y) of the grid, respectively.

ezod
+1, I think your answer is more mathematical than mine :)
Yoni
Elegant! Mathematical! Upvoted!
Bandi-T
Very nice solution.
Stephen Canon
great answer, thanks!
Ropstah
Does this calculation imply that the distance between 0,0 and 0,1 is smaller than the distance between 0,0 and 1,1?
Ropstah
@ropstah: I would certainly hope so. :)
ezod
@ropstah : test for yourself !
Valentin Rocher
@Bishiboosh: I'd like to talk in concepts before starting with numbers... There might be another solution which more appropriate in my case. Anyway, this solution seems quite appropriate :)
Ropstah
Is it so that the distances between two points on the inside of center cirlce are closer to each other than two points on the outside are?
Ropstah
@ropstah: I'm not sure what you mean. If you're referring to the shape of the "torus", don't get hung up on what a torus looks like in Euclidean 3-space; we're just talking about a Euclidean plane whose edges are connected, Pac-Man style, and all distances are as you would expect.
ezod
i've thought about the visual representation of not being 'true' in 3d space. Thanks for explaining!
Ropstah
+1  A: 

for points (x1,y1) and (x2,y2), you need to calculate 4 distances:

  • from (x1,y1) to (x2,y2)
  • from (x1,y1) to (x2, 500-y2)
  • from (x1,y1) to (500-x2, y2)
  • from (x1,y1) to (500-x2, 500-y2)

and then take the minimum of those.

Yoni
This doesn't work: try it with (0,200) and (0,300). That gives a distance of 0 with the second formula!Also, as is stated (or at least implied) in other responses, you can work out the shortest x and shortest y distances first; then you only need to apply the Pythagorean theorem once.
BobS
thanks, I stand corrected
Yoni
+3  A: 

If your grid wraps around at the edges, there will be four distances between each coordinate (for 2 dimensions). I'm assuming you want to know the shortest distance.

Let's use a smaller grid, the numbers are a bit more manageable. Say the grid is 10x10. Let's also use just one dimension for simplicity (in which case there'll be just two distances), just as you have in your example. Say we have the points 0,2 and 0,6. The two distances between the points are d_1 = (6-2) = 4 and d_2 = (10-6) + 2 = 6, so in this case the shortest distance would be d_1.

In general, you can do the following:

  • For each coordinate:
    • subtract the smaller from the larger number
    • if the result is greater than half the width of the grid the shortest distance in this coordinate is the grid width minus the result
    • if the result is less than half the width of the grid, the shortest distance in this coordinate is the result

Then using Pythagoras' theorem, the shortest distance between the two points is the square root of the sum of the squares of the shortest distances in each direction. You can calculate the other three distances by calculating Pythagoras' theorem using the other combinations of distances in each direction.

As another poster has said, the shape formed when you wrap round at the edges (for a 2 dimensional grid) is a torus and I think the method I've used above is the same as the equation given but has the advantage that it can be extended to n-dimensions if required. Unfortunately there's not really an easy visualisation above 2 dimensions.

tobyclemson
Upvoted for detailed explanation.
Bandi-T