From ad8509243347e8e3abce1031cfaade2f382599c9 Mon Sep 17 00:00:00 2001 From: SiliconKiwi Date: Tue, 16 Sep 2014 09:09:39 +1200 Subject: [PATCH 1/2] Update closest_point.hpp Algorithm works fine with 2d points as well... --- glm/gtx/closest_point.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/glm/gtx/closest_point.hpp b/glm/gtx/closest_point.hpp index d0f17155..eb893291 100644 --- a/glm/gtx/closest_point.hpp +++ b/glm/gtx/closest_point.hpp @@ -56,6 +56,13 @@ namespace glm detail::tvec3 const & point, detail::tvec3 const & a, detail::tvec3 const & b); + + /// 2d lines work as well + template + GLM_FUNC_DECL detail::tvec2 closestPointOnLine( + detail::tvec2 const & point, + detail::tvec2 const & a, + detail::tvec2 const & b); /// @} }// namespace glm From b5409fb728867427a385185800fc9fc9d8b7a651 Mon Sep 17 00:00:00 2001 From: SiliconKiwi Date: Tue, 16 Sep 2014 09:11:34 +1200 Subject: [PATCH 2/2] Update closest_point.inl --- glm/gtx/closest_point.inl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/glm/gtx/closest_point.inl b/glm/gtx/closest_point.inl index b13370b2..889b75b1 100644 --- a/glm/gtx/closest_point.inl +++ b/glm/gtx/closest_point.inl @@ -28,4 +28,25 @@ namespace glm if(Distance >= LineLength) return b; return a + LineDirection * Distance; } + + template + GLM_FUNC_QUALIFIER detail::tvec2 closestPointOnLine + ( + detail::tvec2 const & point, + detail::tvec2 const & a, + detail::tvec2 const & b + ) + { + T LineLength = distance(a, b); + detail::tvec2 Vector = point - a; + detail::tvec2 LineDirection = (b - a) / LineLength; + + // Project Vector to LineDirection to get the distance of point from a + T Distance = dot(Vector, LineDirection); + + if(Distance <= T(0)) return a; + if(Distance >= LineLength) return b; + return a + LineDirection * Distance; + } + }//namespace glm