math_util.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start1, unsigned length1) {
  11. return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1));
  12. }
  13. template<typename T>
  14. inline T Clamp(const T val, const T& min, const T& max)
  15. {
  16. return std::max(min, std::min(max, val));
  17. }
  18. template<class T>
  19. struct Rectangle
  20. {
  21. T left;
  22. T top;
  23. T right;
  24. T bottom;
  25. Rectangle() {}
  26. Rectangle(T left, T top, T right, T bottom) : left(left), top(top), right(right), bottom(bottom) {}
  27. T GetWidth() const { return std::abs(static_cast<typename std::make_signed<T>::type>(right - left)); }
  28. T GetHeight() const { return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top)); }
  29. };
  30. } // namespace MathUtil