math_2d.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*************************************************************************/
  2. /* math_2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 snapped(const Vector2 &p_by) const;
  129. real_t aspect() const { return width / height; }
  130. operator String() const { return String::num(x) + ", " + String::num(y); }
  131. _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
  132. x = p_x;
  133. y = p_y;
  134. }
  135. _FORCE_INLINE_ Vector2() {
  136. x = 0;
  137. y = 0;
  138. }
  139. };
  140. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  141. return p_vec - *this * (dot(p_vec) - p_d);
  142. }
  143. _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  144. return p_vec * p_scalar;
  145. }
  146. Vector2 Vector2::linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  147. Vector2 res = *this;
  148. res.x += (p_t * (p_b.x - x));
  149. res.y += (p_t * (p_b.y - y));
  150. return res;
  151. }
  152. Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  153. Vector2 res = p_a;
  154. res.x += (p_t * (p_b.x - p_a.x));
  155. res.y += (p_t * (p_b.y - p_a.y));
  156. return res;
  157. }
  158. typedef Vector2 Size2;
  159. typedef Vector2 Point2;
  160. struct Transform2D;
  161. struct Rect2 {
  162. Point2 position;
  163. Size2 size;
  164. const Vector2 &get_position() const { return position; }
  165. void set_position(const Vector2 &p_pos) { position = p_pos; }
  166. const Vector2 &get_size() const { return size; }
  167. void set_size(const Vector2 &p_size) { size = p_size; }
  168. real_t get_area() const { return size.width * size.height; }
  169. inline bool intersects(const Rect2 &p_rect) const {
  170. if (position.x >= (p_rect.position.x + p_rect.size.width))
  171. return false;
  172. if ((position.x + size.width) <= p_rect.position.x)
  173. return false;
  174. if (position.y >= (p_rect.position.y + p_rect.size.height))
  175. return false;
  176. if ((position.y + size.height) <= p_rect.position.y)
  177. return false;
  178. return true;
  179. }
  180. inline real_t distance_to(const Vector2 &p_point) const {
  181. real_t dist = 1e20;
  182. if (p_point.x < position.x) {
  183. dist = MIN(dist, position.x - p_point.x);
  184. }
  185. if (p_point.y < position.y) {
  186. dist = MIN(dist, position.y - p_point.y);
  187. }
  188. if (p_point.x >= (position.x + size.x)) {
  189. dist = MIN(p_point.x - (position.x + size.x), dist);
  190. }
  191. if (p_point.y >= (position.y + size.y)) {
  192. dist = MIN(p_point.y - (position.y + size.y), dist);
  193. }
  194. if (dist == 1e20)
  195. return 0;
  196. else
  197. return dist;
  198. }
  199. _FORCE_INLINE_ bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  200. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = NULL, Point2 *r_normal = NULL) const;
  201. inline bool encloses(const Rect2 &p_rect) const {
  202. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  203. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  204. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  205. }
  206. inline bool has_no_area() const {
  207. return (size.x <= 0 || size.y <= 0);
  208. }
  209. inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect
  210. Rect2 new_rect = p_rect;
  211. if (!intersects(new_rect))
  212. return Rect2();
  213. new_rect.position.x = MAX(p_rect.position.x, position.x);
  214. new_rect.position.y = MAX(p_rect.position.y, position.y);
  215. Point2 p_rect_end = p_rect.position + p_rect.size;
  216. Point2 end = position + size;
  217. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  218. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  219. return new_rect;
  220. }
  221. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  222. Rect2 new_rect;
  223. new_rect.position.x = MIN(p_rect.position.x, position.x);
  224. new_rect.position.y = MIN(p_rect.position.y, position.y);
  225. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  226. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  227. new_rect.size = new_rect.size - new_rect.position; //make relative again
  228. return new_rect;
  229. };
  230. inline bool has_point(const Point2 &p_point) const {
  231. if (p_point.x < position.x)
  232. return false;
  233. if (p_point.y < position.y)
  234. return false;
  235. if (p_point.x >= (position.x + size.x))
  236. return false;
  237. if (p_point.y >= (position.y + size.y))
  238. return false;
  239. return true;
  240. }
  241. inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
  242. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  243. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  244. inline Rect2 grow(real_t p_by) const {
  245. Rect2 g = *this;
  246. g.position.x -= p_by;
  247. g.position.y -= p_by;
  248. g.size.width += p_by * 2;
  249. g.size.height += p_by * 2;
  250. return g;
  251. }
  252. inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const {
  253. Rect2 g = *this;
  254. g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  255. (MARGIN_TOP == p_margin) ? p_amount : 0,
  256. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  257. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  258. return g;
  259. }
  260. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  261. Rect2 g = *this;
  262. g.position.x -= p_left;
  263. g.position.y -= p_top;
  264. g.size.width += p_left + p_right;
  265. g.size.height += p_top + p_bottom;
  266. return g;
  267. }
  268. inline Rect2 expand(const Vector2 &p_vector) const {
  269. Rect2 r = *this;
  270. r.expand_to(p_vector);
  271. return r;
  272. }
  273. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  274. Vector2 begin = position;
  275. Vector2 end = position + size;
  276. if (p_vector.x < begin.x)
  277. begin.x = p_vector.x;
  278. if (p_vector.y < begin.y)
  279. begin.y = p_vector.y;
  280. if (p_vector.x > end.x)
  281. end.x = p_vector.x;
  282. if (p_vector.y > end.y)
  283. end.y = p_vector.y;
  284. position = begin;
  285. size = end - begin;
  286. }
  287. operator String() const { return String(position) + ", " + String(size); }
  288. Rect2() {}
  289. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height)
  290. : position(Point2(p_x, p_y)),
  291. size(Size2(p_width, p_height)) {
  292. }
  293. Rect2(const Point2 &p_pos, const Size2 &p_size)
  294. : position(p_pos),
  295. size(p_size) {
  296. }
  297. };
  298. /* INTEGER STUFF */
  299. struct Point2i {
  300. union {
  301. int x;
  302. int width;
  303. };
  304. union {
  305. int y;
  306. int height;
  307. };
  308. _FORCE_INLINE_ int &operator[](int p_idx) {
  309. return p_idx ? y : x;
  310. }
  311. _FORCE_INLINE_ const int &operator[](int p_idx) const {
  312. return p_idx ? y : x;
  313. }
  314. Point2i operator+(const Point2i &p_v) const;
  315. void operator+=(const Point2i &p_v);
  316. Point2i operator-(const Point2i &p_v) const;
  317. void operator-=(const Point2i &p_v);
  318. Point2i operator*(const Point2i &p_v1) const;
  319. Point2i operator*(const int &rvalue) const;
  320. void operator*=(const int &rvalue);
  321. Point2i operator/(const Point2i &p_v1) const;
  322. Point2i operator/(const int &rvalue) const;
  323. void operator/=(const int &rvalue);
  324. Point2i operator-() const;
  325. bool operator<(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  326. bool operator>(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  327. bool operator==(const Point2i &p_vec2) const;
  328. bool operator!=(const Point2i &p_vec2) const;
  329. real_t get_aspect() const { return width / (real_t)height; }
  330. operator String() const { return String::num(x) + ", " + String::num(y); }
  331. operator Vector2() const { return Vector2(x, y); }
  332. inline Point2i(const Vector2 &p_vec2) {
  333. x = (int)p_vec2.x;
  334. y = (int)p_vec2.y;
  335. }
  336. inline Point2i(int p_x, int p_y) {
  337. x = p_x;
  338. y = p_y;
  339. }
  340. inline Point2i() {
  341. x = 0;
  342. y = 0;
  343. }
  344. };
  345. typedef Point2i Size2i;
  346. struct Rect2i {
  347. Point2i position;
  348. Size2i size;
  349. const Point2i &get_position() const { return position; }
  350. void set_position(const Point2i &p_pos) { position = p_pos; }
  351. const Point2i &get_size() const { return size; }
  352. void set_size(const Point2i &p_size) { size = p_size; }
  353. int get_area() const { return size.width * size.height; }
  354. inline bool intersects(const Rect2i &p_rect) const {
  355. if (position.x > (p_rect.position.x + p_rect.size.width))
  356. return false;
  357. if ((position.x + size.width) < p_rect.position.x)
  358. return false;
  359. if (position.y > (p_rect.position.y + p_rect.size.height))
  360. return false;
  361. if ((position.y + size.height) < p_rect.position.y)
  362. return false;
  363. return true;
  364. }
  365. inline bool encloses(const Rect2i &p_rect) const {
  366. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  367. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  368. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  369. }
  370. inline bool has_no_area() const {
  371. return (size.x <= 0 || size.y <= 0);
  372. }
  373. inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
  374. Rect2i new_rect = p_rect;
  375. if (!intersects(new_rect))
  376. return Rect2i();
  377. new_rect.position.x = MAX(p_rect.position.x, position.x);
  378. new_rect.position.y = MAX(p_rect.position.y, position.y);
  379. Point2 p_rect_end = p_rect.position + p_rect.size;
  380. Point2 end = position + size;
  381. new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.position.x);
  382. new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.position.y);
  383. return new_rect;
  384. }
  385. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  386. Rect2i new_rect;
  387. new_rect.position.x = MIN(p_rect.position.x, position.x);
  388. new_rect.position.y = MIN(p_rect.position.y, position.y);
  389. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  390. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  391. new_rect.size = new_rect.size - new_rect.position; //make relative again
  392. return new_rect;
  393. };
  394. bool has_point(const Point2 &p_point) const {
  395. if (p_point.x < position.x)
  396. return false;
  397. if (p_point.y < position.y)
  398. return false;
  399. if (p_point.x >= (position.x + size.x))
  400. return false;
  401. if (p_point.y >= (position.y + size.y))
  402. return false;
  403. return true;
  404. }
  405. bool no_area() { return (size.width <= 0 || size.height <= 0); }
  406. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  407. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  408. Rect2i grow(int p_by) const {
  409. Rect2i g = *this;
  410. g.position.x -= p_by;
  411. g.position.y -= p_by;
  412. g.size.width += p_by * 2;
  413. g.size.height += p_by * 2;
  414. return g;
  415. }
  416. inline void expand_to(const Point2i &p_vector) {
  417. Point2i begin = position;
  418. Point2i end = position + size;
  419. if (p_vector.x < begin.x)
  420. begin.x = p_vector.x;
  421. if (p_vector.y < begin.y)
  422. begin.y = p_vector.y;
  423. if (p_vector.x > end.x)
  424. end.x = p_vector.x;
  425. if (p_vector.y > end.y)
  426. end.y = p_vector.y;
  427. position = begin;
  428. size = end - begin;
  429. }
  430. operator String() const { return String(position) + ", " + String(size); }
  431. operator Rect2() const { return Rect2(position, size); }
  432. Rect2i(const Rect2 &p_r2)
  433. : position(p_r2.position),
  434. size(p_r2.size) {
  435. }
  436. Rect2i() {}
  437. Rect2i(int p_x, int p_y, int p_width, int p_height)
  438. : position(Point2(p_x, p_y)),
  439. size(Size2(p_width, p_height)) {
  440. }
  441. Rect2i(const Point2 &p_pos, const Size2 &p_size)
  442. : position(p_pos),
  443. size(p_size) {
  444. }
  445. };
  446. struct Transform2D {
  447. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  448. // M = (elements[0][0] elements[1][0])
  449. // (elements[0][1] elements[1][1])
  450. // 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].
  451. // 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.
  452. // This requires additional care when working with explicit indices.
  453. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  454. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  455. // and angle is measure from +X to +Y in a clockwise-fashion.
  456. Vector2 elements[3];
  457. _FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  458. _FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  459. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  460. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  461. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const {
  462. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  463. return elements[p_axis];
  464. }
  465. _FORCE_INLINE_ void set_axis(int p_axis, const Vector2 &p_vec) {
  466. ERR_FAIL_INDEX(p_axis, 3);
  467. elements[p_axis] = p_vec;
  468. }
  469. void invert();
  470. Transform2D inverse() const;
  471. void affine_invert();
  472. Transform2D affine_inverse() const;
  473. void set_rotation(real_t p_rot);
  474. real_t get_rotation() const;
  475. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  476. void rotate(real_t p_phi);
  477. void scale(const Size2 &p_scale);
  478. void scale_basis(const Size2 &p_scale);
  479. void translate(real_t p_tx, real_t p_ty);
  480. void translate(const Vector2 &p_translation);
  481. real_t basis_determinant() const;
  482. Size2 get_scale() const;
  483. _FORCE_INLINE_ const Vector2 &get_origin() const { return elements[2]; }
  484. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  485. Transform2D scaled(const Size2 &p_scale) const;
  486. Transform2D basis_scaled(const Size2 &p_scale) const;
  487. Transform2D translated(const Vector2 &p_offset) const;
  488. Transform2D rotated(real_t p_phi) const;
  489. Transform2D untranslated() const;
  490. void orthonormalize();
  491. Transform2D orthonormalized() const;
  492. bool operator==(const Transform2D &p_transform) const;
  493. bool operator!=(const Transform2D &p_transform) const;
  494. void operator*=(const Transform2D &p_transform);
  495. Transform2D operator*(const Transform2D &p_transform) const;
  496. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  497. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  498. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  499. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  500. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  501. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
  502. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
  503. operator String() const;
  504. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  505. elements[0][0] = xx;
  506. elements[0][1] = xy;
  507. elements[1][0] = yx;
  508. elements[1][1] = yy;
  509. elements[2][0] = ox;
  510. elements[2][1] = oy;
  511. }
  512. Transform2D(real_t p_rot, const Vector2 &p_pos);
  513. Transform2D() {
  514. elements[0][0] = 1.0;
  515. elements[1][1] = 1.0;
  516. }
  517. };
  518. bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
  519. //SAT intersection between local and transformed rect2
  520. Vector2 xf_points[4] = {
  521. p_xform.xform(p_rect.position),
  522. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
  523. p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  524. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  525. };
  526. real_t low_limit;
  527. //base rect2 first (faster)
  528. if (xf_points[0].y > position.y)
  529. goto next1;
  530. if (xf_points[1].y > position.y)
  531. goto next1;
  532. if (xf_points[2].y > position.y)
  533. goto next1;
  534. if (xf_points[3].y > position.y)
  535. goto next1;
  536. return false;
  537. next1:
  538. low_limit = position.y + size.y;
  539. if (xf_points[0].y < low_limit)
  540. goto next2;
  541. if (xf_points[1].y < low_limit)
  542. goto next2;
  543. if (xf_points[2].y < low_limit)
  544. goto next2;
  545. if (xf_points[3].y < low_limit)
  546. goto next2;
  547. return false;
  548. next2:
  549. if (xf_points[0].x > position.x)
  550. goto next3;
  551. if (xf_points[1].x > position.x)
  552. goto next3;
  553. if (xf_points[2].x > position.x)
  554. goto next3;
  555. if (xf_points[3].x > position.x)
  556. goto next3;
  557. return false;
  558. next3:
  559. low_limit = position.x + size.x;
  560. if (xf_points[0].x < low_limit)
  561. goto next4;
  562. if (xf_points[1].x < low_limit)
  563. goto next4;
  564. if (xf_points[2].x < low_limit)
  565. goto next4;
  566. if (xf_points[3].x < low_limit)
  567. goto next4;
  568. return false;
  569. next4:
  570. Vector2 xf_points2[4] = {
  571. position,
  572. Vector2(position.x + size.x, position.y),
  573. Vector2(position.x, position.y + size.y),
  574. Vector2(position.x + size.x, position.y + size.y),
  575. };
  576. real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
  577. real_t mina = maxa;
  578. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  579. maxa = MAX(dp, maxa);
  580. mina = MIN(dp, mina);
  581. dp = p_xform.elements[0].dot(xf_points2[2]);
  582. maxa = MAX(dp, maxa);
  583. mina = MIN(dp, mina);
  584. dp = p_xform.elements[0].dot(xf_points2[3]);
  585. maxa = MAX(dp, maxa);
  586. mina = MIN(dp, mina);
  587. real_t maxb = p_xform.elements[0].dot(xf_points[0]);
  588. real_t minb = maxb;
  589. dp = p_xform.elements[0].dot(xf_points[1]);
  590. maxb = MAX(dp, maxb);
  591. minb = MIN(dp, minb);
  592. dp = p_xform.elements[0].dot(xf_points[2]);
  593. maxb = MAX(dp, maxb);
  594. minb = MIN(dp, minb);
  595. dp = p_xform.elements[0].dot(xf_points[3]);
  596. maxb = MAX(dp, maxb);
  597. minb = MIN(dp, minb);
  598. if (mina > maxb)
  599. return false;
  600. if (minb > maxa)
  601. return false;
  602. maxa = p_xform.elements[1].dot(xf_points2[0]);
  603. mina = maxa;
  604. dp = p_xform.elements[1].dot(xf_points2[1]);
  605. maxa = MAX(dp, maxa);
  606. mina = MIN(dp, mina);
  607. dp = p_xform.elements[1].dot(xf_points2[2]);
  608. maxa = MAX(dp, maxa);
  609. mina = MIN(dp, mina);
  610. dp = p_xform.elements[1].dot(xf_points2[3]);
  611. maxa = MAX(dp, maxa);
  612. mina = MIN(dp, mina);
  613. maxb = p_xform.elements[1].dot(xf_points[0]);
  614. minb = maxb;
  615. dp = p_xform.elements[1].dot(xf_points[1]);
  616. maxb = MAX(dp, maxb);
  617. minb = MIN(dp, minb);
  618. dp = p_xform.elements[1].dot(xf_points[2]);
  619. maxb = MAX(dp, maxb);
  620. minb = MIN(dp, minb);
  621. dp = p_xform.elements[1].dot(xf_points[3]);
  622. maxb = MAX(dp, maxb);
  623. minb = MIN(dp, minb);
  624. if (mina > maxb)
  625. return false;
  626. if (minb > maxa)
  627. return false;
  628. return true;
  629. }
  630. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  631. return Vector2(
  632. tdotx(p_vec),
  633. tdoty(p_vec));
  634. }
  635. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  636. return Vector2(
  637. elements[0].dot(p_vec),
  638. elements[1].dot(p_vec));
  639. }
  640. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  641. return Vector2(
  642. tdotx(p_vec),
  643. tdoty(p_vec)) +
  644. elements[2];
  645. }
  646. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  647. Vector2 v = p_vec - elements[2];
  648. return Vector2(
  649. elements[0].dot(v),
  650. elements[1].dot(v));
  651. }
  652. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  653. Vector2 x = elements[0] * p_rect.size.x;
  654. Vector2 y = elements[1] * p_rect.size.y;
  655. Vector2 pos = xform(p_rect.position);
  656. Rect2 new_rect;
  657. new_rect.position = pos;
  658. new_rect.expand_to(pos + x);
  659. new_rect.expand_to(pos + y);
  660. new_rect.expand_to(pos + x + y);
  661. return new_rect;
  662. }
  663. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  664. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  665. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  666. elements[1][0] = -Math::sin(p_rot) * p_scale.y;
  667. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  668. }
  669. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  670. Vector2 ends[4] = {
  671. xform_inv(p_rect.position),
  672. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  673. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  674. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  675. };
  676. Rect2 new_rect;
  677. new_rect.position = ends[0];
  678. new_rect.expand_to(ends[1]);
  679. new_rect.expand_to(ends[2]);
  680. new_rect.expand_to(ends[3]);
  681. return new_rect;
  682. }
  683. #endif