Manhattan versus Euclidian distance
When I was testing my new code for Simutrans-Experimental to calculate the average speed, I found that the average speed given was far too high (often in excess of the vehicle's maximum speed) where vehicles travelled a significant distance on the diagonal. I realised that this must be because the number of steps per tile for diagonal tiles has recently been reduced from 255 to 127, but the Manhattan distance is still used. This would give results that correlated correctly with the speed calculations when the diagonals were 255 per tile, but not with the new system where diagonals are 127 per tile. So, I wrote a new method in koord.h thus:
tatic inline double accurate_distance(const koord &a, const koord &b)
{
// Euclidian distance
return sqrt(pow((double)a.x - (int)b.x, 2) + pow((double)a.y - (int)b.y, 2));
}
and used that instead for my speed calculations. It gave an accurate result: the average speed of a train with a maximum speed of 300kph was 222kph, not 315kph. That made me wonder whether there are other places in the code where the Manhattan distance is being used where it would, now that the conversion to 127 step diagonal tiles has been made, be better to use the Euclidian distance. Would it be better to use it, for example, for checking the distance for the revenue calculation? I appreciate that it might be slightly slower than the Manhattan distance to calculate, but the difference is not likely to be very great, and, in any event, the method is not, I do not think, called often enough for it to make a great difference to the overall speed of the game. Are there any potential problems with replacing calls to abs_distance with calls to accurate_distance? Would it cause problems elsewhere?