MailRuChamps / raic-2018

Russian AI Cup — artificial intelligence programming contest. Official website: http://russianaicup.ru
43 stars 32 forks source link

How is `clamp()` for 3D vectors implemented? #103

Closed leloykun closed 5 years ago

MrSmile commented 5 years ago

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));
}
leloykun commented 5 years ago

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);
}

?

MrSmile commented 5 years ago

Yes, that looks right.