def shortest_torus_path_length(source, destination, width, height):
    """Get the length of a shortest path from source to destination using
    wrap-around links.

    See http://jhnet.co.uk/articles/torus_paths for an explanation of how this
    method works.

    Parameters
    ----------
    source : (x, y, z)
    destination : (x, y, z)
    width : int
    height : int

    Returns
    -------
    int
    """
    # Aliases for convenience
    w, h = width, height

    # Get (non-wrapping) x, y vector from source to destination as if the
    # source was at (0, 0).
    x, y, z = (d - s for s, d in zip(source, destination))
    x, y = x - z, y - z
    x %= w
    y %= h

    return min(max(x, y),          # No wrap
               w - x + y,          # Wrap X only
               x + h - y,          # Wrap Y only
               max(w - x, h - y))