Results 1 to 2 of 2

Thread: Calculate distance on a wrapped map

  1. #1
    Mar 2009

    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.
    Last edited by phungus420; Mar 12th 2009 at 06:31 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Mar 2009
    For anyone interested this is how it was solved:

                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.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 9
    Last Post: Feb 1st 2011, 02:23 AM
  2. Replies: 1
    Last Post: Aug 9th 2010, 04:03 PM
  3. Calculate acceleration with time and distance?
    Posted in the Math Topics Forum
    Replies: 3
    Last Post: Feb 23rd 2010, 11:51 AM
  4. Replies: 1
    Last Post: Sep 15th 2009, 03:11 PM
  5. Calculate the distance..
    Posted in the Algebra Forum
    Replies: 4
    Last Post: Oct 16th 2008, 01:11 PM

Search Tags

/mathhelpforum @mathhelpforum