How is `clamp()` for 3D vectors implemented?
leloykun opened this issue · comments
Franz Louis Cesista commented
How is `clamp()` for 3D vectors implemented?
Nikolay Vabishchevich commented
That's variant from my working code:
inline Vec3D clamp(const Vec3D &v, double lim)
{
double v2 = v.sqr(); return v2 < lim * lim ? v : v * (lim / std::sqrt(v2));
}
Franz Louis Cesista commented
Thanks @MrSmile!
correct me if I'm wrong but is clamp()
for doubles implemented as:
inline double clamp(double v, double minv, double maxv) {
return std::min(std::max(v, minv), maxv);
}
?
Nikolay Vabishchevich commented
Yes, that looks right.