math_2d.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*************************************************************************/
  2. /* math_2d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef MATH_2D_H
  30. #define MATH_2D_H
  31. #include "math_funcs.h"
  32. #include "ustring.h"
  33. /**
  34. @author Juan Linietsky <[email protected]>
  35. */
  36. enum Margin {
  37. MARGIN_LEFT,
  38. MARGIN_TOP,
  39. MARGIN_RIGHT,
  40. MARGIN_BOTTOM
  41. };
  42. enum Orientation {
  43. HORIZONTAL,
  44. VERTICAL
  45. };
  46. enum HAlign {
  47. HALIGN_LEFT,
  48. HALIGN_CENTER,
  49. HALIGN_RIGHT
  50. };
  51. enum VAlign {
  52. VALIGN_TOP,
  53. VALIGN_CENTER,
  54. VALIGN_BOTTOM
  55. };
  56. struct Vector2 {
  57. union {
  58. float x;
  59. float width;
  60. };
  61. union {
  62. float y;
  63. float height;
  64. };
  65. _FORCE_INLINE_ float& operator[](int p_idx) {
  66. return p_idx?y:x;
  67. }
  68. _FORCE_INLINE_ const float& operator[](int p_idx) const {
  69. return p_idx?y:x;
  70. }
  71. void normalize();
  72. Vector2 normalized() const;
  73. float length() const;
  74. float length_squared() const;
  75. float distance_to(const Vector2& p_vector2) const;
  76. float distance_squared_to(const Vector2& p_vector2) const;
  77. float angle_to(const Vector2& p_vector2) const;
  78. float dot(const Vector2& p_other) const;
  79. float cross(const Vector2& p_other) const;
  80. Vector2 cross(real_t p_other) const;
  81. Vector2 project(const Vector2& p_vec) const;
  82. Vector2 plane_project(real_t p_d, const Vector2& p_vec) const;
  83. Vector2 clamped(real_t p_len) const;
  84. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2& p_a, const Vector2& p_b,float p_t);
  85. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2& p_b,float p_t) const;
  86. Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const;
  87. Vector2 cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const;
  88. Vector2 slide(const Vector2& p_vec) const;
  89. Vector2 reflect(const Vector2& p_vec) const;
  90. Vector2 operator+(const Vector2& p_v) const;
  91. void operator+=(const Vector2& p_v);
  92. Vector2 operator-(const Vector2& p_v) const;
  93. void operator-=(const Vector2& p_v);
  94. Vector2 operator*(const Vector2 &p_v1) const;
  95. Vector2 operator*(const float &rvalue) const;
  96. void operator*=(const float &rvalue);
  97. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  98. Vector2 operator/(const Vector2 &p_v1) const;
  99. Vector2 operator/(const float &rvalue) const;
  100. void operator/=(const float &rvalue);
  101. Vector2 operator-() const;
  102. bool operator==(const Vector2& p_vec2) const;
  103. bool operator!=(const Vector2& p_vec2) const;
  104. bool operator<(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  105. bool operator<=(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<=p_vec2.y):(x<=p_vec2.x); }
  106. real_t atan2() const;
  107. void set_rotation(float p_radians) {
  108. x=Math::sin(p_radians);
  109. y=Math::cos(p_radians);
  110. }
  111. _FORCE_INLINE_ Vector2 abs() const {
  112. return Vector2( Math::abs(x), Math::abs(y) );
  113. }
  114. Vector2 rotated(float p_by) const;
  115. Vector2 tangent() const {
  116. return Vector2(y,-x);
  117. }
  118. Vector2 floor() const;
  119. Vector2 snapped(const Vector2& p_by) const;
  120. float get_aspect() const { return width/height; }
  121. operator String() const { return String::num(x)+","+String::num(y); }
  122. inline Vector2(float p_x,float p_y) { x=p_x; y=p_y; }
  123. inline Vector2() { x=0; y=0; }
  124. };
  125. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2& p_vec) const {
  126. return p_vec - *this * ( dot(p_vec) -p_d);
  127. }
  128. _FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2& p_vec) {
  129. return p_vec*p_scalar;
  130. }
  131. Vector2 Vector2::linear_interpolate(const Vector2& p_b,float p_t) const {
  132. Vector2 res=*this;
  133. res.x+= (p_t * (p_b.x-x));
  134. res.y+= (p_t * (p_b.y-y));
  135. return res;
  136. }
  137. Vector2 Vector2::linear_interpolate(const Vector2& p_a, const Vector2& p_b,float p_t) {
  138. Vector2 res=p_a;
  139. res.x+= (p_t * (p_b.x-p_a.x));
  140. res.y+= (p_t * (p_b.y-p_a.y));
  141. return res;
  142. }
  143. typedef Vector2 Size2;
  144. typedef Vector2 Point2;
  145. struct Rect2 {
  146. Point2 pos;
  147. Size2 size;
  148. const Vector2& get_pos() const { return pos; }
  149. void set_pos(const Vector2& p_pos) { pos=p_pos; }
  150. const Vector2& get_size() const { return size; }
  151. void set_size(const Vector2& p_size) { size=p_size; }
  152. float get_area() const { return size.width*size.height; }
  153. inline bool intersects(const Rect2& p_rect) const {
  154. if ( pos.x > (p_rect.pos.x + p_rect.size.width) )
  155. return false;
  156. if ( (pos.x+size.width) < p_rect.pos.x )
  157. return false;
  158. if ( pos.y > (p_rect.pos.y + p_rect.size.height) )
  159. return false;
  160. if ( (pos.y+size.height) < p_rect.pos.y )
  161. return false;
  162. return true;
  163. }
  164. bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const;
  165. inline bool encloses(const Rect2& p_rect) const {
  166. return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
  167. ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
  168. ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
  169. }
  170. inline bool has_no_area() const {
  171. return (size.x<=0 || size.y<=0);
  172. }
  173. inline Rect2 clip(const Rect2& p_rect) const { /// return a clipped rect
  174. Rect2 new_rect=p_rect;
  175. if (!intersects( new_rect ))
  176. return Rect2();
  177. new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
  178. new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
  179. Point2 p_rect_end=p_rect.pos+p_rect.size;
  180. Point2 end=pos+size;
  181. new_rect.size.x=MIN(p_rect_end.x,end.x) - new_rect.pos.x;
  182. new_rect.size.y=MIN(p_rect_end.y,end.y) - new_rect.pos.y;
  183. return new_rect;
  184. }
  185. inline Rect2 merge(const Rect2& p_rect) const { ///< return a merged rect
  186. Rect2 new_rect;
  187. new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
  188. new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
  189. new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
  190. new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
  191. new_rect.size = new_rect.size - new_rect.pos; //make relative again
  192. return new_rect;
  193. };
  194. bool has_point(const Point2& p_point) const {
  195. if (p_point.x < pos.x)
  196. return false;
  197. if (p_point.y < pos.y)
  198. return false;
  199. if (p_point.x >= (pos.x+size.x) )
  200. return false;
  201. if (p_point.y >= (pos.y+size.y) )
  202. return false;
  203. return true;
  204. }
  205. bool no_area() const { return (size.width<=0 || size.height<=0 ); }
  206. bool operator==(const Rect2& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
  207. bool operator!=(const Rect2& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
  208. Rect2 grow(real_t p_by) const {
  209. Rect2 g=*this;
  210. g.pos.x-=p_by;
  211. g.pos.y-=p_by;
  212. g.size.width+=p_by*2;
  213. g.size.height+=p_by*2;
  214. return g;
  215. }
  216. inline Rect2 expand(const Vector2& p_vector) const {
  217. Rect2 r = *this;
  218. r.expand_to(p_vector);
  219. return r;
  220. }
  221. inline void expand_to(const Vector2& p_vector) { //in place function for speed
  222. Vector2 begin=pos;
  223. Vector2 end=pos+size;
  224. if (p_vector.x<begin.x)
  225. begin.x=p_vector.x;
  226. if (p_vector.y<begin.y)
  227. begin.y=p_vector.y;
  228. if (p_vector.x>end.x)
  229. end.x=p_vector.x;
  230. if (p_vector.y>end.y)
  231. end.y=p_vector.y;
  232. pos=begin;
  233. size=end-begin;
  234. }
  235. operator String() const { return String(pos)+","+String(size); }
  236. Rect2() {}
  237. Rect2( float p_x, float p_y, float p_width, float p_height) { pos=Point2(p_x,p_y); size=Size2( p_width, p_height ); }
  238. Rect2( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
  239. };
  240. /* INTEGER STUFF */
  241. struct Point2i {
  242. union {
  243. int x;
  244. int width;
  245. };
  246. union {
  247. int y;
  248. int height;
  249. };
  250. _FORCE_INLINE_ int& operator[](int p_idx) {
  251. return p_idx?y:x;
  252. }
  253. _FORCE_INLINE_ const int& operator[](int p_idx) const {
  254. return p_idx?y:x;
  255. }
  256. Point2i operator+(const Point2i& p_v) const;
  257. void operator+=(const Point2i& p_v);
  258. Point2i operator-(const Point2i& p_v) const;
  259. void operator-=(const Point2i& p_v);
  260. Point2i operator*(const Point2i &p_v1) const;
  261. Point2i operator*(const int &rvalue) const;
  262. void operator*=(const int &rvalue);
  263. Point2i operator/(const Point2i &p_v1) const;
  264. Point2i operator/(const int &rvalue) const;
  265. void operator/=(const int &rvalue);
  266. Point2i operator-() const;
  267. bool operator<(const Point2i& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  268. bool operator>(const Point2i& p_vec2) const { return (x==p_vec2.x)?(y>p_vec2.y):(x>p_vec2.x); }
  269. bool operator==(const Point2i& p_vec2) const;
  270. bool operator!=(const Point2i& p_vec2) const;
  271. float get_aspect() const { return width/(float)height; }
  272. operator String() const { return String::num(x)+","+String::num(y); }
  273. operator Vector2() const { return Vector2(x,y); }
  274. inline Point2i(const Vector2& p_vec2) { x=(int)p_vec2.x; y=(int)p_vec2.y; }
  275. inline Point2i(int p_x,int p_y) { x=p_x; y=p_y; }
  276. inline Point2i() { x=0; y=0; }
  277. };
  278. typedef Point2i Size2i;
  279. struct Rect2i {
  280. Point2i pos;
  281. Size2i size;
  282. const Point2i& get_pos() const { return pos; }
  283. void set_pos(const Point2i& p_pos) { pos=p_pos; }
  284. const Point2i& get_size() const { return size; }
  285. void set_size(const Point2i& p_size) { size=p_size; }
  286. int get_area() const { return size.width*size.height; }
  287. inline bool intersects(const Rect2i& p_rect) const {
  288. if ( pos.x > (p_rect.pos.x + p_rect.size.width) )
  289. return false;
  290. if ( (pos.x+size.width) < p_rect.pos.x )
  291. return false;
  292. if ( pos.y > (p_rect.pos.y + p_rect.size.height) )
  293. return false;
  294. if ( (pos.y+size.height) < p_rect.pos.y )
  295. return false;
  296. return true;
  297. }
  298. inline bool encloses(const Rect2i& p_rect) const {
  299. return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
  300. ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
  301. ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
  302. }
  303. inline bool has_no_area() const {
  304. return (size.x<=0 || size.y<=0);
  305. }
  306. inline Rect2i clip(const Rect2i& p_rect) const { /// return a clipped rect
  307. Rect2i new_rect=p_rect;
  308. if (!intersects( new_rect ))
  309. return Rect2i();
  310. new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
  311. new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
  312. Point2 p_rect_end=p_rect.pos+p_rect.size;
  313. Point2 end=pos+size;
  314. new_rect.size.x=(int)(MIN(p_rect_end.x,end.x) - new_rect.pos.x);
  315. new_rect.size.y=(int)(MIN(p_rect_end.y,end.y) - new_rect.pos.y);
  316. return new_rect;
  317. }
  318. inline Rect2i merge(const Rect2i& p_rect) const { ///< return a merged rect
  319. Rect2i new_rect;
  320. new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
  321. new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
  322. new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
  323. new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
  324. new_rect.size = new_rect.size - new_rect.pos; //make relative again
  325. return new_rect;
  326. };
  327. bool has_point(const Point2& p_point) {
  328. if (p_point.x < pos.x)
  329. return false;
  330. if (p_point.y < pos.y)
  331. return false;
  332. if (p_point.x >= (pos.x+size.x) )
  333. return false;
  334. if (p_point.y >= (pos.y+size.y) )
  335. return false;
  336. return true;
  337. }
  338. bool no_area() { return (size.width<=0 || size.height<=0 ); }
  339. bool operator==(const Rect2i& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
  340. bool operator!=(const Rect2i& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
  341. Rect2i grow(int p_by) const {
  342. Rect2i g=*this;
  343. g.pos.x-=p_by;
  344. g.pos.y-=p_by;
  345. g.size.width+=p_by*2;
  346. g.size.height+=p_by*2;
  347. return g;
  348. }
  349. inline void expand_to(const Point2i& p_vector) {
  350. Point2i begin=pos;
  351. Point2i end=pos+size;
  352. if (p_vector.x<begin.x)
  353. begin.x=p_vector.x;
  354. if (p_vector.y<begin.y)
  355. begin.y=p_vector.y;
  356. if (p_vector.x>end.x)
  357. end.x=p_vector.x;
  358. if (p_vector.y>end.y)
  359. end.y=p_vector.y;
  360. pos=begin;
  361. size=end-begin;
  362. }
  363. operator String() const { return String(pos)+","+String(size); }
  364. operator Rect2() const { return Rect2(pos,size); }
  365. Rect2i(const Rect2& p_r2) { pos=p_r2.pos; size=p_r2.size; }
  366. Rect2i() {}
  367. Rect2i( int p_x, int p_y, int p_width, int p_height) { pos=Point2(p_x,p_y); size=Size2( p_width, p_height ); }
  368. Rect2i( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
  369. };
  370. struct Matrix32 {
  371. Vector2 elements[3];
  372. _FORCE_INLINE_ float tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  373. _FORCE_INLINE_ float tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  374. const Vector2& operator[](int p_idx) const { return elements[p_idx]; }
  375. Vector2& operator[](int p_idx) { return elements[p_idx]; }
  376. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
  377. _FORCE_INLINE_ void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
  378. void invert();
  379. Matrix32 inverse() const;
  380. void affine_invert();
  381. Matrix32 affine_inverse() const;
  382. void set_rotation(real_t p_phi);
  383. real_t get_rotation() const;
  384. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
  385. void rotate(real_t p_phi);
  386. void scale(const Vector2& p_scale);
  387. void scale_basis(const Vector2& p_scale);
  388. void translate( real_t p_tx, real_t p_ty);
  389. void translate( const Vector2& p_translation );
  390. Vector2 get_scale() const;
  391. _FORCE_INLINE_ const Vector2& get_origin() const { return elements[2]; }
  392. _FORCE_INLINE_ void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
  393. Matrix32 scaled(const Vector2& p_scale) const;
  394. Matrix32 basis_scaled(const Vector2& p_scale) const;
  395. Matrix32 translated(const Vector2& p_offset) const;
  396. Matrix32 rotated(float p_phi) const;
  397. Matrix32 untranslated() const;
  398. void orthonormalize();
  399. Matrix32 orthonormalized() const;
  400. bool operator==(const Matrix32& p_transform) const;
  401. bool operator!=(const Matrix32& p_transform) const;
  402. void operator*=(const Matrix32& p_transform);
  403. Matrix32 operator*(const Matrix32& p_transform) const;
  404. Matrix32 interpolate_with(const Matrix32& p_transform, float p_c) const;
  405. _FORCE_INLINE_ Vector2 basis_xform(const Vector2& p_vec) const;
  406. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2& p_vec) const;
  407. _FORCE_INLINE_ Vector2 xform(const Vector2& p_vec) const;
  408. _FORCE_INLINE_ Vector2 xform_inv(const Vector2& p_vec) const;
  409. _FORCE_INLINE_ Rect2 xform(const Rect2& p_vec) const;
  410. _FORCE_INLINE_ Rect2 xform_inv(const Rect2& p_vec) const;
  411. operator String() const;
  412. Matrix32(real_t p_rot, const Vector2& p_pos);
  413. Matrix32() { elements[0][0]=1.0; elements[1][1]=1.0; }
  414. };
  415. Vector2 Matrix32::basis_xform(const Vector2& v) const {
  416. return Vector2(
  417. tdotx(v),
  418. tdoty(v)
  419. );
  420. }
  421. Vector2 Matrix32::basis_xform_inv(const Vector2& v) const{
  422. return Vector2(
  423. elements[0].dot(v),
  424. elements[1].dot(v)
  425. );
  426. }
  427. Vector2 Matrix32::xform(const Vector2& v) const {
  428. return Vector2(
  429. tdotx(v),
  430. tdoty(v)
  431. ) + elements[2];
  432. }
  433. Vector2 Matrix32::xform_inv(const Vector2& p_vec) const {
  434. Vector2 v = p_vec - elements[2];
  435. return Vector2(
  436. elements[0].dot(v),
  437. elements[1].dot(v)
  438. );
  439. }
  440. Rect2 Matrix32::xform(const Rect2& p_rect) const {
  441. Vector2 x=elements[0]*p_rect.size.x;
  442. Vector2 y=elements[1]*p_rect.size.y;
  443. Vector2 pos = xform( p_rect.pos );
  444. Rect2 new_rect;
  445. new_rect.pos=pos;
  446. new_rect.expand_to( pos+x );
  447. new_rect.expand_to( pos+y );
  448. new_rect.expand_to( pos+x+y );
  449. return new_rect;
  450. }
  451. void Matrix32::set_rotation_and_scale(real_t p_rot,const Size2& p_scale) {
  452. elements[0][0]=Math::cos(p_rot)*p_scale.x;
  453. elements[1][1]=Math::cos(p_rot)*p_scale.y;
  454. elements[0][1]=-Math::sin(p_rot)*p_scale.x;
  455. elements[1][0]=Math::sin(p_rot)*p_scale.y;
  456. }
  457. Rect2 Matrix32::xform_inv(const Rect2& p_rect) const {
  458. Vector2 ends[4]={
  459. xform_inv( p_rect.pos ),
  460. xform_inv( Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y ) ),
  461. xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y ) ),
  462. xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y ) )
  463. };
  464. Rect2 new_rect;
  465. new_rect.pos=ends[0];
  466. new_rect.expand_to(ends[1]);
  467. new_rect.expand_to(ends[2]);
  468. new_rect.expand_to(ends[3]);
  469. return new_rect;
  470. }
  471. #endif