X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGVec2.hxx;h=2a40564145c39e68ad13426a4b556f92a0ad6506;hb=7a52c2fa71355631a559d74e0860fa6c5efa424e;hp=5024e2c19e56c00fe8d99fec47a160359886d14b;hpb=9cbbe5559844317f44744788ddb308101a1e75e9;p=simgear.git diff --git a/simgear/math/SGVec2.hxx b/simgear/math/SGVec2.hxx index 5024e2c1..2a405641 100644 --- a/simgear/math/SGVec2.hxx +++ b/simgear/math/SGVec2.hxx @@ -18,10 +18,6 @@ #ifndef SGVec2_H #define SGVec2_H -#if defined ( __CYGWIN__ ) -#include -#endif - #ifndef NO_OPENSCENEGRAPH_INTERFACE #include #include @@ -244,7 +240,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 +332,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