math_util.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 MathUtil {
  8. constexpr float PI = 3.14159265f;
  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. T GetWidth() const {
  19. return std::abs(static_cast<std::make_signed_t<T>>(right - left));
  20. }
  21. T GetHeight() const {
  22. return std::abs(static_cast<std::make_signed_t<T>>(bottom - top));
  23. }
  24. Rectangle<T> TranslateX(const T x) const {
  25. return Rectangle{left + x, top, right + x, bottom};
  26. }
  27. Rectangle<T> TranslateY(const T y) const {
  28. return Rectangle{left, top + y, right, bottom + y};
  29. }
  30. Rectangle<T> Scale(const float s) const {
  31. return Rectangle{left, top, static_cast<T>(left + GetWidth() * s),
  32. static_cast<T>(top + GetHeight() * s)};
  33. }
  34. };
  35. } // namespace MathUtil