Sign in to follow this  

distance between two points in a wrapping coordinate system

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='EqualityAssignment' timestamp='1306885318' post='4818084']
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?
[/quote]

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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this