rect2.hpp 10 KB

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