rect2.hpp 7.9 KB

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