math_util.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <cstdlib>
  6. #include <type_traits>
  7. namespace Common {
  8. constexpr float PI = 3.1415926535f;
  9. template <class T>
  10. struct Rectangle {
  11. T left{};
  12. T top{};
  13. T right{};
  14. T bottom{};
  15. constexpr Rectangle() = default;
  16. constexpr Rectangle(T left_, T top_, T right_, T bottom_)
  17. : left(left_), top(top_), right(right_), bottom(bottom_) {}
  18. [[nodiscard]] T GetWidth() const {
  19. if constexpr (std::is_floating_point_v<T>) {
  20. return std::abs(right - left);
  21. } else {
  22. return static_cast<T>(std::abs(static_cast<std::make_signed_t<T>>(right - left)));
  23. }
  24. }
  25. [[nodiscard]] T GetHeight() const {
  26. if constexpr (std::is_floating_point_v<T>) {
  27. return std::abs(bottom - top);
  28. } else {
  29. return static_cast<T>(std::abs(static_cast<std::make_signed_t<T>>(bottom - top)));
  30. }
  31. }
  32. [[nodiscard]] Rectangle<T> TranslateX(const T x) const {
  33. return Rectangle{left + x, top, right + x, bottom};
  34. }
  35. [[nodiscard]] Rectangle<T> TranslateY(const T y) const {
  36. return Rectangle{left, top + y, right, bottom + y};
  37. }
  38. [[nodiscard]] Rectangle<T> Scale(const float s) const {
  39. return Rectangle{left, top, static_cast<T>(left + GetWidth() * s),
  40. static_cast<T>(top + GetHeight() * s)};
  41. }
  42. };
  43. template <typename T>
  44. Rectangle(T, T, T, T) -> Rectangle<T>;
  45. } // namespace Common