point.h 3.3 KB

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