Taxicab distance



Taxicab distance, also known as rectilinear distance, city block distance, Manhattan distance, and other names, is a metric in taxicab geometry for measuring distance, as an alternative to Euclidean distance (straight-line, or "as the crow flies"). Minecraft uses taxicab distance as an efficient range measurement in several elements of the game.

Calculation
In Minecraft, the distance between two points in three-dimensional space can be measured in taxicab or Euclidean distance, depending on the situation.

Given two points in 3-D space $$(x_1, y_1, z_1)$$ and $$(x_2, y_2, z_2)$$, the straight-line distance (Euclidean distance) between them is:
 * $$d = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2 + (z_1 - z_2)^2}$$

The square root operation, if it needs to be done many times during a game tick, can be processor intensive, particularly on mobile devices that may have slow processors. The taxicab distance is a simplified calculation that eliminates the square root by simply calculating the absolute value (positive value) of the difference between each coordinate value:
 * $$d = |x_1 - x_2| + |y_1 - y_2| + |z_1 - z_2|$$

A consequence of this calculation is that a taxicab "circle" drawn with a constant taxicab radius appears as a diamond, in which the taxicab radius equals the euclidean radius only the cardinal directions (north, south, east, west), and the radius is shorter in all other directions. Similarly, in three dimensions, a taxicab "sphere" appears as an octahedron.

Taxicab
Minecraft makes use of taxicab distance in various situations:
 * Light from light sources such as torches, campfires, lanterns, glowstone, etc. illuminate surfaces according to taxicab distance.
 * Torches also also melt snow layers within 2 blocks and ice within 3 blocks taxicab distance.
 * Heat blocks melt snow layers and ice within 2 blocks taxicab distance, but do not emit light.
 * Liquid (water or lava) flowing horizontally on a flat surface flows from the source in taxicab distance. For example, if flowing water is forced into a 1-block-wide stream and turns corners, it travels 7 blocks. If it is allowed to flow freely on a surface, it also travels 7 blocks taxicab distance.
 * Simulation distance, the distance from the player at which blocks get tick updates (such as lightning strikes), and beyond which mobs despawn, uses taxicab distance.
 * A sponge absorbs both flowing and source blocks of water up to 7 blocks away in taxicab distance.
 * Phantoms spawn 20–34 blocks above the player, and off to the side by a taxicab distance of up to 10 blocks.
 * If bone meal is used on a grass block, then grass, tall grass, or flowers form on random grass blocks up to 7 taxicab blocks away. Similarly, using bone meal on sea pickles that are on coral blocks creates more sea pickles on nearby coral blocks out to a horizontal taxicab distance of 2.
 * A block of copper is oxidized according to a taxicab distance search for unwaxed copper blocks within 4 blocks of the block being considered for oxidation.

Euclidean
Situations in which Euclidean distance is used:
 * Nether portal searches for destination portals
 * The command
 * Despawning range in Java Edition
 * Lightning rods redirect lightning within a Euclidean spherical volume centered on the rod, having a radius of 128 blocks in Java Edition and 64 blocks in Bedrock Edition.