You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2013 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. #ifndef glm_gtx_closest_point
  10. #define glm_gtx_closest_point
  11. namespace glm
  12. {
  13. template <typename valType>
  14. GLM_FUNC_QUALIFIER detail::tvec3<valType> closestPointOnLine
  15. (
  16. detail::tvec3<valType> const & point,
  17. detail::tvec3<valType> const & a,
  18. detail::tvec3<valType> const & b
  19. )
  20. {
  21. valType LineLength = distance(a, b);
  22. detail::tvec3<valType> Vector = point - a;
  23. detail::tvec3<valType> LineDirection = (b - a) / LineLength;
  24. // Project Vector to LineDirection to get the distance of point from a
  25. valType Distance = dot(Vector, LineDirection);
  26. if(Distance <= valType(0)) return a;
  27. if(Distance >= LineLength) return b;
  28. return a + LineDirection * Distance;
  29. }
  30. }//namespace glm
  31. #endif//glm_gtx_closest_point