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


I have a toroidal-isch euclidean-ish map. This surface is a flat, Euclidean rectangle, but when a point runs to the right extent, it will appear on the left hand side (same Y value) given by x_new = x_old% width

Basically, Points are plotted on: * edit

  (x_new, y_new) = (x_old% width, y_old% height)  
Pac Man - Walking from the edge of the screen, you will see in the opposite direction.

What is the best way to calculate the smallest distance between the two points? Normal implementation suggests a large distance for points on opposite sides of the map, when in reality, the actual wrapped distance is very close.

The best way I can think of is the classical Delta X and Raped Delta X, and the classical Delta Y and the wrapped Delta Y, and Sqrt (x ^ 2 + y ^ 2) in the distance formula, each pair of lower Using part.

But this will include many investigations, calculations, operations - something that I think can be unnecessary.

Is there any better way?


edit

Object moves, this position runs on (x_old, y_old), it runs through the above formula, And stores (x_new, y_new) run as its status. The above formula was only added to clarify what happens when the boundaries cross the border; In reality, only one (X, Y) pair is stored in each object at one time.

The best method I can think of is classical Delta X and Raped Delta X, and Calculating classical Delta Y and Raped Delta Y, and Sqrt (x ^ 2 + y ^ 2) is using the bottom of each pair in the distance formula.

That's it, I do not think there is any faster way. But this is not very difficult to calculate; You can do something like

  dx = abs (x1 - x2); If (dx> width / 2) dx = width - dx; // again x - & gt; Y and width - & gt; Height  

(I believe you can translate it into your preferred language)


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -