X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGVec2.hxx;h=65c802ea3912ac5af7b18e7b7fdf332768b12f66;hb=6a7c2000027cd22eea603e936ddbad1a5bfc8b04;hp=5024e2c19e56c00fe8d99fec47a160359886d14b;hpb=9cbbe5559844317f44744788ddb308101a1e75e9;p=simgear.git diff --git a/simgear/math/SGVec2.hxx b/simgear/math/SGVec2.hxx index 5024e2c1..65c802ea 100644 --- a/simgear/math/SGVec2.hxx +++ b/simgear/math/SGVec2.hxx @@ -244,7 +244,12 @@ template inline SGVec2 normalize(const SGVec2& v) -{ return (1/norm(v))*v; } +{ + T normv = norm(v); + if (normv <= SGLimits::min()) + return SGVec2::zeros(); + return (1/normv)*v; +} /// Return true if exactly the same template @@ -331,6 +336,18 @@ T distSqr(const SGVec2& v1, const SGVec2& v2) { SGVec2 tmp = v1 - v2; return dot(tmp, tmp); } +// calculate the projection of u along the direction of d. +template +inline +SGVec2 +projection(const SGVec2& u, const SGVec2& d) +{ + T denom = dot(d, d); + T ud = dot(u, d); + if (SGLimits::min() < denom) return u; + else return d * (dot(u, d) / denom); +} + #ifndef NDEBUG template inline