master
ブランチ master
VulkanExample / dep / include / glm / gtx / closest_point.inl
1.1 KB、45行 生 Blame 履歴
1/// @ref gtx_closest_point
2
3namespace 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
17 // Project Vector to LineDirection to get the distance of point from a
18 T Distance = dot(Vector, LineDirection);
19
20 if(Distance <= T(0)) return a;
21 if(Distance >= LineLength) return b;
22 return a + LineDirection * Distance;
23 }
24
25 template<typename T, qualifier Q>
26 GLM_FUNC_QUALIFIER vec<2, T, Q> closestPointOnLine
27 (
28 vec<2, T, Q> const& point,
29 vec<2, T, Q> const& a,
30 vec<2, T, Q> const& b
31 )
32 {
33 T LineLength = distance(a, b);
34 vec<2, T, Q> Vector = point - a;
35 vec<2, T, Q> LineDirection = (b - a) / LineLength;
36
37 // Project Vector to LineDirection to get the distance of point from a
38 T Distance = dot(Vector, LineDirection);
39
40 if(Distance <= T(0)) return a;
41 if(Distance >= LineLength) return b;
42 return a + LineDirection * Distance;
43 }
44
45}//namespace glm