Rect.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Rect& operator=(const Rect &p)
  111. {
  112. x = p.x;
  113. y = p.y;
  114. w = p.w;
  115. h = p.h;
  116. return *this;
  117. }
  118. Rect& operator+=(const Point &p)
  119. {
  120. x += p.x;
  121. y += p.y;
  122. return *this;
  123. }
  124. Rect& operator-=(const Point &p)
  125. {
  126. x -= p.x;
  127. y -= p.y;
  128. return *this;
  129. }
  130. bool operator == (const Rect & other)
  131. {
  132. return x == other.x && y == other.y && w == other.w && h == other.h;
  133. }
  134. /// returns distance from this rect to point, or 0 if inside
  135. DLL_LINKAGE int distanceTo(const Point & target) const;
  136. /// returns true if this rect intersects with another rect
  137. DLL_LINKAGE bool intersectionTest(const Rect & other) const;
  138. /// returns true if this rect intersects with line specified by two points
  139. DLL_LINKAGE bool intersectionTest(const Point & line1, const Point & line2) const;
  140. /// Returns rect that represents intersection of two rects
  141. DLL_LINKAGE Rect intersect(const Rect & other) const;
  142. /// Returns rect union - rect that covers both this rect and provided rect
  143. DLL_LINKAGE Rect include(const Rect & other) const;
  144. template <typename Handler>
  145. void serialize(Handler &h, const int version)
  146. {
  147. h & x;
  148. h & y;
  149. h & w;
  150. h & this->h;
  151. }
  152. };
  153. VCMI_LIB_NAMESPACE_END