Rect.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Rect.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. #include "Point.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// Rectangle class, which have a position and a size
  14. class Rect
  15. {
  16. public:
  17. int x;
  18. int y;
  19. int w;
  20. int h;
  21. Rect()
  22. {
  23. x = y = w = h = -1;
  24. }
  25. Rect(int X, int Y, int W, int H)
  26. {
  27. x = X;
  28. y = Y;
  29. w = W;
  30. h = H;
  31. }
  32. Rect(const Point & position, const Point & size)
  33. {
  34. x = position.x;
  35. y = position.y;
  36. w = size.x;
  37. h = size.y;
  38. }
  39. Rect(const Rect& r) = default;
  40. DLL_LINKAGE static Rect createCentered( const Point & around, const Point & size );
  41. DLL_LINKAGE static Rect createCentered( const Rect & target, const Point & size );
  42. DLL_LINKAGE static Rect createAround(const Rect &r, int borderWidth);
  43. bool isInside(int qx, int qy) const
  44. {
  45. if (qx > x && qx<x+w && qy>y && qy<y+h)
  46. return true;
  47. return false;
  48. }
  49. bool isInside(const Point & q) const
  50. {
  51. return isInside(q.x,q.y);
  52. }
  53. int top() const
  54. {
  55. return y;
  56. }
  57. int bottom() const
  58. {
  59. return y+h;
  60. }
  61. int left() const
  62. {
  63. return x;
  64. }
  65. int right() const
  66. {
  67. return x+w;
  68. }
  69. Point topLeft() const
  70. {
  71. return Point(x,y);
  72. }
  73. Point topRight() const
  74. {
  75. return Point(x+w,y);
  76. }
  77. Point bottomLeft() const
  78. {
  79. return Point(x,y+h);
  80. }
  81. Point bottomRight() const
  82. {
  83. return Point(x+w,y+h);
  84. }
  85. Point center() const
  86. {
  87. return Point(x+w/2,y+h/2);
  88. }
  89. Point dimensions() const
  90. {
  91. return Point(w,h);
  92. }
  93. Rect resize(const int size) const
  94. {
  95. return Rect(x-size,y-size,w+2*size,h+2*size);
  96. }
  97. void moveTo(const Point & dest)
  98. {
  99. x = dest.x;
  100. y = dest.y;
  101. }
  102. Rect operator+(const Point &p) const
  103. {
  104. return Rect(x+p.x,y+p.y,w,h);
  105. }
  106. Rect operator-(const Point &p) const
  107. {
  108. return Rect(x-p.x,y-p.y,w,h);
  109. }
  110. template<typename T>
  111. Rect operator*(const T &mul) const
  112. {
  113. return Rect(x*mul, y*mul, w*mul, h*mul);
  114. }
  115. Rect& operator=(const Rect &p)
  116. {
  117. x = p.x;
  118. y = p.y;
  119. w = p.w;
  120. h = p.h;
  121. return *this;
  122. }
  123. Rect& operator+=(const Point &p)
  124. {
  125. x += p.x;
  126. y += p.y;
  127. return *this;
  128. }
  129. Rect& operator-=(const Point &p)
  130. {
  131. x -= p.x;
  132. y -= p.y;
  133. return *this;
  134. }
  135. bool operator == (const Rect & other)
  136. {
  137. return x == other.x && y == other.y && w == other.w && h == other.h;
  138. }
  139. /// returns distance from this rect to point, or 0 if inside
  140. DLL_LINKAGE int distanceTo(const Point & target) const;
  141. /// returns true if this rect intersects with another rect
  142. DLL_LINKAGE bool intersectionTest(const Rect & other) const;
  143. /// returns true if this rect intersects with line specified by two points
  144. DLL_LINKAGE bool intersectionTest(const Point & line1, const Point & line2) const;
  145. /// Returns rect that represents intersection of two rects
  146. DLL_LINKAGE Rect intersect(const Rect & other) const;
  147. /// Returns rect union - rect that covers both this rect and provided rect
  148. DLL_LINKAGE Rect include(const Rect & other) const;
  149. template <typename Handler>
  150. void serialize(Handler &h)
  151. {
  152. h & x;
  153. h & y;
  154. h & w;
  155. h & this->h;
  156. }
  157. };
  158. VCMI_LIB_NAMESPACE_END