No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rect.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #ifndef RECT_H
  24. #define RECT_H
  25. #include "prerequisites.h"
  26. #include "point.h"
  27. #include "size.h"
  28. template <class T>
  29. class TPoint;
  30. template <class T>
  31. class TSize;
  32. template <class T>
  33. class TRect
  34. {
  35. public:
  36. inline TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
  37. inline TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
  38. inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
  39. inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
  40. inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
  41. inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x+size.width()-1), y2(y+size.height()-1) { }
  42. inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
  43. inline bool isEmpty() const { return x1 > x2 || y1 > y2; }
  44. inline bool isValid() const { return x1 <= x2 && y1 <= y2; }
  45. inline T left() const { return x1; }
  46. inline T top() const { return y1; }
  47. inline T right() const { return x2; }
  48. inline T bottom() const { return y2; }
  49. inline T x() const { return x1; }
  50. inline T y() const { return y1; }
  51. inline TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
  52. inline TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
  53. inline TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
  54. inline TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
  55. inline TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
  56. inline T width() const { return x2 - x1 + 1; }
  57. inline T height() const { return y2 - y1 + 1; }
  58. inline TSize<T> size() const { return TSize<T>(width(), height()); }
  59. inline void setLeft(T pos) { x1 = pos; }
  60. inline void setTop(T pos) { y1 = pos; }
  61. inline void setRight(T pos) { x2 = pos; }
  62. inline void setBottom(T pos) { y2 = pos; }
  63. inline void setX(T x) { x1 = x; }
  64. inline void setY(T y) { y1 = y; }
  65. inline void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
  66. inline void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
  67. inline void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
  68. inline void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
  69. inline void setWidth(T width) { x2 = x1 + width - 1; }
  70. inline void setHeight(T height) { y2 = y1 + height- 1; }
  71. inline void setSize(T width, T height) { x2 = x1 + width; y2 = y1 + height; }
  72. inline void setSize(const TSize<T>& size) { x2 = x1 + size.width(); y2 = y1 + size.height(); }
  73. inline void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
  74. inline void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
  75. inline void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
  76. inline void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
  77. inline void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
  78. inline void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
  79. inline void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
  80. inline void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
  81. inline void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
  82. inline void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
  83. inline void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
  84. inline void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
  85. inline void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
  86. inline void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
  87. inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
  88. inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x(), y1 + p.y()), TPoint<T>(x2 + p.x(), y2 + p.y())); }
  89. void moveCenter(const TPoint<T> &p) {
  90. T w = x2 - x1;
  91. T h = y2 - y1;
  92. x1 = p.x() - w/2;
  93. y1 = p.y() - h/2;
  94. x2 = x1 + w;
  95. y2 = y1 + h;
  96. }
  97. bool contains(const TPoint<T> &p, bool insideOnly = false) const {
  98. T l, r;
  99. if(x2 < x1 - 1) {
  100. l = x2;
  101. r = x1;
  102. } else {
  103. l = x1;
  104. r = x2;
  105. }
  106. if(insideOnly) {
  107. if(p.x() <= l || p.x() >= r)
  108. return false;
  109. } else {
  110. if(p.x() < l || p.x() > r)
  111. return false;
  112. }
  113. int t, b;
  114. if(y2 < y1 - 1) {
  115. t = y2;
  116. b = y1;
  117. } else {
  118. t = y1;
  119. b = y2;
  120. }
  121. if(insideOnly) {
  122. if(p.y() <= t || p.y() >= b)
  123. return false;
  124. } else {
  125. if(p.y() < t || p.y() > b)
  126. return false;
  127. }
  128. return true;
  129. }
  130. bool intersects(const TRect<T> &r) const {
  131. if(isNull() || r.isNull())
  132. return false;
  133. int l1 = x1;
  134. int r1 = x1;
  135. if(x2 - x1 + 1 < 0)
  136. l1 = x2;
  137. else
  138. r1 = x2;
  139. int l2 = r.x1;
  140. int r2 = r.x1;
  141. if(r.x2 - r.x1 + 1 < 0)
  142. l2 = r.x2;
  143. else
  144. r2 = r.x2;
  145. if (l1 > r2 || l2 > r1)
  146. return false;
  147. int t1 = y1;
  148. int b1 = y1;
  149. if(y2 - y1 + 1 < 0)
  150. t1 = y2;
  151. else
  152. b1 = y2;
  153. int t2 = r.y1;
  154. int b2 = r.y1;
  155. if (r.y2 - r.y1 + 1 < 0)
  156. t2 = r.y2;
  157. else
  158. b2 = r.y2;
  159. if(t1 > b2 || t2 > b1)
  160. return false;
  161. return true;
  162. }
  163. TRect<T> united(const TRect<T> &r) const {
  164. if(isNull() || r.isNull())
  165. return TRect<T>();
  166. int l1 = x1;
  167. int r1 = x1;
  168. if (x2 - x1 + 1 < 0)
  169. l1 = x2;
  170. else
  171. r1 = x2;
  172. int l2 = r.x1;
  173. int r2 = r.x1;
  174. if(r.x2 - r.x1 + 1 < 0)
  175. l2 = r.x2;
  176. else
  177. r2 = r.x2;
  178. if(l1 > r2 || l2 > r1)
  179. return TRect<T>();
  180. int t1 = y1;
  181. int b1 = y1;
  182. if(y2 - y1 + 1 < 0)
  183. t1 = y2;
  184. else
  185. b1 = y2;
  186. int t2 = r.y1;
  187. int b2 = r.y1;
  188. if(r.y2 - r.y1 + 1 < 0)
  189. t2 = r.y2;
  190. else
  191. b2 = r.y2;
  192. if(t1 > b2 || t2 > b1)
  193. return TRect<T>();
  194. TRect<T> tmp;
  195. tmp.x1 = std::max(l1, l2);
  196. tmp.x2 = std::min(r1, r2);
  197. tmp.y1 = std::max(t1, t2);
  198. tmp.y2 = std::min(b1, b2);
  199. return tmp;
  200. }
  201. TRect<T> intersection(const TRect<T> &r) const {
  202. if(isNull())
  203. return r;
  204. if(r.isNull())
  205. return *this;
  206. int l1 = x1;
  207. int r1 = x1;
  208. if(x2 - x1 + 1 < 0)
  209. l1 = x2;
  210. else
  211. r1 = x2;
  212. int l2 = r.x1;
  213. int r2 = r.x1;
  214. if(r.x2 - r.x1 + 1 < 0)
  215. l2 = r.x2;
  216. else
  217. r2 = r.x2;
  218. int t1 = y1;
  219. int b1 = y1;
  220. if(y2 - y1 + 1 < 0)
  221. t1 = y2;
  222. else
  223. b1 = y2;
  224. int t2 = r.y1;
  225. int b2 = r.y1;
  226. if(r.y2 - r.y1 + 1 < 0)
  227. t2 = r.y2;
  228. else
  229. b2 = r.y2;
  230. TRect<T> tmp;
  231. tmp.x1 = std::min(l1, l2);
  232. tmp.x2 = std::max(r1, r2);
  233. tmp.y1 = std::min(t1, t2);
  234. tmp.y2 = std::max(b1, b2);
  235. return tmp;
  236. }
  237. inline TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
  238. inline bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
  239. inline bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
  240. private:
  241. T x1, y1, x2, y2;
  242. };
  243. typedef TRect<int> Rect;
  244. typedef TRect<float> RectF;
  245. #endif // RECT_H