I don't think this exact question has been asked already (I've done a lot of searching but come up empty).. I am trying to work out what would be described as the 'L0 Tile' distance between two points in my game in tiles.
My coordinate system is as below:

And this is the effect I'm looking for when calculating distance from a central point:

The problem is I keep running into boundary conditions when trying to come up with a better solution than just 'walking' between the two positions and counting the steps.
The code I have at the moment only works if the direction between the two points is perfectly vertical, horizontal or diagonal:
if( fIsVertical )
{
// Vertical distance calculation
nDistance = nYDifferenceTiles / 2;
}
else if( fIsHorizontal )
{
// Horizontal distance calculation
nDistance = nXDifferenceTiles;
}
else
{
// Diagonal distance calculation (slightly more headache-prone)
bool fIsPerfectDiagonal = false;
if( nYDifferenceTiles % 2 == 0 )
{
// Irrespective of W or E path these values are always the same
if( nXDifferenceTiles * 2 == nYDifferenceTiles )
{
fIsPerfectDiagonal = true;
}
}
else
{
// Values for X when Y is odd are slightly different..
if( fNorthWestOrSouthWest )
{
// Handle the West paths
if( nXDifferenceTiles == ( nYDifferenceTiles - std::floor( nYDifferenceTiles / 2.0 ) ) )
{
fIsPerfectDiagonal = true;
}
}
else
{
// Handle the East paths
if( nXDifferenceTiles == ( nYDifferenceTiles - std::ceil( nYDifferenceTiles / 2.0 ) ) )
{
fIsPerfectDiagonal = true;
}
}
}
// Now we've calculated whether we are on a perfect diagonal
// line from the origin we can decide how to calculate the distance
if( fIsPerfectDiagonal )
{
// If exact diagonal this works well
nDistance = std::max< int >( nYDifferenceTiles, nXDifferenceTiles );
}
else
{
// ? - problem
}
}
So in the diagram below, the green tiles are correctly calculated but for the red ones I have to fall back to my quick and nasty code to walk the distance.

Despite trying a good few things I'm still no closer to having a simple and quick way of calculating the distance. I'm sure this problem has to have been addressed before and that I'm missing something probably obvious. Please help! :)