# distance between two points in a wrapping coordinate system

This topic is 2602 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there, I'm running into an issue where I need to calculate the distance between two points in a wrapping coordinate system and I can't quite figure out how to do it. Basically what this means is we have two points, say there are (28, 2) and (50, 92), the world is 96x96 so a point at (96, 96) is equivalent to (0, 0), and I need to find the distance between them. Doing it normally obviously the points are way off since the distance between 2 and 92 is huge though it should only be 6 because of the wrap. Does anyone know how to do this? I've come up with a very crude way of doing it, which is just calculating all of the distances possible and seeing which one is smaller, but I'm hoping for something more elegant.

##### Share on other sites
The surface your describing is a torus (doughnut) (http://en.wikipedia.org/wiki/Torus). The plane is what you get if you lay the doughnut flat on the table, cut it once and unbend it so its a long tube, then slice it laterally but only half way through. It becomes a plane with the wrapping edges where your knife cut.

Basically, to calculate the distance, consider the line segment joining the two points, then extend it to the edge of your plane. The edges of that line meet.

.\........
..X.......
...\......
....\.....
.....X....
......\...

The distance is either the section in the middle, sqrt((x2 - x1)**2 + (y2 - y1)**2) OR the other two sections, which are easily found given the points and the width and height of the grid.

http://salin.org/2009/distance-between-2-points-on-a-torus

http://www.swarm.org/pipermail/modelling/2005-October/003828.html

##### Share on other sites
Sweet thanks. That certainly makes it easier. Though once I find the shortest distance I need to retain the two points that gave that distance. Is there a way to do that? Since I need them for some other calculations.

##### Share on other sites
Maybe I'm misunderstanding, you've got two points on the grid, you calculate the distance between them, and then you have the points and the distance, how is storing them a problem?

##### Share on other sites
If you mean the points where it wraps to the other side of the map, 1 point should be enough...

##### Share on other sites

Maybe I'm misunderstanding, you've got two points on the grid, you calculate the distance between them, and then you have the points and the distance, how is storing them a problem?

Yeah ignore that. I got it working. Thanks for the help

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×