lxgui
utils_maths.cpp
1 #include "lxgui/utils_maths.hpp"
2 
3 #include <algorithm>
4 #include <cmath>
5 
6 namespace lxgui::utils {
7 
8 float round(float value, float unit, rounding_method method) {
9  switch (method) {
10  case rounding_method::nearest: return std::round(value / unit) * unit;
12  if (value > 0.0f)
13  return std::max(1.0f, std::round(value / unit) * unit);
14  else if (value < 0.0f)
15  return std::min(-1.0f, std::round(value / unit) * unit);
16  else
17  return 0.0f;
18  case rounding_method::up: return std::ceil(value / unit) * unit;
19  case rounding_method::down: return std::floor(value / unit) * unit;
20  default: return std::round(value / unit) * unit;
21  }
22 }
23 
24 } // namespace lxgui::utils
float round(float value, float unit, rounding_method method)
Round a floating point value to a specific unit and using a specific rounding method.
Definition: utils_maths.cpp:8
rounding_method
Rounding method for points to pixels conversions.
Definition: utils_maths.hpp:10
@ up
Equivalent to ceil()
@ down
Equivalent to floor()
@ nearest
Equivalent to round()
@ nearest_not_zero
Equivalent to round() but only returns 0 if input is exactly 0.