import math


def distance(line, point):

    """Returns the closest point and distance from a point to a line segment.
    """

    (cx, cy), ((ax, ay), (bx, by)) = point, line
    r_num = (cx - ax) * (bx - ax) + (cy - ay) * (by - ay)
    r_den = (bx - ax) * (bx - ax) + (by - ay) * (by - ay)

    if r_den > 0.0:

        r = r_num / r_den
        px, py = ax + r * (bx - ax), ay + r * (by - ay)
        s = ((ay - cy) * (bx - ax) - (ax - cx) * (by - ay)) / r_den

        xx = px
        yy = py

        if (r >= 0) and (r <= 1):
            dseg = abs(s) * math.sqrt(r_den)

        else:
            dist1 = (cx - ax) * (cx - ax) + (cy - ay) * (cy - ay)
            dist2 = (cx - bx) * (cx - bx) + (cy - by) * (cy - by)

            if dist1 < dist2:
                xx = ax
                yy = ay
                dseg = math.sqrt(dist1)
            else:
                xx = bx
                yy = by
                dseg = math.sqrt(dist2)

        return (xx, yy), dseg

    else:
        return point, float('inf')