math_2d.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*************************************************************************/
  2. /* math_2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 MATH_2D_H
  31. #define MATH_2D_H
  32. #include "math_funcs.h"
  33. #include "ustring.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. enum Margin {
  38. MARGIN_LEFT,
  39. MARGIN_TOP,
  40. MARGIN_RIGHT,
  41. MARGIN_BOTTOM
  42. };
  43. enum Corner {
  44. CORNER_TOP_LEFT,
  45. CORNER_TOP_RIGHT,
  46. CORNER_BOTTOM_RIGHT,
  47. CORNER_BOTTOM_LEFT
  48. };
  49. enum Orientation {
  50. HORIZONTAL,
  51. VERTICAL
  52. };
  53. enum HAlign {
  54. HALIGN_LEFT,
  55. HALIGN_CENTER,
  56. HALIGN_RIGHT
  57. };
  58. enum VAlign {
  59. VALIGN_TOP,
  60. VALIGN_CENTER,
  61. VALIGN_BOTTOM
  62. };
  63. struct Vector2 {
  64. union {
  65. real_t x;
  66. real_t width;
  67. };
  68. union {
  69. real_t y;
  70. real_t height;
  71. };
  72. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  73. return p_idx ? y : x;
  74. }
  75. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  76. return p_idx ? y : x;
  77. }
  78. void normalize();
  79. Vector2 normalized() const;
  80. bool is_normalized() const;
  81. real_t length() const;
  82. real_t length_squared() const;
  83. real_t distance_to(const Vector2 &p_vector2) const;
  84. real_t distance_squared_to(const Vector2 &p_vector2) const;
  85. real_t angle_to(const Vector2 &p_vector2) const;
  86. real_t angle_to_point(const Vector2 &p_vector2) const;
  87. real_t dot(const Vector2 &p_other) const;
  88. real_t cross(const Vector2 &p_other) const;
  89. Vector2 cross(real_t p_other) const;
  90. Vector2 project(const Vector2 &p_vec) const;
  91. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  92. Vector2 clamped(real_t p_len) const;
  93. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t);
  94. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const;
  95. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
  96. Vector2 slide(const Vector2 &p_normal) const;
  97. Vector2 bounce(const Vector2 &p_normal) const;
  98. Vector2 reflect(const Vector2 &p_normal) const;
  99. Vector2 operator+(const Vector2 &p_v) const;
  100. void operator+=(const Vector2 &p_v);
  101. Vector2 operator-(const Vector2 &p_v) const;
  102. void operator-=(const Vector2 &p_v);
  103. Vector2 operator*(const Vector2 &p_v1) const;
  104. Vector2 operator*(const real_t &rvalue) const;
  105. void operator*=(const real_t &rvalue);
  106. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  107. Vector2 operator/(const Vector2 &p_v1) const;
  108. Vector2 operator/(const real_t &rvalue) const;
  109. void operator/=(const real_t &rvalue);
  110. Vector2 operator-() const;
  111. bool operator==(const Vector2 &p_vec2) const;
  112. bool operator!=(const Vector2 &p_vec2) const;
  113. bool operator<(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  114. bool operator<=(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y <= p_vec2.y) : (x <= p_vec2.x); }
  115. real_t angle() const;
  116. void set_rotation(real_t p_radians) {
  117. x = Math::cos(p_radians);
  118. y = Math::sin(p_radians);
  119. }
  120. _FORCE_INLINE_ Vector2 abs() const {
  121. return Vector2(Math::abs(x), Math::abs(y));
  122. }
  123. Vector2 rotated(real_t p_by) const;
  124. Vector2 tangent() const {
  125. return Vector2(y, -x);
  126. }
  127. Vector2 floor() const;
  128. Vector2 ceil() const;
  129. Vector2 round() const;
  130. Vector2 snapped(const Vector2 &p_by) const;
  131. real_t aspect() const { return width / height; }
  132. operator String() const { return String::num(x) + ", " + String::num(y); }
  133. _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
  134. x = p_x;
  135. y = p_y;
  136. }
  137. _FORCE_INLINE_ Vector2() {
  138. x = 0;
  139. y = 0;
  140. }
  141. };
  142. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  143. return p_vec - *this * (dot(p_vec) - p_d);
  144. }
  145. _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  146. return p_vec * p_scalar;
  147. }
  148. _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
  149. return Vector2(x + p_v.x, y + p_v.y);
  150. }
  151. _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
  152. x += p_v.x;
  153. y += p_v.y;
  154. }
  155. _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
  156. return Vector2(x - p_v.x, y - p_v.y);
  157. }
  158. _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
  159. x -= p_v.x;
  160. y -= p_v.y;
  161. }
  162. _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  163. return Vector2(x * p_v1.x, y * p_v1.y);
  164. };
  165. _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
  166. return Vector2(x * rvalue, y * rvalue);
  167. };
  168. _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
  169. x *= rvalue;
  170. y *= rvalue;
  171. };
  172. _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  173. return Vector2(x / p_v1.x, y / p_v1.y);
  174. };
  175. _FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
  176. return Vector2(x / rvalue, y / rvalue);
  177. };
  178. _FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
  179. x /= rvalue;
  180. y /= rvalue;
  181. };
  182. _FORCE_INLINE_ Vector2 Vector2::operator-() const {
  183. return Vector2(-x, -y);
  184. }
  185. _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
  186. return x == p_vec2.x && y == p_vec2.y;
  187. }
  188. _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
  189. return x != p_vec2.x || y != p_vec2.y;
  190. }
  191. Vector2 Vector2::linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  192. Vector2 res = *this;
  193. res.x += (p_t * (p_b.x - x));
  194. res.y += (p_t * (p_b.y - y));
  195. return res;
  196. }
  197. Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  198. Vector2 res = p_a;
  199. res.x += (p_t * (p_b.x - p_a.x));
  200. res.y += (p_t * (p_b.y - p_a.y));
  201. return res;
  202. }
  203. typedef Vector2 Size2;
  204. typedef Vector2 Point2;
  205. struct Transform2D;
  206. struct Rect2 {
  207. Point2 position;
  208. Size2 size;
  209. const Vector2 &get_position() const { return position; }
  210. void set_position(const Vector2 &p_pos) { position = p_pos; }
  211. const Vector2 &get_size() const { return size; }
  212. void set_size(const Vector2 &p_size) { size = p_size; }
  213. real_t get_area() const { return size.width * size.height; }
  214. inline bool intersects(const Rect2 &p_rect) const {
  215. if (position.x >= (p_rect.position.x + p_rect.size.width))
  216. return false;
  217. if ((position.x + size.width) <= p_rect.position.x)
  218. return false;
  219. if (position.y >= (p_rect.position.y + p_rect.size.height))
  220. return false;
  221. if ((position.y + size.height) <= p_rect.position.y)
  222. return false;
  223. return true;
  224. }
  225. inline real_t distance_to(const Vector2 &p_point) const {
  226. real_t dist = 0.0;
  227. bool inside = true;
  228. if (p_point.x < position.x) {
  229. real_t d = position.x - p_point.x;
  230. dist = inside ? d : MIN(dist, d);
  231. inside = false;
  232. }
  233. if (p_point.y < position.y) {
  234. real_t d = position.y - p_point.y;
  235. dist = inside ? d : MIN(dist, d);
  236. inside = false;
  237. }
  238. if (p_point.x >= (position.x + size.x)) {
  239. real_t d = p_point.x - (position.x + size.x);
  240. dist = inside ? d : MIN(dist, d);
  241. inside = false;
  242. }
  243. if (p_point.y >= (position.y + size.y)) {
  244. real_t d = p_point.y - (position.y + size.y);
  245. dist = inside ? d : MIN(dist, d);
  246. inside = false;
  247. }
  248. if (inside)
  249. return 0;
  250. else
  251. return dist;
  252. }
  253. _FORCE_INLINE_ bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  254. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = NULL, Point2 *r_normal = NULL) const;
  255. inline bool encloses(const Rect2 &p_rect) const {
  256. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  257. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  258. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  259. }
  260. inline bool has_no_area() const {
  261. return (size.x <= 0 || size.y <= 0);
  262. }
  263. inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect
  264. Rect2 new_rect = p_rect;
  265. if (!intersects(new_rect))
  266. return Rect2();
  267. new_rect.position.x = MAX(p_rect.position.x, position.x);
  268. new_rect.position.y = MAX(p_rect.position.y, position.y);
  269. Point2 p_rect_end = p_rect.position + p_rect.size;
  270. Point2 end = position + size;
  271. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  272. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  273. return new_rect;
  274. }
  275. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  276. Rect2 new_rect;
  277. new_rect.position.x = MIN(p_rect.position.x, position.x);
  278. new_rect.position.y = MIN(p_rect.position.y, position.y);
  279. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  280. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  281. new_rect.size = new_rect.size - new_rect.position; //make relative again
  282. return new_rect;
  283. };
  284. inline bool has_point(const Point2 &p_point) const {
  285. if (p_point.x < position.x)
  286. return false;
  287. if (p_point.y < position.y)
  288. return false;
  289. if (p_point.x >= (position.x + size.x))
  290. return false;
  291. if (p_point.y >= (position.y + size.y))
  292. return false;
  293. return true;
  294. }
  295. inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
  296. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  297. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  298. inline Rect2 grow(real_t p_by) const {
  299. Rect2 g = *this;
  300. g.position.x -= p_by;
  301. g.position.y -= p_by;
  302. g.size.width += p_by * 2;
  303. g.size.height += p_by * 2;
  304. return g;
  305. }
  306. inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const {
  307. Rect2 g = *this;
  308. g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  309. (MARGIN_TOP == p_margin) ? p_amount : 0,
  310. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  311. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  312. return g;
  313. }
  314. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  315. Rect2 g = *this;
  316. g.position.x -= p_left;
  317. g.position.y -= p_top;
  318. g.size.width += p_left + p_right;
  319. g.size.height += p_top + p_bottom;
  320. return g;
  321. }
  322. inline Rect2 expand(const Vector2 &p_vector) const {
  323. Rect2 r = *this;
  324. r.expand_to(p_vector);
  325. return r;
  326. }
  327. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  328. Vector2 begin = position;
  329. Vector2 end = position + size;
  330. if (p_vector.x < begin.x)
  331. begin.x = p_vector.x;
  332. if (p_vector.y < begin.y)
  333. begin.y = p_vector.y;
  334. if (p_vector.x > end.x)
  335. end.x = p_vector.x;
  336. if (p_vector.y > end.y)
  337. end.y = p_vector.y;
  338. position = begin;
  339. size = end - begin;
  340. }
  341. inline Rect2 abs() const {
  342. return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
  343. }
  344. operator String() const { return String(position) + ", " + String(size); }
  345. Rect2() {}
  346. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  347. position(Point2(p_x, p_y)),
  348. size(Size2(p_width, p_height)) {
  349. }
  350. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  351. position(p_pos),
  352. size(p_size) {
  353. }
  354. };
  355. /* INTEGER STUFF */
  356. struct Point2i {
  357. union {
  358. int x;
  359. int width;
  360. };
  361. union {
  362. int y;
  363. int height;
  364. };
  365. _FORCE_INLINE_ int &operator[](int p_idx) {
  366. return p_idx ? y : x;
  367. }
  368. _FORCE_INLINE_ const int &operator[](int p_idx) const {
  369. return p_idx ? y : x;
  370. }
  371. Point2i operator+(const Point2i &p_v) const;
  372. void operator+=(const Point2i &p_v);
  373. Point2i operator-(const Point2i &p_v) const;
  374. void operator-=(const Point2i &p_v);
  375. Point2i operator*(const Point2i &p_v1) const;
  376. Point2i operator*(const int &rvalue) const;
  377. void operator*=(const int &rvalue);
  378. Point2i operator/(const Point2i &p_v1) const;
  379. Point2i operator/(const int &rvalue) const;
  380. void operator/=(const int &rvalue);
  381. Point2i operator-() const;
  382. bool operator<(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  383. bool operator>(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  384. bool operator==(const Point2i &p_vec2) const;
  385. bool operator!=(const Point2i &p_vec2) const;
  386. real_t get_aspect() const { return width / (real_t)height; }
  387. operator String() const { return String::num(x) + ", " + String::num(y); }
  388. operator Vector2() const { return Vector2(x, y); }
  389. inline Point2i(const Vector2 &p_vec2) {
  390. x = (int)p_vec2.x;
  391. y = (int)p_vec2.y;
  392. }
  393. inline Point2i(int p_x, int p_y) {
  394. x = p_x;
  395. y = p_y;
  396. }
  397. inline Point2i() {
  398. x = 0;
  399. y = 0;
  400. }
  401. };
  402. typedef Point2i Size2i;
  403. struct Rect2i {
  404. Point2i position;
  405. Size2i size;
  406. const Point2i &get_position() const { return position; }
  407. void set_position(const Point2i &p_pos) { position = p_pos; }
  408. const Point2i &get_size() const { return size; }
  409. void set_size(const Point2i &p_size) { size = p_size; }
  410. int get_area() const { return size.width * size.height; }
  411. inline bool intersects(const Rect2i &p_rect) const {
  412. if (position.x > (p_rect.position.x + p_rect.size.width))
  413. return false;
  414. if ((position.x + size.width) < p_rect.position.x)
  415. return false;
  416. if (position.y > (p_rect.position.y + p_rect.size.height))
  417. return false;
  418. if ((position.y + size.height) < p_rect.position.y)
  419. return false;
  420. return true;
  421. }
  422. inline bool encloses(const Rect2i &p_rect) const {
  423. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  424. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  425. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  426. }
  427. inline bool has_no_area() const {
  428. return (size.x <= 0 || size.y <= 0);
  429. }
  430. inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
  431. Rect2i new_rect = p_rect;
  432. if (!intersects(new_rect))
  433. return Rect2i();
  434. new_rect.position.x = MAX(p_rect.position.x, position.x);
  435. new_rect.position.y = MAX(p_rect.position.y, position.y);
  436. Point2 p_rect_end = p_rect.position + p_rect.size;
  437. Point2 end = position + size;
  438. new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.position.x);
  439. new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.position.y);
  440. return new_rect;
  441. }
  442. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  443. Rect2i new_rect;
  444. new_rect.position.x = MIN(p_rect.position.x, position.x);
  445. new_rect.position.y = MIN(p_rect.position.y, position.y);
  446. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  447. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  448. new_rect.size = new_rect.size - new_rect.position; //make relative again
  449. return new_rect;
  450. };
  451. bool has_point(const Point2 &p_point) const {
  452. if (p_point.x < position.x)
  453. return false;
  454. if (p_point.y < position.y)
  455. return false;
  456. if (p_point.x >= (position.x + size.x))
  457. return false;
  458. if (p_point.y >= (position.y + size.y))
  459. return false;
  460. return true;
  461. }
  462. bool no_area() { return (size.width <= 0 || size.height <= 0); }
  463. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  464. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  465. Rect2i grow(int p_by) const {
  466. Rect2i g = *this;
  467. g.position.x -= p_by;
  468. g.position.y -= p_by;
  469. g.size.width += p_by * 2;
  470. g.size.height += p_by * 2;
  471. return g;
  472. }
  473. inline void expand_to(const Point2i &p_vector) {
  474. Point2i begin = position;
  475. Point2i end = position + size;
  476. if (p_vector.x < begin.x)
  477. begin.x = p_vector.x;
  478. if (p_vector.y < begin.y)
  479. begin.y = p_vector.y;
  480. if (p_vector.x > end.x)
  481. end.x = p_vector.x;
  482. if (p_vector.y > end.y)
  483. end.y = p_vector.y;
  484. position = begin;
  485. size = end - begin;
  486. }
  487. operator String() const { return String(position) + ", " + String(size); }
  488. operator Rect2() const { return Rect2(position, size); }
  489. Rect2i(const Rect2 &p_r2) :
  490. position(p_r2.position),
  491. size(p_r2.size) {
  492. }
  493. Rect2i() {}
  494. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  495. position(Point2(p_x, p_y)),
  496. size(Size2(p_width, p_height)) {
  497. }
  498. Rect2i(const Point2 &p_pos, const Size2 &p_size) :
  499. position(p_pos),
  500. size(p_size) {
  501. }
  502. };
  503. struct Transform2D {
  504. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  505. // M = (elements[0][0] elements[1][0])
  506. // (elements[0][1] elements[1][1])
  507. // This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as elements[i].
  508. // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here.
  509. // This requires additional care when working with explicit indices.
  510. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  511. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  512. // and angle is measure from +X to +Y in a clockwise-fashion.
  513. Vector2 elements[3];
  514. _FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  515. _FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  516. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  517. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  518. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const {
  519. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  520. return elements[p_axis];
  521. }
  522. _FORCE_INLINE_ void set_axis(int p_axis, const Vector2 &p_vec) {
  523. ERR_FAIL_INDEX(p_axis, 3);
  524. elements[p_axis] = p_vec;
  525. }
  526. void invert();
  527. Transform2D inverse() const;
  528. void affine_invert();
  529. Transform2D affine_inverse() const;
  530. void set_rotation(real_t p_rot);
  531. real_t get_rotation() const;
  532. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  533. void rotate(real_t p_phi);
  534. void scale(const Size2 &p_scale);
  535. void scale_basis(const Size2 &p_scale);
  536. void translate(real_t p_tx, real_t p_ty);
  537. void translate(const Vector2 &p_translation);
  538. real_t basis_determinant() const;
  539. Size2 get_scale() const;
  540. _FORCE_INLINE_ const Vector2 &get_origin() const { return elements[2]; }
  541. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  542. Transform2D scaled(const Size2 &p_scale) const;
  543. Transform2D basis_scaled(const Size2 &p_scale) const;
  544. Transform2D translated(const Vector2 &p_offset) const;
  545. Transform2D rotated(real_t p_phi) const;
  546. Transform2D untranslated() const;
  547. void orthonormalize();
  548. Transform2D orthonormalized() const;
  549. bool operator==(const Transform2D &p_transform) const;
  550. bool operator!=(const Transform2D &p_transform) const;
  551. void operator*=(const Transform2D &p_transform);
  552. Transform2D operator*(const Transform2D &p_transform) const;
  553. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  554. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  555. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  556. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  557. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  558. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  559. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  560. operator String() const;
  561. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  562. elements[0][0] = xx;
  563. elements[0][1] = xy;
  564. elements[1][0] = yx;
  565. elements[1][1] = yy;
  566. elements[2][0] = ox;
  567. elements[2][1] = oy;
  568. }
  569. Transform2D(real_t p_rot, const Vector2 &p_pos);
  570. Transform2D() {
  571. elements[0][0] = 1.0;
  572. elements[1][1] = 1.0;
  573. }
  574. };
  575. bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
  576. //SAT intersection between local and transformed rect2
  577. Vector2 xf_points[4] = {
  578. p_xform.xform(p_rect.position),
  579. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
  580. p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  581. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  582. };
  583. real_t low_limit;
  584. //base rect2 first (faster)
  585. if (xf_points[0].y > position.y)
  586. goto next1;
  587. if (xf_points[1].y > position.y)
  588. goto next1;
  589. if (xf_points[2].y > position.y)
  590. goto next1;
  591. if (xf_points[3].y > position.y)
  592. goto next1;
  593. return false;
  594. next1:
  595. low_limit = position.y + size.y;
  596. if (xf_points[0].y < low_limit)
  597. goto next2;
  598. if (xf_points[1].y < low_limit)
  599. goto next2;
  600. if (xf_points[2].y < low_limit)
  601. goto next2;
  602. if (xf_points[3].y < low_limit)
  603. goto next2;
  604. return false;
  605. next2:
  606. if (xf_points[0].x > position.x)
  607. goto next3;
  608. if (xf_points[1].x > position.x)
  609. goto next3;
  610. if (xf_points[2].x > position.x)
  611. goto next3;
  612. if (xf_points[3].x > position.x)
  613. goto next3;
  614. return false;
  615. next3:
  616. low_limit = position.x + size.x;
  617. if (xf_points[0].x < low_limit)
  618. goto next4;
  619. if (xf_points[1].x < low_limit)
  620. goto next4;
  621. if (xf_points[2].x < low_limit)
  622. goto next4;
  623. if (xf_points[3].x < low_limit)
  624. goto next4;
  625. return false;
  626. next4:
  627. Vector2 xf_points2[4] = {
  628. position,
  629. Vector2(position.x + size.x, position.y),
  630. Vector2(position.x, position.y + size.y),
  631. Vector2(position.x + size.x, position.y + size.y),
  632. };
  633. real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
  634. real_t mina = maxa;
  635. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  636. maxa = MAX(dp, maxa);
  637. mina = MIN(dp, mina);
  638. dp = p_xform.elements[0].dot(xf_points2[2]);
  639. maxa = MAX(dp, maxa);
  640. mina = MIN(dp, mina);
  641. dp = p_xform.elements[0].dot(xf_points2[3]);
  642. maxa = MAX(dp, maxa);
  643. mina = MIN(dp, mina);
  644. real_t maxb = p_xform.elements[0].dot(xf_points[0]);
  645. real_t minb = maxb;
  646. dp = p_xform.elements[0].dot(xf_points[1]);
  647. maxb = MAX(dp, maxb);
  648. minb = MIN(dp, minb);
  649. dp = p_xform.elements[0].dot(xf_points[2]);
  650. maxb = MAX(dp, maxb);
  651. minb = MIN(dp, minb);
  652. dp = p_xform.elements[0].dot(xf_points[3]);
  653. maxb = MAX(dp, maxb);
  654. minb = MIN(dp, minb);
  655. if (mina > maxb)
  656. return false;
  657. if (minb > maxa)
  658. return false;
  659. maxa = p_xform.elements[1].dot(xf_points2[0]);
  660. mina = maxa;
  661. dp = p_xform.elements[1].dot(xf_points2[1]);
  662. maxa = MAX(dp, maxa);
  663. mina = MIN(dp, mina);
  664. dp = p_xform.elements[1].dot(xf_points2[2]);
  665. maxa = MAX(dp, maxa);
  666. mina = MIN(dp, mina);
  667. dp = p_xform.elements[1].dot(xf_points2[3]);
  668. maxa = MAX(dp, maxa);
  669. mina = MIN(dp, mina);
  670. maxb = p_xform.elements[1].dot(xf_points[0]);
  671. minb = maxb;
  672. dp = p_xform.elements[1].dot(xf_points[1]);
  673. maxb = MAX(dp, maxb);
  674. minb = MIN(dp, minb);
  675. dp = p_xform.elements[1].dot(xf_points[2]);
  676. maxb = MAX(dp, maxb);
  677. minb = MIN(dp, minb);
  678. dp = p_xform.elements[1].dot(xf_points[3]);
  679. maxb = MAX(dp, maxb);
  680. minb = MIN(dp, minb);
  681. if (mina > maxb)
  682. return false;
  683. if (minb > maxa)
  684. return false;
  685. return true;
  686. }
  687. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  688. return Vector2(
  689. tdotx(p_vec),
  690. tdoty(p_vec));
  691. }
  692. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  693. return Vector2(
  694. elements[0].dot(p_vec),
  695. elements[1].dot(p_vec));
  696. }
  697. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  698. return Vector2(
  699. tdotx(p_vec),
  700. tdoty(p_vec)) +
  701. elements[2];
  702. }
  703. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  704. Vector2 v = p_vec - elements[2];
  705. return Vector2(
  706. elements[0].dot(v),
  707. elements[1].dot(v));
  708. }
  709. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  710. Vector2 x = elements[0] * p_rect.size.x;
  711. Vector2 y = elements[1] * p_rect.size.y;
  712. Vector2 pos = xform(p_rect.position);
  713. Rect2 new_rect;
  714. new_rect.position = pos;
  715. new_rect.expand_to(pos + x);
  716. new_rect.expand_to(pos + y);
  717. new_rect.expand_to(pos + x + y);
  718. return new_rect;
  719. }
  720. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  721. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  722. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  723. elements[1][0] = -Math::sin(p_rot) * p_scale.y;
  724. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  725. }
  726. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  727. Vector2 ends[4] = {
  728. xform_inv(p_rect.position),
  729. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  730. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  731. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  732. };
  733. Rect2 new_rect;
  734. new_rect.position = ends[0];
  735. new_rect.expand_to(ends[1]);
  736. new_rect.expand_to(ends[2]);
  737. new_rect.expand_to(ends[3]);
  738. return new_rect;
  739. }
  740. #endif