docs: def _reorder_lines(lines):
      """
      Reorder lines so that the distance from the end of one to the beginning of
      the next is minimised.
      """
  
      x = 0
      y = 0
      new_lines = []
  
      # treat the list of lines as a stack, off which we keep popping the best
      # one to add next.
      while lines:
          # looping over all the lines like this isn't terribly efficient, but
          # in local tests seems to handle a few thousand lines without a
          # problem.
          min_dist = None
          min_i = None
          for i, line in enumerate(lines):
              moveto, _, _ = line
  
              dist = abs(moveto.x - x) + abs(moveto.y - y)
              if min_dist is None or dist < min_dist:
                  min_dist = dist
                  min_i = i
  
          assert min_i is not None
          line = lines.pop(min_i)
          _, endsat, _ = line
          x = endsat.x
          y = endsat.y
          new_lines.append(line)
  
      return new_lines