Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest distance between points on a toroidally wrapped (x- and y- wrapping) map?

I have a toroidal-ish Euclidean-ish map. That is the surface is a flat, Euclidean rectangle, but when a point moves to the right boundary, it will appear at the left boundary (at the same y value), given by x_new = x_old % width

Basically, points are plotted based on: * see edit

(x_new, y_new) = ( x_old % width, y_old % height)

Think Pac Man -- walking off one edge of the screen will make you appear on the opposite edge.

What's the best way to calculate the shortest distance between two points? The typical implementation suggests a large distance for points on opposite corners of the map, when in reality, the real wrapped distance is very close.

The best way I can think of is calculating Classical Delta X and Wrapped Delta X, and Classical Delta Y and Wrapped Delta Y, and using the lower of each pair in the Sqrt(x^2+y^2) distance formula.

But that would involve many checks, calculations, operations -- some that I feel might be unnecessary.

Is there a better way?


edit

When an object moves, it moves to position (x_old,y_old), runs it through the above formula, and stores (x_new, y_new) as its position. The above formula was only added to clarify what happens when objects move across the boundary; in reality, only one (x,y) pair is stored in each object at a time.

like image 606
Justin L. Avatar asked Jun 14 '10 22:06

Justin L.


People also ask

What is the shortest distance between two points on a map?

In practical terms, a great circle drawn on the surface of the Earth between 2 points will be the shortest distance between those points (the Earth is not, of course, a perfect sphere, but it is close enough for this to be of use). Navigational charts are traditionally drawn on the Mercator projection.

How do you find the shortest distance between points?

The shortest distance between two points is a straight line. This distance can be calculated by using the distance formula. The distance between two points ( x 1 , y 1 ) and ( x 2 , y 2 ) can be defined as d = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 .

How do you calculate wrap around?

To calculate a wrap rate, divide the fully loaded rate by your base hourly labor rate. Typically, a competitive wrap rate will be somewhere between “1” and “2.”


1 Answers

The best way I can think of is calculating Classical Delta X and Wrapped Delta X, and Classical Delta Y and Wrapped Delta Y, and using the lower of each pair in the Sqrt(x^2+y^2) distance formula.

That's it, I don't think there is any quicker way. But it's not too hard of a computation; you could do something like

dx = abs(x1 - x2);
if (dx > width/2)
  dx = width - dx;
// again with x -> y and width -> height

(I trust you can translate that into your preferred language)

like image 58
David Z Avatar answered Sep 29 '22 13:09

David Z