Counting Steps Between Isopod Shelters
The damp end of the enclosure has a curved piece of bark; the drier end has a small cork shelter. I sketch both on graph paper, keeping the moisture gradient visible in the margin. The drawing is tidy enough to ask a useful question: how far apart are the shelters in grid steps?
A ruler would give me the straight-line distance. For a map where I want to count horizontal and vertical moves, Manhattan distance is more convenient: take the absolute difference between the column numbers, then add the absolute difference between the row numbers. The name evokes a city grid, though the calculation is much older than the computers that made it a familiar programming tool.
Here are the shelters at (2, 1) and (7, 4). Go spells out the absolute-value operation for integers:
package main
import "fmt"
func abs(n int) int {
if n < 0 { return -n }
return n
}
func main() {
x1, y1, x2, y2 := 2, 1, 7, 4
fmt.Println(abs(x2-x1) + abs(y2-y1))
}
Lua has the operation in its maths library, so the same calculation fits on fewer lines:
local x1, y1 = 2, 1
local x2, y2 = 7, 4
local horizontal = math.abs(x2 - x1)
local vertical = math.abs(y2 - y1)
local distance = horizontal + vertical
print(distance)
Both programs print 8: five column steps plus three row steps. If each square represents one centimetre, that is eight centimetres along a shortest grid route. Plenty of such routes exist; taking the horizontal steps first is just one of them.
The number belongs to the map, not to an individual isopod’s travel diary. A real animal can move diagonally, pause, or take a detour around a piece of leaf litter. Manhattan distance ignores those details, which makes it quick to calculate and easy to compare across observations. I can keep the grid distance beside each shelter pair and use a separate note for what I actually saw.