Point.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Point.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. class int3;
  13. // A point with x/y coordinate, used mostly for graphic rendering
  14. class Point
  15. {
  16. public:
  17. int x, y;
  18. //constructors
  19. constexpr Point() : x(0), y(0)
  20. {
  21. }
  22. constexpr Point(int X, int Y)
  23. : x(X)
  24. , y(Y)
  25. {
  26. }
  27. constexpr static Point makeInvalid()
  28. {
  29. return Point(std::numeric_limits<int>::min(), std::numeric_limits<int>::min());
  30. }
  31. explicit DLL_LINKAGE Point(const int3 &a);
  32. template<typename T>
  33. constexpr Point operator+(const T &b) const
  34. {
  35. return Point(x+b.x,y+b.y);
  36. }
  37. template<typename T>
  38. constexpr Point operator/(const T &div) const
  39. {
  40. return Point(x/div, y/div);
  41. }
  42. template<typename T>
  43. constexpr Point operator*(const T &mul) const
  44. {
  45. return Point(x*mul, y*mul);
  46. }
  47. constexpr Point operator/(const Point &b) const
  48. {
  49. return Point(x/b.x,y/b.y);
  50. }
  51. constexpr Point operator*(const Point &b) const
  52. {
  53. return Point(x*b.x,y*b.y);
  54. }
  55. template<typename T>
  56. constexpr Point& operator+=(const T &b)
  57. {
  58. x += b.x;
  59. y += b.y;
  60. return *this;
  61. }
  62. constexpr Point operator-() const
  63. {
  64. return Point(-x, -y);
  65. }
  66. template<typename T>
  67. constexpr Point operator-(const T &b) const
  68. {
  69. return Point(x - b.x, y - b.y);
  70. }
  71. template<typename T>
  72. constexpr Point& operator-=(const T &b)
  73. {
  74. x -= b.x;
  75. y -= b.y;
  76. return *this;
  77. }
  78. template<typename T> constexpr Point& operator=(const T &t)
  79. {
  80. x = t.x;
  81. y = t.y;
  82. return *this;
  83. }
  84. template<typename T> constexpr bool operator==(const T &t) const
  85. {
  86. return x == t.x && y == t.y;
  87. }
  88. template<typename T> constexpr bool operator!=(const T &t) const
  89. {
  90. return !(*this == t);
  91. }
  92. constexpr bool isValid() const
  93. {
  94. return x > std::numeric_limits<int>::min() && y > std::numeric_limits<int>::min();
  95. }
  96. constexpr int lengthSquared() const
  97. {
  98. return x * x + y * y;
  99. }
  100. int length() const
  101. {
  102. return std::sqrt(lengthSquared());
  103. }
  104. double angle() const
  105. {
  106. return std::atan2(y, x); // rad
  107. }
  108. template <typename Handler>
  109. void serialize(Handler &h)
  110. {
  111. h & x;
  112. h & y;
  113. }
  114. };
  115. VCMI_LIB_NAMESPACE_END