math_util.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. static constexpr float PI = 3.14159265f;
  10. inline bool IntervalsIntersect(unsigned start0, unsigned length0, unsigned start1,
  11. unsigned length1) {
  12. return (std::max(start0, start1) < std::min(start0 + length0, start1 + length1));
  13. }
  14. template <class T>
  15. struct Rectangle {
  16. T left;
  17. T top;
  18. T right;
  19. T bottom;
  20. Rectangle() {}
  21. Rectangle(T left, T top, T right, T bottom)
  22. : left(left), top(top), right(right), bottom(bottom) {}
  23. T GetWidth() const {
  24. return std::abs(static_cast<typename std::make_signed<T>::type>(right - left));
  25. }
  26. T GetHeight() const {
  27. return std::abs(static_cast<typename std::make_signed<T>::type>(bottom - top));
  28. }
  29. Rectangle<T> TranslateX(const T x) const {
  30. return Rectangle{left + x, top, right + x, bottom};
  31. }
  32. Rectangle<T> TranslateY(const T y) const {
  33. return Rectangle{left, top + y, right, bottom + y};
  34. }
  35. Rectangle<T> Scale(const float s) const {
  36. return Rectangle{left, top, static_cast<T>(left + GetWidth() * s),
  37. static_cast<T>(top + GetHeight() * s)};
  38. }
  39. };
  40. } // namespace MathUtil