Point.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. template<typename T>
  52. constexpr Point& operator+=(const T &b)
  53. {
  54. x += b.x;
  55. y += b.y;
  56. return *this;
  57. }
  58. constexpr Point operator-() const
  59. {
  60. return Point(-x, -y);
  61. }
  62. template<typename T>
  63. constexpr Point operator-(const T &b) const
  64. {
  65. return Point(x - b.x, y - b.y);
  66. }
  67. template<typename T>
  68. constexpr Point& operator-=(const T &b)
  69. {
  70. x -= b.x;
  71. y -= b.y;
  72. return *this;
  73. }
  74. template<typename T> constexpr Point& operator=(const T &t)
  75. {
  76. x = t.x;
  77. y = t.y;
  78. return *this;
  79. }
  80. template<typename T> constexpr bool operator==(const T &t) const
  81. {
  82. return x == t.x && y == t.y;
  83. }
  84. template<typename T> constexpr bool operator!=(const T &t) const
  85. {
  86. return !(*this == t);
  87. }
  88. constexpr bool isValid() const
  89. {
  90. return x > std::numeric_limits<int>::min() && y > std::numeric_limits<int>::min();
  91. }
  92. constexpr int lengthSquared() const
  93. {
  94. return x * x + y * y;
  95. }
  96. int length() const
  97. {
  98. return std::sqrt(lengthSquared());
  99. }
  100. double angle() const
  101. {
  102. return std::atan2(y, x); // rad
  103. }
  104. template <typename Handler>
  105. void serialize(Handler &h)
  106. {
  107. h & x;
  108. h & y;
  109. }
  110. };
  111. VCMI_LIB_NAMESPACE_END