Rect2.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "Rect2.hpp"
  2. #include "String.hpp"
  3. #include "Transform2D.hpp"
  4. #include "Vector2.hpp"
  5. #include <cmath>
  6. namespace godot {
  7. #ifndef MAX
  8. #define MAX(a, b) (a > b ? a : b)
  9. #endif
  10. #ifndef MIN
  11. #define MIN(a, b) (a < b ? a : b)
  12. #endif
  13. real_t Rect2::distance_to(const Vector2 &p_point) const {
  14. real_t dist = 1e20;
  15. if (p_point.x < position.x) {
  16. dist = MIN(dist, position.x - p_point.x);
  17. }
  18. if (p_point.y < position.y) {
  19. dist = MIN(dist, position.y - p_point.y);
  20. }
  21. if (p_point.x >= (position.x + size.x)) {
  22. dist = MIN(p_point.x - (position.x + size.x), dist);
  23. }
  24. if (p_point.y >= (position.y + size.y)) {
  25. dist = MIN(p_point.y - (position.y + size.y), dist);
  26. }
  27. if (dist == 1e20)
  28. return 0;
  29. else
  30. return dist;
  31. }
  32. Rect2 Rect2::clip(const Rect2 &p_rect) const { /// return a clipped rect
  33. Rect2 new_rect = p_rect;
  34. if (!intersects(new_rect))
  35. return Rect2();
  36. new_rect.position.x = MAX(p_rect.position.x, position.x);
  37. new_rect.position.y = MAX(p_rect.position.y, position.y);
  38. Point2 p_rect_end = p_rect.position + p_rect.size;
  39. Point2 end = position + size;
  40. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  41. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  42. return new_rect;
  43. }
  44. Rect2 Rect2::merge(const Rect2 &p_rect) const { ///< return a merged rect
  45. Rect2 new_rect;
  46. new_rect.position.x = MIN(p_rect.position.x, position.x);
  47. new_rect.position.y = MIN(p_rect.position.y, position.y);
  48. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  49. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  50. new_rect.size = new_rect.size - new_rect.position; //make relative again
  51. return new_rect;
  52. }
  53. Rect2::operator String() const {
  54. return String(position) + ", " + String(size);
  55. }
  56. bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_position, Point2 *r_normal) const {
  57. real_t min = 0, max = 1;
  58. int axis = 0;
  59. real_t sign = 0;
  60. for (int i = 0; i < 2; i++) {
  61. real_t seg_from = p_from[i];
  62. real_t seg_to = p_to[i];
  63. real_t box_begin = position[i];
  64. real_t box_end = box_begin + size[i];
  65. real_t cmin, cmax;
  66. real_t csign;
  67. if (seg_from < seg_to) {
  68. if (seg_from > box_end || seg_to < box_begin)
  69. return false;
  70. real_t length = seg_to - seg_from;
  71. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  72. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  73. csign = -1.0;
  74. } else {
  75. if (seg_to > box_end || seg_from < box_begin)
  76. return false;
  77. real_t length = seg_to - seg_from;
  78. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  79. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  80. csign = 1.0;
  81. }
  82. if (cmin > min) {
  83. min = cmin;
  84. axis = i;
  85. sign = csign;
  86. }
  87. if (cmax < max)
  88. max = cmax;
  89. if (max < min)
  90. return false;
  91. }
  92. Vector2 rel = p_to - p_from;
  93. if (r_normal) {
  94. Vector2 normal;
  95. normal[axis] = sign;
  96. *r_normal = normal;
  97. }
  98. if (r_position)
  99. *r_position = p_from + rel * min;
  100. return true;
  101. }
  102. bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
  103. //SAT intersection between local and transformed rect2
  104. Vector2 xf_points[4] = {
  105. p_xform.xform(p_rect.position),
  106. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
  107. p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  108. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  109. };
  110. real_t low_limit;
  111. //base rect2 first (faster)
  112. if (xf_points[0].y > position.y)
  113. goto next1;
  114. if (xf_points[1].y > position.y)
  115. goto next1;
  116. if (xf_points[2].y > position.y)
  117. goto next1;
  118. if (xf_points[3].y > position.y)
  119. goto next1;
  120. return false;
  121. next1:
  122. low_limit = position.y + size.y;
  123. if (xf_points[0].y < low_limit)
  124. goto next2;
  125. if (xf_points[1].y < low_limit)
  126. goto next2;
  127. if (xf_points[2].y < low_limit)
  128. goto next2;
  129. if (xf_points[3].y < low_limit)
  130. goto next2;
  131. return false;
  132. next2:
  133. if (xf_points[0].x > position.x)
  134. goto next3;
  135. if (xf_points[1].x > position.x)
  136. goto next3;
  137. if (xf_points[2].x > position.x)
  138. goto next3;
  139. if (xf_points[3].x > position.x)
  140. goto next3;
  141. return false;
  142. next3:
  143. low_limit = position.x + size.x;
  144. if (xf_points[0].x < low_limit)
  145. goto next4;
  146. if (xf_points[1].x < low_limit)
  147. goto next4;
  148. if (xf_points[2].x < low_limit)
  149. goto next4;
  150. if (xf_points[3].x < low_limit)
  151. goto next4;
  152. return false;
  153. next4:
  154. Vector2 xf_points2[4] = {
  155. position,
  156. Vector2(position.x + size.x, position.y),
  157. Vector2(position.x, position.y + size.y),
  158. Vector2(position.x + size.x, position.y + size.y),
  159. };
  160. real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
  161. real_t mina = maxa;
  162. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  163. maxa = MAX(dp, maxa);
  164. mina = MIN(dp, mina);
  165. dp = p_xform.elements[0].dot(xf_points2[2]);
  166. maxa = MAX(dp, maxa);
  167. mina = MIN(dp, mina);
  168. dp = p_xform.elements[0].dot(xf_points2[3]);
  169. maxa = MAX(dp, maxa);
  170. mina = MIN(dp, mina);
  171. real_t maxb = p_xform.elements[0].dot(xf_points[0]);
  172. real_t minb = maxb;
  173. dp = p_xform.elements[0].dot(xf_points[1]);
  174. maxb = MAX(dp, maxb);
  175. minb = MIN(dp, minb);
  176. dp = p_xform.elements[0].dot(xf_points[2]);
  177. maxb = MAX(dp, maxb);
  178. minb = MIN(dp, minb);
  179. dp = p_xform.elements[0].dot(xf_points[3]);
  180. maxb = MAX(dp, maxb);
  181. minb = MIN(dp, minb);
  182. if (mina > maxb)
  183. return false;
  184. if (minb > maxa)
  185. return false;
  186. maxa = p_xform.elements[1].dot(xf_points2[0]);
  187. mina = maxa;
  188. dp = p_xform.elements[1].dot(xf_points2[1]);
  189. maxa = MAX(dp, maxa);
  190. mina = MIN(dp, mina);
  191. dp = p_xform.elements[1].dot(xf_points2[2]);
  192. maxa = MAX(dp, maxa);
  193. mina = MIN(dp, mina);
  194. dp = p_xform.elements[1].dot(xf_points2[3]);
  195. maxa = MAX(dp, maxa);
  196. mina = MIN(dp, mina);
  197. maxb = p_xform.elements[1].dot(xf_points[0]);
  198. minb = maxb;
  199. dp = p_xform.elements[1].dot(xf_points[1]);
  200. maxb = MAX(dp, maxb);
  201. minb = MIN(dp, minb);
  202. dp = p_xform.elements[1].dot(xf_points[2]);
  203. maxb = MAX(dp, maxb);
  204. minb = MIN(dp, minb);
  205. dp = p_xform.elements[1].dot(xf_points[3]);
  206. maxb = MAX(dp, maxb);
  207. minb = MIN(dp, minb);
  208. if (mina > maxb)
  209. return false;
  210. if (minb > maxa)
  211. return false;
  212. return true;
  213. }
  214. } // namespace godot