Browse Source

Add Round To Nearest Multiple

TrevorCash 6 years ago
parent
commit
b4df74097d
1 changed files with 12 additions and 0 deletions
  1. 12 0
      Source/Urho3D/Math/MathDefs.h

+ 12 - 0
Source/Urho3D/Math/MathDefs.h

@@ -169,6 +169,18 @@ template <class T> inline T Round(T x) { return round(x); }
 /// Round value to nearest integer.
 template <class T> inline int RoundToInt(T x) { return static_cast<int>(round(x)); }
 
+/// Round value to nearest multiple. 
+template <class T> inline T RoundToNearestMultiple(T x, T multiple)
+{
+    T mag = Abs(x);
+    multiple = Abs(multiple);
+    T remainder = Mod(mag, multiple);
+    if (remainder >= multiple / 2) 
+        return (FloorToInt<T>(mag / multiple) * multiple + multiple)*Sign(x);
+    else
+        return (FloorToInt<T>(mag / multiple) * multiple)*Sign(x);
+}
+
 /// Round value up.
 template <class T> inline T Ceil(T x) { return ceil(x); }