Point.h 2.1 KB

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