Home | History | Annotate | Download | only in gtx
      1 ///////////////////////////////////////////////////////////////////////////////////////////////////
      2 // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
      3 ///////////////////////////////////////////////////////////////////////////////////////////////////
      4 // Created : 2005-12-30
      5 // Updated : 2008-10-05
      6 // Licence : This source is under MIT License
      7 // File    : glm/gtx/closest_point.inl
      8 ///////////////////////////////////////////////////////////////////////////////////////////////////
      9 
     10 #ifndef glm_gtx_closest_point
     11 #define glm_gtx_closest_point
     12 
     13 namespace glm
     14 {
     15 	template <typename T, precision P>
     16 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> closestPointOnLine
     17 	(
     18 		detail::tvec3<T, P> const & point,
     19 		detail::tvec3<T, P> const & a,
     20 		detail::tvec3<T, P> const & b
     21 	)
     22 	{
     23 		T LineLength = distance(a, b);
     24 		detail::tvec3<T, P> Vector = point - a;
     25 		detail::tvec3<T, P> LineDirection = (b - a) / LineLength;
     26 
     27 		// Project Vector to LineDirection to get the distance of point from a
     28 		T Distance = dot(Vector, LineDirection);
     29 
     30 		if(Distance <= T(0)) return a;
     31 		if(Distance >= LineLength) return b;
     32 		return a + LineDirection * Distance;
     33 	}
     34 }//namespace glm
     35 
     36 #endif//glm_gtx_closest_point
     37