The first part of this problem is finding the point on the line closest to the given point.
Here P is the point in space and A and B constitute the beginning and end of a line. This func returns that point. This is just to give you some insight b/c my use case was a little odd as you see my point P came from the 2d plane while my lines were in 3d space. How I ever got this figured out is beyond me.
func closestPointOnLineSegment(P : Vector2, A : Vector3, B : Vector3) -> Vector3:
var AB = Vector2(B.x,B.z) - Vector2(A.x,A.z);
var AP = P - Vector2(A.x,A.z);
var lengthSqrAB = AB.x * AB.x + AB.y * AB.y;
var t = (AP.x * AB.x + AP.y * AB.y) / lengthSqrAB;
t = clamp(t,0.0,1.0)
var AB3d = B - A
return A + t * AB3d;