I am writing a game tool that involves calculating the distance between two coordinates on a toroidal plane 500 units across. That is, [0,0] by [499,499] are real coordinates, and [0,0] and [499,499] are also next to each other.
Currently, in my application, I am comparing the distance between the city with the location [X, Y] corresponding to the user's own location [X, Y], which they previously configured.
To do this, I found this algorithm, which works:
Math.sqrt ( dx * dx + dy * dy );
Since sorting a computed list by distance is a useful thing I can do, I applied this algorithm in a MySQL query and made it available for my application using the following part of the SELECT statement:
SQRT( POW( ( ".strval($sourceX)." - cityX ) , 2 ) + POW( ( ".strval($sourceY)." - cityY ) , 2 ) ) AS distance
This is great for many calculations, but does not take into account the fact that [0,0] and [499,499] the kitten is angle to each other.
Is there any way to tune this algorithm to create an exact distance, given that 0 and 499 are adjacent?