Find the closest point on a line. This point will be reproducible by a Hue
    lamp.

def get_closest_point_to_line(A, B, P):
    '''
    Find the closest point on a line. This point will be reproducible by a Hue
    lamp.
    '''
    AP = XYPoint(P.x - A.x, P.y - A.y)
    AB = XYPoint(B.x - A.x, B.y - A.y)
    ab2 = AB.x * AB.x + AB.y * AB.y
    ap_ab = AP.x * AB.x + AP.y * AB.y
    t = ap_ab / ab2

    if t < 0.0:
        t = 0.0
    elif t > 1.0:
        t = 1.0

    return XYPoint(A.x + AB.x * t, A.y + AB.y * t)