Collection of DPF-based plugins for packaging
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.

47 lines
1.1KB

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