def point_line_distance(P, A, B):
    """
    Given points P, A, and B in R^2, find the distance between P and the
    line through A and B.
    """

    #If the line through A and B is defined by ax + by + c = 0,
    #then the distance is given by |a*Px + b*Py + c|/sqrt(a^2 + b^2)
    dy = B[1] - A[1]
    dx = B[0] - A[0]
##    a = dy
##    b = -dx
##    c = -(a*A[0] - b*A[1])
##
##    return abs(a*P[0] + b*P[1] + c)/sqrt(a**2 + b**2)
    y = P[1] - A[1]
    x = P[0] - A[0]
    num = float(x * dx + y * dy)
    denom = dx ** 2 + dy ** 2
    frac = num/denom
    perp_vector = (x - frac * dx, y - frac * dy)
    return (perp_vector[0] ** 2 + perp_vector[1] ** 2) ** 0.5