rect2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*************************************************************************/
  2. /* rect2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. }
  47. if ((position.x + size.width) < p_rect.position.x) {
  48. return false;
  49. }
  50. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  51. return false;
  52. }
  53. if ((position.y + size.height) < p_rect.position.y) {
  54. return false;
  55. }
  56. } else {
  57. if (position.x >= (p_rect.position.x + p_rect.size.width)) {
  58. return false;
  59. }
  60. if ((position.x + size.width) <= p_rect.position.x) {
  61. return false;
  62. }
  63. if (position.y >= (p_rect.position.y + p_rect.size.height)) {
  64. return false;
  65. }
  66. if ((position.y + size.height) <= p_rect.position.y) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. inline real_t distance_to(const Vector2 &p_point) const {
  73. real_t dist = 0.0;
  74. bool inside = true;
  75. if (p_point.x < position.x) {
  76. real_t d = position.x - p_point.x;
  77. dist = d;
  78. inside = false;
  79. }
  80. if (p_point.y < position.y) {
  81. real_t d = position.y - p_point.y;
  82. dist = inside ? d : MIN(dist, d);
  83. inside = false;
  84. }
  85. if (p_point.x >= (position.x + size.x)) {
  86. real_t d = p_point.x - (position.x + size.x);
  87. dist = inside ? d : MIN(dist, d);
  88. inside = false;
  89. }
  90. if (p_point.y >= (position.y + size.y)) {
  91. real_t d = p_point.y - (position.y + size.y);
  92. dist = inside ? d : MIN(dist, d);
  93. inside = false;
  94. }
  95. if (inside) {
  96. return 0;
  97. } else {
  98. return dist;
  99. }
  100. }
  101. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  102. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
  103. inline bool encloses(const Rect2 &p_rect) const {
  104. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  105. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  106. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  107. }
  108. _FORCE_INLINE_ bool has_no_area() const {
  109. return (size.x <= 0 || size.y <= 0);
  110. }
  111. inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect
  112. Rect2 new_rect = p_rect;
  113. if (!intersects(new_rect)) {
  114. return Rect2();
  115. }
  116. new_rect.position.x = MAX(p_rect.position.x, position.x);
  117. new_rect.position.y = MAX(p_rect.position.y, position.y);
  118. Point2 p_rect_end = p_rect.position + p_rect.size;
  119. Point2 end = position + size;
  120. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  121. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  122. return new_rect;
  123. }
  124. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  125. Rect2 new_rect;
  126. new_rect.position.x = MIN(p_rect.position.x, position.x);
  127. new_rect.position.y = MIN(p_rect.position.y, position.y);
  128. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  129. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  130. new_rect.size = new_rect.size - new_rect.position; //make relative again
  131. return new_rect;
  132. };
  133. inline bool has_point(const Point2 &p_point) const {
  134. if (p_point.x < position.x) {
  135. return false;
  136. }
  137. if (p_point.y < position.y) {
  138. return false;
  139. }
  140. if (p_point.x >= (position.x + size.x)) {
  141. return false;
  142. }
  143. if (p_point.y >= (position.y + size.y)) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. bool is_equal_approx(const Rect2 &p_rect) const;
  149. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  150. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  151. inline Rect2 grow(real_t p_by) const {
  152. Rect2 g = *this;
  153. g.grow_by(p_by);
  154. return g;
  155. }
  156. inline void grow_by(real_t p_by) {
  157. position.x -= p_by;
  158. position.y -= p_by;
  159. size.width += p_by * 2;
  160. size.height += p_by * 2;
  161. }
  162. inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const {
  163. Rect2 g = *this;
  164. g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  165. (MARGIN_TOP == p_margin) ? p_amount : 0,
  166. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  167. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  168. return g;
  169. }
  170. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  171. Rect2 g = *this;
  172. g.position.x -= p_left;
  173. g.position.y -= p_top;
  174. g.size.width += p_left + p_right;
  175. g.size.height += p_top + p_bottom;
  176. return g;
  177. }
  178. _FORCE_INLINE_ Rect2 expand(const Vector2 &p_vector) const {
  179. Rect2 r = *this;
  180. r.expand_to(p_vector);
  181. return r;
  182. }
  183. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  184. Vector2 begin = position;
  185. Vector2 end = position + size;
  186. if (p_vector.x < begin.x) {
  187. begin.x = p_vector.x;
  188. }
  189. if (p_vector.y < begin.y) {
  190. begin.y = p_vector.y;
  191. }
  192. if (p_vector.x > end.x) {
  193. end.x = p_vector.x;
  194. }
  195. if (p_vector.y > end.y) {
  196. end.y = p_vector.y;
  197. }
  198. position = begin;
  199. size = end - begin;
  200. }
  201. _FORCE_INLINE_ Rect2 abs() const {
  202. return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
  203. }
  204. operator String() const { return String(position) + ", " + String(size); }
  205. Rect2() {}
  206. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  207. position(Point2(p_x, p_y)),
  208. size(Size2(p_width, p_height)) {
  209. }
  210. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  211. position(p_pos),
  212. size(p_size) {
  213. }
  214. };
  215. struct Rect2i {
  216. Point2i position;
  217. Size2i size;
  218. const Point2i &get_position() const { return position; }
  219. void set_position(const Point2i &p_position) { position = p_position; }
  220. const Size2i &get_size() const { return size; }
  221. void set_size(const Size2i &p_size) { size = p_size; }
  222. int get_area() const { return size.width * size.height; }
  223. inline bool intersects(const Rect2i &p_rect) const {
  224. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  225. return false;
  226. }
  227. if ((position.x + size.width) < p_rect.position.x) {
  228. return false;
  229. }
  230. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  231. return false;
  232. }
  233. if ((position.y + size.height) < p_rect.position.y) {
  234. return false;
  235. }
  236. return true;
  237. }
  238. inline bool encloses(const Rect2i &p_rect) const {
  239. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  240. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  241. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  242. }
  243. _FORCE_INLINE_ bool has_no_area() const {
  244. return (size.x <= 0 || size.y <= 0);
  245. }
  246. inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
  247. Rect2i new_rect = p_rect;
  248. if (!intersects(new_rect)) {
  249. return Rect2i();
  250. }
  251. new_rect.position.x = MAX(p_rect.position.x, position.x);
  252. new_rect.position.y = MAX(p_rect.position.y, position.y);
  253. Point2 p_rect_end = p_rect.position + p_rect.size;
  254. Point2 end = position + size;
  255. new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.position.x);
  256. new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.position.y);
  257. return new_rect;
  258. }
  259. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  260. Rect2i new_rect;
  261. new_rect.position.x = MIN(p_rect.position.x, position.x);
  262. new_rect.position.y = MIN(p_rect.position.y, position.y);
  263. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  264. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  265. new_rect.size = new_rect.size - new_rect.position; //make relative again
  266. return new_rect;
  267. };
  268. bool has_point(const Point2 &p_point) const {
  269. if (p_point.x < position.x) {
  270. return false;
  271. }
  272. if (p_point.y < position.y) {
  273. return false;
  274. }
  275. if (p_point.x >= (position.x + size.x)) {
  276. return false;
  277. }
  278. if (p_point.y >= (position.y + size.y)) {
  279. return false;
  280. }
  281. return true;
  282. }
  283. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  284. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  285. Rect2i grow(int p_by) const {
  286. Rect2i g = *this;
  287. g.position.x -= p_by;
  288. g.position.y -= p_by;
  289. g.size.width += p_by * 2;
  290. g.size.height += p_by * 2;
  291. return g;
  292. }
  293. inline Rect2i grow_margin(Margin p_margin, int p_amount) const {
  294. Rect2i g = *this;
  295. g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  296. (MARGIN_TOP == p_margin) ? p_amount : 0,
  297. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  298. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  299. return g;
  300. }
  301. inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
  302. Rect2i g = *this;
  303. g.position.x -= p_left;
  304. g.position.y -= p_top;
  305. g.size.width += p_left + p_right;
  306. g.size.height += p_top + p_bottom;
  307. return g;
  308. }
  309. _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const {
  310. Rect2i r = *this;
  311. r.expand_to(p_vector);
  312. return r;
  313. }
  314. inline void expand_to(const Point2i &p_vector) {
  315. Point2i begin = position;
  316. Point2i end = position + size;
  317. if (p_vector.x < begin.x) {
  318. begin.x = p_vector.x;
  319. }
  320. if (p_vector.y < begin.y) {
  321. begin.y = p_vector.y;
  322. }
  323. if (p_vector.x > end.x) {
  324. end.x = p_vector.x;
  325. }
  326. if (p_vector.y > end.y) {
  327. end.y = p_vector.y;
  328. }
  329. position = begin;
  330. size = end - begin;
  331. }
  332. operator String() const { return String(position) + ", " + String(size); }
  333. operator Rect2() const { return Rect2(position, size); }
  334. Rect2i(const Rect2 &p_r2) :
  335. position(p_r2.position),
  336. size(p_r2.size) {
  337. }
  338. Rect2i() {}
  339. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  340. position(Point2(p_x, p_y)),
  341. size(Size2(p_width, p_height)) {
  342. }
  343. Rect2i(const Point2 &p_pos, const Size2 &p_size) :
  344. position(p_pos),
  345. size(p_size) {
  346. }
  347. };
  348. #endif // RECT2_H