rect2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*************************************************************************/
  2. /* rect2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RECT2_H
  31. #define RECT2_H
  32. #include "core/math/vector2.h" // also includes math_funcs and ustring
  33. struct Transform2D;
  34. struct Rect2 {
  35. Point2 position;
  36. Size2 size;
  37. const Vector2 &get_position() const { return position; }
  38. void set_position(const Vector2 &p_pos) { position = p_pos; }
  39. const Vector2 &get_size() const { return size; }
  40. void set_size(const Vector2 &p_size) { size = p_size; }
  41. real_t get_area() const { return size.width * size.height; }
  42. inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
  43. if (p_include_borders) {
  44. if (position.x > (p_rect.position.x + p_rect.size.width))
  45. return false;
  46. if ((position.x + size.width) < p_rect.position.x)
  47. return false;
  48. if (position.y > (p_rect.position.y + p_rect.size.height))
  49. return false;
  50. if ((position.y + size.height) < p_rect.position.y)
  51. return false;
  52. } else {
  53. if (position.x >= (p_rect.position.x + p_rect.size.width))
  54. return false;
  55. if ((position.x + size.width) <= p_rect.position.x)
  56. return false;
  57. if (position.y >= (p_rect.position.y + p_rect.size.height))
  58. return false;
  59. if ((position.y + size.height) <= p_rect.position.y)
  60. return false;
  61. }
  62. return true;
  63. }
  64. inline real_t distance_to(const Vector2 &p_point) const {
  65. real_t dist = 0.0;
  66. bool inside = true;
  67. if (p_point.x < position.x) {
  68. real_t d = position.x - p_point.x;
  69. dist = d;
  70. inside = false;
  71. }
  72. if (p_point.y < position.y) {
  73. real_t d = position.y - p_point.y;
  74. dist = inside ? d : MIN(dist, d);
  75. inside = false;
  76. }
  77. if (p_point.x >= (position.x + size.x)) {
  78. real_t d = p_point.x - (position.x + size.x);
  79. dist = inside ? d : MIN(dist, d);
  80. inside = false;
  81. }
  82. if (p_point.y >= (position.y + size.y)) {
  83. real_t d = p_point.y - (position.y + size.y);
  84. dist = inside ? d : MIN(dist, d);
  85. inside = false;
  86. }
  87. if (inside)
  88. return 0;
  89. else
  90. return dist;
  91. }
  92. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  93. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = NULL, Point2 *r_normal = NULL) const;
  94. inline bool encloses(const Rect2 &p_rect) const {
  95. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  96. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  97. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  98. }
  99. _FORCE_INLINE_ bool has_no_area() const {
  100. return (size.x <= 0 || size.y <= 0);
  101. }
  102. inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect
  103. Rect2 new_rect = p_rect;
  104. if (!intersects(new_rect))
  105. return Rect2();
  106. new_rect.position.x = MAX(p_rect.position.x, position.x);
  107. new_rect.position.y = MAX(p_rect.position.y, position.y);
  108. Point2 p_rect_end = p_rect.position + p_rect.size;
  109. Point2 end = position + size;
  110. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  111. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  112. return new_rect;
  113. }
  114. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  115. Rect2 new_rect;
  116. new_rect.position.x = MIN(p_rect.position.x, position.x);
  117. new_rect.position.y = MIN(p_rect.position.y, position.y);
  118. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  119. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  120. new_rect.size = new_rect.size - new_rect.position; //make relative again
  121. return new_rect;
  122. };
  123. inline bool has_point(const Point2 &p_point) const {
  124. if (p_point.x < position.x)
  125. return false;
  126. if (p_point.y < position.y)
  127. return false;
  128. if (p_point.x >= (position.x + size.x))
  129. return false;
  130. if (p_point.y >= (position.y + size.y))
  131. return false;
  132. return true;
  133. }
  134. bool is_equal_approx(const Rect2 &p_rect) const;
  135. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  136. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  137. inline Rect2 grow(real_t p_by) const {
  138. Rect2 g = *this;
  139. g.position.x -= p_by;
  140. g.position.y -= p_by;
  141. g.size.width += p_by * 2;
  142. g.size.height += p_by * 2;
  143. return g;
  144. }
  145. inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const {
  146. Rect2 g = *this;
  147. g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  148. (MARGIN_TOP == p_margin) ? p_amount : 0,
  149. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  150. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  151. return g;
  152. }
  153. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  154. Rect2 g = *this;
  155. g.position.x -= p_left;
  156. g.position.y -= p_top;
  157. g.size.width += p_left + p_right;
  158. g.size.height += p_top + p_bottom;
  159. return g;
  160. }
  161. _FORCE_INLINE_ Rect2 expand(const Vector2 &p_vector) const {
  162. Rect2 r = *this;
  163. r.expand_to(p_vector);
  164. return r;
  165. }
  166. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  167. Vector2 begin = position;
  168. Vector2 end = position + size;
  169. if (p_vector.x < begin.x)
  170. begin.x = p_vector.x;
  171. if (p_vector.y < begin.y)
  172. begin.y = p_vector.y;
  173. if (p_vector.x > end.x)
  174. end.x = p_vector.x;
  175. if (p_vector.y > end.y)
  176. end.y = p_vector.y;
  177. position = begin;
  178. size = end - begin;
  179. }
  180. _FORCE_INLINE_ Rect2 abs() const {
  181. return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
  182. }
  183. operator String() const { return String(position) + ", " + String(size); }
  184. Rect2() {}
  185. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  186. position(Point2(p_x, p_y)),
  187. size(Size2(p_width, p_height)) {
  188. }
  189. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  190. position(p_pos),
  191. size(p_size) {
  192. }
  193. };
  194. struct Rect2i {
  195. Point2i position;
  196. Size2i size;
  197. const Point2i &get_position() const { return position; }
  198. void set_position(const Point2i &p_position) { position = p_position; }
  199. const Size2i &get_size() const { return size; }
  200. void set_size(const Size2i &p_size) { size = p_size; }
  201. int get_area() const { return size.width * size.height; }
  202. inline bool intersects(const Rect2i &p_rect) const {
  203. if (position.x > (p_rect.position.x + p_rect.size.width))
  204. return false;
  205. if ((position.x + size.width) < p_rect.position.x)
  206. return false;
  207. if (position.y > (p_rect.position.y + p_rect.size.height))
  208. return false;
  209. if ((position.y + size.height) < p_rect.position.y)
  210. return false;
  211. return true;
  212. }
  213. inline bool encloses(const Rect2i &p_rect) const {
  214. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  215. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  216. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  217. }
  218. _FORCE_INLINE_ bool has_no_area() const {
  219. return (size.x <= 0 || size.y <= 0);
  220. }
  221. inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
  222. Rect2i new_rect = p_rect;
  223. if (!intersects(new_rect))
  224. return Rect2i();
  225. new_rect.position.x = MAX(p_rect.position.x, position.x);
  226. new_rect.position.y = MAX(p_rect.position.y, position.y);
  227. Point2 p_rect_end = p_rect.position + p_rect.size;
  228. Point2 end = position + size;
  229. new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.position.x);
  230. new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.position.y);
  231. return new_rect;
  232. }
  233. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  234. Rect2i new_rect;
  235. new_rect.position.x = MIN(p_rect.position.x, position.x);
  236. new_rect.position.y = MIN(p_rect.position.y, position.y);
  237. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  238. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  239. new_rect.size = new_rect.size - new_rect.position; //make relative again
  240. return new_rect;
  241. };
  242. bool has_point(const Point2 &p_point) const {
  243. if (p_point.x < position.x)
  244. return false;
  245. if (p_point.y < position.y)
  246. return false;
  247. if (p_point.x >= (position.x + size.x))
  248. return false;
  249. if (p_point.y >= (position.y + size.y))
  250. return false;
  251. return true;
  252. }
  253. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  254. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  255. Rect2i grow(int p_by) const {
  256. Rect2i g = *this;
  257. g.position.x -= p_by;
  258. g.position.y -= p_by;
  259. g.size.width += p_by * 2;
  260. g.size.height += p_by * 2;
  261. return g;
  262. }
  263. inline Rect2i grow_margin(Margin p_margin, int p_amount) const {
  264. Rect2i g = *this;
  265. g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  266. (MARGIN_TOP == p_margin) ? p_amount : 0,
  267. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  268. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  269. return g;
  270. }
  271. inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
  272. Rect2i g = *this;
  273. g.position.x -= p_left;
  274. g.position.y -= p_top;
  275. g.size.width += p_left + p_right;
  276. g.size.height += p_top + p_bottom;
  277. return g;
  278. }
  279. _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const {
  280. Rect2i r = *this;
  281. r.expand_to(p_vector);
  282. return r;
  283. }
  284. inline void expand_to(const Point2i &p_vector) {
  285. Point2i begin = position;
  286. Point2i end = position + size;
  287. if (p_vector.x < begin.x)
  288. begin.x = p_vector.x;
  289. if (p_vector.y < begin.y)
  290. begin.y = p_vector.y;
  291. if (p_vector.x > end.x)
  292. end.x = p_vector.x;
  293. if (p_vector.y > end.y)
  294. end.y = p_vector.y;
  295. position = begin;
  296. size = end - begin;
  297. }
  298. operator String() const { return String(position) + ", " + String(size); }
  299. operator Rect2() const { return Rect2(position, size); }
  300. Rect2i(const Rect2 &p_r2) :
  301. position(p_r2.position),
  302. size(p_r2.size) {
  303. }
  304. Rect2i() {}
  305. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  306. position(Point2(p_x, p_y)),
  307. size(Size2(p_width, p_height)) {
  308. }
  309. Rect2i(const Point2 &p_pos, const Size2 &p_size) :
  310. position(p_pos),
  311. size(p_size) {
  312. }
  313. };
  314. #endif // RECT2_H