def pt_segment_dist(A, B, P):
    """
    SUMMARY
        computes the distance of point P from segment AB
    PARAMETERS
        A: endpoint of segment AB
        B: other endpoint of segment AB
        P: point at some distance from AB

    RETURNS
        float
    """
    x1, y1 = A
    x2, y2 = B
    x3, y3 = P
    px = x2-x1
    py = y2-y1
    norm = px*px + py*py
    u =  ((x3 - x1) * px + (y3 - y1) * py) / float(norm)

    if u > 1:
        u = 1
    elif u < 0:
        u = 0

    x = x1 + u * px
    y = y1 + u * py
    dx = x - x3
    dy = y - y3

    dist = (dx*dx + dy*dy)**.5
    return dist