rect2.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*************************************************************************/
  2. /* rect2.hpp */
  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 GODOT_RECT2_HPP
  31. #define GODOT_RECT2_HPP
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. #include <godot_cpp/variant/vector2.hpp>
  35. namespace godot {
  36. class Rect2i;
  37. class String;
  38. class Transform2D;
  39. class Rect2 {
  40. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  41. friend class Variant;
  42. public:
  43. Point2 position;
  44. Size2 size;
  45. const Vector2 &get_position() const { return position; }
  46. void set_position(const Vector2 &p_pos) { position = p_pos; }
  47. const Vector2 &get_size() const { return size; }
  48. void set_size(const Vector2 &p_size) { size = p_size; }
  49. real_t get_area() const { return size.width * size.height; }
  50. inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
  51. if (p_include_borders) {
  52. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  53. return false;
  54. }
  55. if ((position.x + size.width) < p_rect.position.x) {
  56. return false;
  57. }
  58. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  59. return false;
  60. }
  61. if ((position.y + size.height) < p_rect.position.y) {
  62. return false;
  63. }
  64. } else {
  65. if (position.x >= (p_rect.position.x + p_rect.size.width)) {
  66. return false;
  67. }
  68. if ((position.x + size.width) <= p_rect.position.x) {
  69. return false;
  70. }
  71. if (position.y >= (p_rect.position.y + p_rect.size.height)) {
  72. return false;
  73. }
  74. if ((position.y + size.height) <= p_rect.position.y) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. inline real_t distance_to(const Vector2 &p_point) const {
  81. real_t dist = 0.0;
  82. bool inside = true;
  83. if (p_point.x < position.x) {
  84. real_t d = position.x - p_point.x;
  85. dist = d;
  86. inside = false;
  87. }
  88. if (p_point.y < position.y) {
  89. real_t d = position.y - p_point.y;
  90. dist = inside ? d : Math::min(dist, d);
  91. inside = false;
  92. }
  93. if (p_point.x >= (position.x + size.x)) {
  94. real_t d = p_point.x - (position.x + size.x);
  95. dist = inside ? d : Math::min(dist, d);
  96. inside = false;
  97. }
  98. if (p_point.y >= (position.y + size.y)) {
  99. real_t d = p_point.y - (position.y + size.y);
  100. dist = inside ? d : Math::min(dist, d);
  101. inside = false;
  102. }
  103. if (inside) {
  104. return 0;
  105. } else {
  106. return dist;
  107. }
  108. }
  109. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  110. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
  111. inline bool encloses(const Rect2 &p_rect) const {
  112. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  113. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  114. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  115. }
  116. inline bool has_no_area() const {
  117. return (size.x <= 0 || size.y <= 0);
  118. }
  119. // Returns the instersection between two Rect2s or an empty Rect2 if there is no intersection
  120. inline Rect2 intersection(const Rect2 &p_rect) const {
  121. Rect2 new_rect = p_rect;
  122. if (!intersects(new_rect)) {
  123. return Rect2();
  124. }
  125. new_rect.position.x = Math::max(p_rect.position.x, position.x);
  126. new_rect.position.y = Math::max(p_rect.position.y, position.y);
  127. Point2 p_rect_end = p_rect.position + p_rect.size;
  128. Point2 end = position + size;
  129. new_rect.size.x = Math::min(p_rect_end.x, end.x) - new_rect.position.x;
  130. new_rect.size.y = Math::min(p_rect_end.y, end.y) - new_rect.position.y;
  131. return new_rect;
  132. }
  133. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  134. Rect2 new_rect;
  135. new_rect.position.x = Math::min(p_rect.position.x, position.x);
  136. new_rect.position.y = Math::min(p_rect.position.y, position.y);
  137. new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
  138. new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
  139. new_rect.size = new_rect.size - new_rect.position; // make relative again
  140. return new_rect;
  141. }
  142. inline bool has_point(const Point2 &p_point) const {
  143. if (p_point.x < position.x) {
  144. return false;
  145. }
  146. if (p_point.y < position.y) {
  147. return false;
  148. }
  149. if (p_point.x >= (position.x + size.x)) {
  150. return false;
  151. }
  152. if (p_point.y >= (position.y + size.y)) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. bool is_equal_approx(const Rect2 &p_rect) const;
  158. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  159. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  160. inline Rect2 grow(real_t p_amount) const {
  161. Rect2 g = *this;
  162. g.position.x -= p_amount;
  163. g.position.y -= p_amount;
  164. g.size.width += p_amount * 2;
  165. g.size.height += p_amount * 2;
  166. return g;
  167. }
  168. inline Rect2 grow_side(Side p_side, real_t p_amount) const {
  169. Rect2 g = *this;
  170. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  171. (SIDE_TOP == p_side) ? p_amount : 0,
  172. (SIDE_RIGHT == p_side) ? p_amount : 0,
  173. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  174. return g;
  175. }
  176. inline Rect2 grow_side_bind(uint32_t p_side, real_t p_amount) const {
  177. return grow_side(Side(p_side), p_amount);
  178. }
  179. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  180. Rect2 g = *this;
  181. g.position.x -= p_left;
  182. g.position.y -= p_top;
  183. g.size.width += p_left + p_right;
  184. g.size.height += p_top + p_bottom;
  185. return g;
  186. }
  187. inline Rect2 expand(const Vector2 &p_vector) const {
  188. Rect2 r = *this;
  189. r.expand_to(p_vector);
  190. return r;
  191. }
  192. inline void expand_to(const Vector2 &p_vector) { // in place function for speed
  193. Vector2 begin = position;
  194. Vector2 end = position + size;
  195. if (p_vector.x < begin.x) {
  196. begin.x = p_vector.x;
  197. }
  198. if (p_vector.y < begin.y) {
  199. begin.y = p_vector.y;
  200. }
  201. if (p_vector.x > end.x) {
  202. end.x = p_vector.x;
  203. }
  204. if (p_vector.y > end.y) {
  205. end.y = p_vector.y;
  206. }
  207. position = begin;
  208. size = end - begin;
  209. }
  210. inline Rect2 abs() const {
  211. return Rect2(Point2(position.x + Math::min(size.x, (real_t)0), position.y + Math::min(size.y, (real_t)0)), size.abs());
  212. }
  213. Vector2 get_support(const Vector2 &p_normal) const {
  214. Vector2 half_extents = size * 0.5;
  215. Vector2 ofs = position + half_extents;
  216. return Vector2(
  217. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  218. (p_normal.y > 0) ? -half_extents.y : half_extents.y) +
  219. ofs;
  220. }
  221. inline bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
  222. Vector2 center = position + size * 0.5;
  223. int side_plus = 0;
  224. int side_minus = 0;
  225. Vector2 end = position + size;
  226. int i_f = p_point_count - 1;
  227. for (int i = 0; i < p_point_count; i++) {
  228. const Vector2 &a = p_points[i_f];
  229. const Vector2 &b = p_points[i];
  230. i_f = i;
  231. Vector2 r = (b - a);
  232. float l = r.length();
  233. if (l == 0.0) {
  234. continue;
  235. }
  236. // check inside
  237. Vector2 tg = r.orthogonal();
  238. float s = tg.dot(center) - tg.dot(a);
  239. if (s < 0.0) {
  240. side_plus++;
  241. } else {
  242. side_minus++;
  243. }
  244. // check ray box
  245. r /= l;
  246. Vector2 ir((real_t)1.0 / r.x, (real_t)1.0 / r.y);
  247. // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
  248. // r.org is origin of ray
  249. Vector2 t13 = (position - a) * ir;
  250. Vector2 t24 = (end - a) * ir;
  251. float tmin = Math::max(Math::min(t13.x, t24.x), Math::min(t13.y, t24.y));
  252. float tmax = Math::min(Math::max(t13.x, t24.x), Math::max(t13.y, t24.y));
  253. // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
  254. if (tmax < 0 || tmin > tmax || tmin >= l) {
  255. continue;
  256. }
  257. return true;
  258. }
  259. if (side_plus * side_minus == 0) {
  260. return true; // all inside
  261. } else {
  262. return false;
  263. }
  264. }
  265. inline void set_end(const Vector2 &p_end) {
  266. size = p_end - position;
  267. }
  268. inline Vector2 get_end() const {
  269. return position + size;
  270. }
  271. operator String() const;
  272. operator Rect2i() const;
  273. Rect2() {}
  274. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  275. position(Point2(p_x, p_y)),
  276. size(Size2(p_width, p_height)) {
  277. }
  278. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  279. position(p_pos),
  280. size(p_size) {
  281. }
  282. };
  283. } // namespace godot
  284. #endif // GODOT_RECT2_HPP