1/// @ref gtx_closest_point 2/// @file glm/gtx/closest_point.inl 3 4namespace glm 5{ 6 template<typename T, qualifier Q> 7 GLM_FUNC_QUALIFIER vec<3, T, Q> closestPointOnLine 8 ( 9 vec<3, T, Q> const& point, 10 vec<3, T, Q> const& a, 11 vec<3, T, Q> const& b 12 ) 13 { 14 T LineLength = distance(a, b); 15 vec<3, T, Q> Vector = point - a; 16 vec<3, T, Q> 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, qualifier Q> 27 GLM_FUNC_QUALIFIER vec<2, T, Q> closestPointOnLine 28 ( 29 vec<2, T, Q> const& point, 30 vec<2, T, Q> const& a, 31 vec<2, T, Q> const& b 32 ) 33 { 34 T LineLength = distance(a, b); 35 vec<2, T, Q> Vector = point - a; 36 vec<2, T, Q> 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