def ProjectPointOntoLine(A, B, P):
    """
    Project a point (in 3D), P, onto a line (in 3D) defined by the two
    points A and B.
    This point Q is the point on line AB such that the line PQ is orthogonal
    to AB (PQ is the shortest line between the point P and the line AB)

    Parameters:
       A - Vector for point A on the line AB
       B - Vector for point B on the line AB
       P - Vector for point P to project onto the line AB

    Return value:
       Vector representing the point Q on line AB such that PQ is the shortest
       line from P to line AB.
    """
    # this basically involves finding the point Q such that
    # (P - Q) * (B - A) = 0
    # where * is dot product.
    # This is done by solving for u after substituting the equation for line AB:
    # Q = A + u(B - A)
    # into the first equation.
    # See  http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/

    # Note * here is just scalar multiplication (float)
    u =((P[0]-A[0])*(B[0]-A[0])+(P[1]-A[1])*(B[1]-A[1])+(P[2]-A[2])*(B[2]-A[2]))\
        / (B - A).normsq()
    Q = A + u*(B - A).get_array() # this * is scalar*vector (Numeric.array *)
    return Q