# Calculate distance on a wrapped map

• Mar 12th 2009, 05:17 PM
phungus420
Calculate distance on a wrapped map
So I'm writing code that calculates distance between two cities for civilization 4 (modding, it's a hobby of mine). That's simple enough until you try to figure out how to deal with the wrapped map. I've taken alot of math, been 5 years or so but got through Calc III. But I just can't figure this one out. I've tried googling the answer and got nothing.

What would be a good formula for calculating distance on a wrapped map (ie in a standard cartesian plane if X(max) + 1 = X(min) --also X(min) will always be 1, but X(max) will change depending on the size of the map chosen by the player at game start-the calculation is done on a static map though once it's applied)? All x and y values in this plane are positive integers, not that that's probably important. Also the ability of the x and y values to wrap is independent (can be changed on game setup), so it's best to analyize the x and y seperatly and just apply pythagoras later. Though if that's impossible, I could just set up a bunch of if statements in the code.
• Mar 13th 2009, 03:05 AM
phungus420
For anyone interested this is how it was solved:

Code:

```            deltaX = abs(pCity.getX() - capital.getX())             if map.isWrapX():                 deltaX = min(deltaX, map.getGridWidth() - deltaX)             deltaY = abs(pCity.getY() - capital.getY())             if map.isWrapX():                 deltaY = min(deltaY, map.getGridWidth() - deltaY)             cityDistRaw = ( deltaX**2 + deltaY**2 )**0.5```

For those who don't speak code (though this should be pretty easy to decifer anyway with math)

dX(i) = |P(x) - C(x)|

For a wrapped map

dX(f) = X(max) - dX(i)

if dX(f) < dX(i) then dX(f) = dX(i)

Repeat for Y component & then apply pythagoras to deltaX and deltaY.