rect2.hpp 10 KB

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