math_util.h 847 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <type_traits>
  8. namespace MathUtil
  9. {
  10. template<typename T>
  11. inline T Clamp(const T val, const T& min, const T& max)
  12. {
  13. return std::max(min, std::min(max, val));
  14. }
  15. template<class T>
  16. struct Rectangle
  17. {
  18. T left;
  19. T top;
  20. T right;
  21. T bottom;
  22. Rectangle() {}
  23. Rectangle(T left, T top, T right, T bottom) : left(left), top(top), right(right), bottom(bottom) {}
  24. T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); }
  25. T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); }
  26. };
  27. } // namespace MathUtil