1/// @ref gtx_closest_point 2/// @file glm/gtx/closest_point.inl 3 4namespace glm 5{ 6 template <typename T, precision P> 7 GLM_FUNC_QUALIFIER tvec3<T, P> closestPointOnLine 8 ( 9 tvec3<T, P> const & point, 10 tvec3<T, P> const & a, 11 tvec3<T, P> const & b 12 ) 13 { 14 T LineLength = distance(a, b); 15 tvec3<T, P> Vector = point - a; 16 tvec3<T, P> LineDirection = (b - a) / LineLength; 17 18 // Project Vector to LineDirection to get the distance of point from a 19 T Distance = dot(Vector, LineDirection); 20 21 if(Distance <= T(0)) return a; 22 if(Distance >= LineLength) return b; 23 return a + LineDirection * Distance; 24 } 25 26 template <typename T, precision P> 27 GLM_FUNC_QUALIFIER tvec2<T, P> closestPointOnLine 28 ( 29 tvec2<T, P> const & point, 30 tvec2<T, P> const & a, 31 tvec2<T, P> const & b 32 ) 33 { 34 T LineLength = distance(a, b); 35 tvec2<T, P> Vector = point - a; 36 tvec2<T, P> LineDirection = (b - a) / LineLength; 37 38 // Project Vector to LineDirection to get the distance of point from a 39 T Distance = dot(Vector, LineDirection); 40 41 if(Distance <= T(0)) return a; 42 if(Distance >= LineLength) return b; 43 return a + LineDirection * Distance; 44 } 45 46}//namespace glm 47