def intersect_1d(p1, p2, p3, p4):
    """Find the intersection, if any, of two line segments on the same line"""
    e1, e2 = sorted((p1, p2))
    e3, e4 = sorted((p3, p4))

    if e2 >= e3 and e4 >= e1:
        # Segments overlap, so find the interval with the two middle points
        ordered = sorted((e1, e2, e3, e4))
        return range(ordered[1], ordered[2] + 1)
    return []