Function nearestInternal

If p is inside of rect, a copy of p is returned. If p is outside of rect, a point is returned closest to p on the edge of the rectangle.