point.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <type_traits>
  6. namespace Common {
  7. // Represents a point within a 2D space.
  8. template <typename T>
  9. struct Point {
  10. static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type!");
  11. T x{};
  12. T y{};
  13. #define ARITHMETIC_OP(op, compound_op) \
  14. friend constexpr Point operator op(const Point& lhs, const Point& rhs) noexcept { \
  15. return { \
  16. .x = static_cast<T>(lhs.x op rhs.x), \
  17. .y = static_cast<T>(lhs.y op rhs.y), \
  18. }; \
  19. } \
  20. friend constexpr Point operator op(const Point& lhs, T value) noexcept { \
  21. return { \
  22. .x = static_cast<T>(lhs.x op value), \
  23. .y = static_cast<T>(lhs.y op value), \
  24. }; \
  25. } \
  26. friend constexpr Point operator op(T value, const Point& rhs) noexcept { \
  27. return { \
  28. .x = static_cast<T>(value op rhs.x), \
  29. .y = static_cast<T>(value op rhs.y), \
  30. }; \
  31. } \
  32. friend constexpr Point& operator compound_op(Point& lhs, const Point& rhs) noexcept { \
  33. lhs.x = static_cast<T>(lhs.x op rhs.x); \
  34. lhs.y = static_cast<T>(lhs.y op rhs.y); \
  35. return lhs; \
  36. } \
  37. friend constexpr Point& operator compound_op(Point& lhs, T value) noexcept { \
  38. lhs.x = static_cast<T>(lhs.x op value); \
  39. lhs.y = static_cast<T>(lhs.y op value); \
  40. return lhs; \
  41. }
  42. ARITHMETIC_OP(+, +=)
  43. ARITHMETIC_OP(-, -=)
  44. ARITHMETIC_OP(*, *=)
  45. ARITHMETIC_OP(/, /=)
  46. #undef ARITHMETIC_OP
  47. friend constexpr bool operator==(const Point&, const Point&) = default;
  48. };
  49. } // namespace Common