math_2d.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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-2016 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 angle_to_point(const Vector2& p_vector2) const;
  79. float dot(const Vector2& p_other) const;
  80. float cross(const Vector2& p_other) const;
  81. Vector2 cross(real_t p_other) const;
  82. Vector2 project(const Vector2& p_vec) const;
  83. Vector2 plane_project(real_t p_d, const Vector2& p_vec) const;
  84. Vector2 clamped(real_t p_len) const;
  85. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2& p_a, const Vector2& p_b,float p_t);
  86. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2& p_b,float p_t) const;
  87. Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const;
  88. Vector2 cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const;
  89. Vector2 slide(const Vector2& p_vec) const;
  90. Vector2 reflect(const Vector2& p_vec) const;
  91. Vector2 operator+(const Vector2& p_v) const;
  92. void operator+=(const Vector2& p_v);
  93. Vector2 operator-(const Vector2& p_v) const;
  94. void operator-=(const Vector2& p_v);
  95. Vector2 operator*(const Vector2 &p_v1) const;
  96. Vector2 operator*(const float &rvalue) const;
  97. void operator*=(const float &rvalue);
  98. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  99. Vector2 operator/(const Vector2 &p_v1) const;
  100. Vector2 operator/(const float &rvalue) const;
  101. void operator/=(const float &rvalue);
  102. Vector2 operator-() const;
  103. bool operator==(const Vector2& p_vec2) const;
  104. bool operator!=(const Vector2& p_vec2) const;
  105. bool operator<(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  106. bool operator<=(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<=p_vec2.y):(x<=p_vec2.x); }
  107. real_t angle() const;
  108. void set_rotation(float p_radians) {
  109. x=Math::sin(p_radians);
  110. y=Math::cos(p_radians);
  111. }
  112. _FORCE_INLINE_ Vector2 abs() const {
  113. return Vector2( Math::abs(x), Math::abs(y) );
  114. }
  115. Vector2 rotated(float p_by) const;
  116. Vector2 tangent() const {
  117. return Vector2(y,-x);
  118. }
  119. Vector2 floor() const;
  120. Vector2 snapped(const Vector2& p_by) const;
  121. float get_aspect() const { return width/height; }
  122. operator String() const { return String::num(x)+","+String::num(y); }
  123. _FORCE_INLINE_ Vector2(float p_x,float p_y) { x=p_x; y=p_y; }
  124. _FORCE_INLINE_ Vector2() { x=0; y=0; }
  125. };
  126. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2& p_vec) const {
  127. return p_vec - *this * ( dot(p_vec) -p_d);
  128. }
  129. _FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2& p_vec) {
  130. return p_vec*p_scalar;
  131. }
  132. Vector2 Vector2::linear_interpolate(const Vector2& p_b,float p_t) const {
  133. Vector2 res=*this;
  134. res.x+= (p_t * (p_b.x-x));
  135. res.y+= (p_t * (p_b.y-y));
  136. return res;
  137. }
  138. Vector2 Vector2::linear_interpolate(const Vector2& p_a, const Vector2& p_b,float p_t) {
  139. Vector2 res=p_a;
  140. res.x+= (p_t * (p_b.x-p_a.x));
  141. res.y+= (p_t * (p_b.y-p_a.y));
  142. return res;
  143. }
  144. typedef Vector2 Size2;
  145. typedef Vector2 Point2;
  146. struct Matrix32;
  147. struct Rect2 {
  148. Point2 pos;
  149. Size2 size;
  150. const Vector2& get_pos() const { return pos; }
  151. void set_pos(const Vector2& p_pos) { pos=p_pos; }
  152. const Vector2& get_size() const { return size; }
  153. void set_size(const Vector2& p_size) { size=p_size; }
  154. float get_area() const { return size.width*size.height; }
  155. inline bool intersects(const Rect2& p_rect) const {
  156. if ( pos.x >= (p_rect.pos.x + p_rect.size.width) )
  157. return false;
  158. if ( (pos.x+size.width) <= p_rect.pos.x )
  159. return false;
  160. if ( pos.y >= (p_rect.pos.y + p_rect.size.height) )
  161. return false;
  162. if ( (pos.y+size.height) <= p_rect.pos.y )
  163. return false;
  164. return true;
  165. }
  166. inline float distance_to(const Vector2& p_point) const {
  167. float dist = 1e20;
  168. if (p_point.x < pos.x) {
  169. dist=MIN(dist,pos.x-p_point.x);
  170. }
  171. if (p_point.y < pos.y) {
  172. dist=MIN(dist,pos.y-p_point.y);
  173. }
  174. if (p_point.x >= (pos.x+size.x) ) {
  175. dist=MIN(p_point.x-(pos.x+size.x),dist);
  176. }
  177. if (p_point.y >= (pos.y+size.y) ) {
  178. dist=MIN(p_point.y-(pos.y+size.y),dist);
  179. }
  180. if (dist==1e20)
  181. return 0;
  182. else
  183. return dist;
  184. }
  185. _FORCE_INLINE_ bool intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const;
  186. bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const;
  187. inline bool encloses(const Rect2& p_rect) const {
  188. return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
  189. ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
  190. ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
  191. }
  192. inline bool has_no_area() const {
  193. return (size.x<=0 || size.y<=0);
  194. }
  195. inline Rect2 clip(const Rect2& p_rect) const { /// return a clipped rect
  196. Rect2 new_rect=p_rect;
  197. if (!intersects( new_rect ))
  198. return Rect2();
  199. new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
  200. new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
  201. Point2 p_rect_end=p_rect.pos+p_rect.size;
  202. Point2 end=pos+size;
  203. new_rect.size.x=MIN(p_rect_end.x,end.x) - new_rect.pos.x;
  204. new_rect.size.y=MIN(p_rect_end.y,end.y) - new_rect.pos.y;
  205. return new_rect;
  206. }
  207. inline Rect2 merge(const Rect2& p_rect) const { ///< return a merged rect
  208. Rect2 new_rect;
  209. new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
  210. new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
  211. new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
  212. new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
  213. new_rect.size = new_rect.size - new_rect.pos; //make relative again
  214. return new_rect;
  215. };
  216. inline bool has_point(const Point2& p_point) const {
  217. if (p_point.x < pos.x)
  218. return false;
  219. if (p_point.y < pos.y)
  220. return false;
  221. if (p_point.x >= (pos.x+size.x) )
  222. return false;
  223. if (p_point.y >= (pos.y+size.y) )
  224. return false;
  225. return true;
  226. }
  227. inline bool no_area() const { return (size.width<=0 || size.height<=0 ); }
  228. bool operator==(const Rect2& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
  229. bool operator!=(const Rect2& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
  230. inline Rect2 grow(real_t p_by) const {
  231. Rect2 g=*this;
  232. g.pos.x-=p_by;
  233. g.pos.y-=p_by;
  234. g.size.width+=p_by*2;
  235. g.size.height+=p_by*2;
  236. return g;
  237. }
  238. inline Rect2 expand(const Vector2& p_vector) const {
  239. Rect2 r = *this;
  240. r.expand_to(p_vector);
  241. return r;
  242. }
  243. inline void expand_to(const Vector2& p_vector) { //in place function for speed
  244. Vector2 begin=pos;
  245. Vector2 end=pos+size;
  246. if (p_vector.x<begin.x)
  247. begin.x=p_vector.x;
  248. if (p_vector.y<begin.y)
  249. begin.y=p_vector.y;
  250. if (p_vector.x>end.x)
  251. end.x=p_vector.x;
  252. if (p_vector.y>end.y)
  253. end.y=p_vector.y;
  254. pos=begin;
  255. size=end-begin;
  256. }
  257. operator String() const { return String(pos)+","+String(size); }
  258. Rect2() {}
  259. 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 ); }
  260. Rect2( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
  261. };
  262. /* INTEGER STUFF */
  263. struct Point2i {
  264. union {
  265. int x;
  266. int width;
  267. };
  268. union {
  269. int y;
  270. int height;
  271. };
  272. _FORCE_INLINE_ int& operator[](int p_idx) {
  273. return p_idx?y:x;
  274. }
  275. _FORCE_INLINE_ const int& operator[](int p_idx) const {
  276. return p_idx?y:x;
  277. }
  278. Point2i operator+(const Point2i& p_v) const;
  279. void operator+=(const Point2i& p_v);
  280. Point2i operator-(const Point2i& p_v) const;
  281. void operator-=(const Point2i& p_v);
  282. Point2i operator*(const Point2i &p_v1) const;
  283. Point2i operator*(const int &rvalue) const;
  284. void operator*=(const int &rvalue);
  285. Point2i operator/(const Point2i &p_v1) const;
  286. Point2i operator/(const int &rvalue) const;
  287. void operator/=(const int &rvalue);
  288. Point2i operator-() const;
  289. bool operator<(const Point2i& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  290. bool operator>(const Point2i& p_vec2) const { return (x==p_vec2.x)?(y>p_vec2.y):(x>p_vec2.x); }
  291. bool operator==(const Point2i& p_vec2) const;
  292. bool operator!=(const Point2i& p_vec2) const;
  293. float get_aspect() const { return width/(float)height; }
  294. operator String() const { return String::num(x)+","+String::num(y); }
  295. operator Vector2() const { return Vector2(x,y); }
  296. inline Point2i(const Vector2& p_vec2) { x=(int)p_vec2.x; y=(int)p_vec2.y; }
  297. inline Point2i(int p_x,int p_y) { x=p_x; y=p_y; }
  298. inline Point2i() { x=0; y=0; }
  299. };
  300. typedef Point2i Size2i;
  301. struct Rect2i {
  302. Point2i pos;
  303. Size2i size;
  304. const Point2i& get_pos() const { return pos; }
  305. void set_pos(const Point2i& p_pos) { pos=p_pos; }
  306. const Point2i& get_size() const { return size; }
  307. void set_size(const Point2i& p_size) { size=p_size; }
  308. int get_area() const { return size.width*size.height; }
  309. inline bool intersects(const Rect2i& p_rect) const {
  310. if ( pos.x > (p_rect.pos.x + p_rect.size.width) )
  311. return false;
  312. if ( (pos.x+size.width) < p_rect.pos.x )
  313. return false;
  314. if ( pos.y > (p_rect.pos.y + p_rect.size.height) )
  315. return false;
  316. if ( (pos.y+size.height) < p_rect.pos.y )
  317. return false;
  318. return true;
  319. }
  320. inline bool encloses(const Rect2i& p_rect) const {
  321. return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
  322. ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
  323. ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
  324. }
  325. inline bool has_no_area() const {
  326. return (size.x<=0 || size.y<=0);
  327. }
  328. inline Rect2i clip(const Rect2i& p_rect) const { /// return a clipped rect
  329. Rect2i new_rect=p_rect;
  330. if (!intersects( new_rect ))
  331. return Rect2i();
  332. new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
  333. new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
  334. Point2 p_rect_end=p_rect.pos+p_rect.size;
  335. Point2 end=pos+size;
  336. new_rect.size.x=(int)(MIN(p_rect_end.x,end.x) - new_rect.pos.x);
  337. new_rect.size.y=(int)(MIN(p_rect_end.y,end.y) - new_rect.pos.y);
  338. return new_rect;
  339. }
  340. inline Rect2i merge(const Rect2i& p_rect) const { ///< return a merged rect
  341. Rect2i new_rect;
  342. new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
  343. new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
  344. new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
  345. new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
  346. new_rect.size = new_rect.size - new_rect.pos; //make relative again
  347. return new_rect;
  348. };
  349. bool has_point(const Point2& p_point) const {
  350. if (p_point.x < pos.x)
  351. return false;
  352. if (p_point.y < pos.y)
  353. return false;
  354. if (p_point.x >= (pos.x+size.x) )
  355. return false;
  356. if (p_point.y >= (pos.y+size.y) )
  357. return false;
  358. return true;
  359. }
  360. bool no_area() { return (size.width<=0 || size.height<=0 ); }
  361. bool operator==(const Rect2i& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
  362. bool operator!=(const Rect2i& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
  363. Rect2i grow(int p_by) const {
  364. Rect2i g=*this;
  365. g.pos.x-=p_by;
  366. g.pos.y-=p_by;
  367. g.size.width+=p_by*2;
  368. g.size.height+=p_by*2;
  369. return g;
  370. }
  371. inline void expand_to(const Point2i& p_vector) {
  372. Point2i begin=pos;
  373. Point2i end=pos+size;
  374. if (p_vector.x<begin.x)
  375. begin.x=p_vector.x;
  376. if (p_vector.y<begin.y)
  377. begin.y=p_vector.y;
  378. if (p_vector.x>end.x)
  379. end.x=p_vector.x;
  380. if (p_vector.y>end.y)
  381. end.y=p_vector.y;
  382. pos=begin;
  383. size=end-begin;
  384. }
  385. operator String() const { return String(pos)+","+String(size); }
  386. operator Rect2() const { return Rect2(pos,size); }
  387. Rect2i(const Rect2& p_r2) { pos=p_r2.pos; size=p_r2.size; }
  388. Rect2i() {}
  389. 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 ); }
  390. Rect2i( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
  391. };
  392. struct Matrix32 {
  393. Vector2 elements[3];
  394. _FORCE_INLINE_ float tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  395. _FORCE_INLINE_ float tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  396. const Vector2& operator[](int p_idx) const { return elements[p_idx]; }
  397. Vector2& operator[](int p_idx) { return elements[p_idx]; }
  398. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
  399. _FORCE_INLINE_ void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
  400. void invert();
  401. Matrix32 inverse() const;
  402. void affine_invert();
  403. Matrix32 affine_inverse() const;
  404. void set_rotation(real_t p_phi);
  405. real_t get_rotation() const;
  406. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
  407. void rotate(real_t p_phi);
  408. void scale(const Vector2& p_scale);
  409. void scale_basis(const Vector2& p_scale);
  410. void translate( real_t p_tx, real_t p_ty);
  411. void translate( const Vector2& p_translation );
  412. float basis_determinant() const;
  413. Vector2 get_scale() const;
  414. _FORCE_INLINE_ const Vector2& get_origin() const { return elements[2]; }
  415. _FORCE_INLINE_ void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
  416. Matrix32 scaled(const Vector2& p_scale) const;
  417. Matrix32 basis_scaled(const Vector2& p_scale) const;
  418. Matrix32 translated(const Vector2& p_offset) const;
  419. Matrix32 rotated(float p_phi) const;
  420. Matrix32 untranslated() const;
  421. void orthonormalize();
  422. Matrix32 orthonormalized() const;
  423. bool operator==(const Matrix32& p_transform) const;
  424. bool operator!=(const Matrix32& p_transform) const;
  425. void operator*=(const Matrix32& p_transform);
  426. Matrix32 operator*(const Matrix32& p_transform) const;
  427. Matrix32 interpolate_with(const Matrix32& p_transform, float p_c) const;
  428. _FORCE_INLINE_ Vector2 basis_xform(const Vector2& p_vec) const;
  429. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2& p_vec) const;
  430. _FORCE_INLINE_ Vector2 xform(const Vector2& p_vec) const;
  431. _FORCE_INLINE_ Vector2 xform_inv(const Vector2& p_vec) const;
  432. _FORCE_INLINE_ Rect2 xform(const Rect2& p_vec) const;
  433. _FORCE_INLINE_ Rect2 xform_inv(const Rect2& p_vec) const;
  434. operator String() const;
  435. Matrix32(real_t p_rot, const Vector2& p_pos);
  436. Matrix32() { elements[0][0]=1.0; elements[1][1]=1.0; }
  437. };
  438. bool Rect2::intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const {
  439. //SAT intersection between local and transformed rect2
  440. Vector2 xf_points[4]={
  441. p_xform.xform(p_rect.pos),
  442. p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y)),
  443. p_xform.xform(Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y)),
  444. p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y)),
  445. };
  446. real_t low_limit;
  447. //base rect2 first (faster)
  448. if (xf_points[0].y>pos.y)
  449. goto next1;
  450. if (xf_points[1].y>pos.y)
  451. goto next1;
  452. if (xf_points[2].y>pos.y)
  453. goto next1;
  454. if (xf_points[3].y>pos.y)
  455. goto next1;
  456. return false;
  457. next1:
  458. low_limit=pos.y+size.y;
  459. if (xf_points[0].y<low_limit)
  460. goto next2;
  461. if (xf_points[1].y<low_limit)
  462. goto next2;
  463. if (xf_points[2].y<low_limit)
  464. goto next2;
  465. if (xf_points[3].y<low_limit)
  466. goto next2;
  467. return false;
  468. next2:
  469. if (xf_points[0].x>pos.x)
  470. goto next3;
  471. if (xf_points[1].x>pos.x)
  472. goto next3;
  473. if (xf_points[2].x>pos.x)
  474. goto next3;
  475. if (xf_points[3].x>pos.x)
  476. goto next3;
  477. return false;
  478. next3:
  479. low_limit=pos.x+size.x;
  480. if (xf_points[0].x<low_limit)
  481. goto next4;
  482. if (xf_points[1].x<low_limit)
  483. goto next4;
  484. if (xf_points[2].x<low_limit)
  485. goto next4;
  486. if (xf_points[3].x<low_limit)
  487. goto next4;
  488. return false;
  489. next4:
  490. Vector2 xf_points2[4]={
  491. pos,
  492. Vector2(pos.x+size.x,pos.y),
  493. Vector2(pos.x,pos.y+size.y),
  494. Vector2(pos.x+size.x,pos.y+size.y),
  495. };
  496. real_t maxa=p_xform.elements[0].dot(xf_points2[0]);
  497. real_t mina=maxa;
  498. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  499. maxa=MAX(dp,maxa);
  500. mina=MIN(dp,mina);
  501. dp = p_xform.elements[0].dot(xf_points2[2]);
  502. maxa=MAX(dp,maxa);
  503. mina=MIN(dp,mina);
  504. dp = p_xform.elements[0].dot(xf_points2[3]);
  505. maxa=MAX(dp,maxa);
  506. mina=MIN(dp,mina);
  507. real_t maxb=p_xform.elements[0].dot(xf_points[0]);
  508. real_t minb=maxb;
  509. dp = p_xform.elements[0].dot(xf_points[1]);
  510. maxb=MAX(dp,maxb);
  511. minb=MIN(dp,minb);
  512. dp = p_xform.elements[0].dot(xf_points[2]);
  513. maxb=MAX(dp,maxb);
  514. minb=MIN(dp,minb);
  515. dp = p_xform.elements[0].dot(xf_points[3]);
  516. maxb=MAX(dp,maxb);
  517. minb=MIN(dp,minb);
  518. if ( mina > maxb )
  519. return false;
  520. if ( minb > maxa )
  521. return false;
  522. maxa=p_xform.elements[1].dot(xf_points2[0]);
  523. mina=maxa;
  524. dp = p_xform.elements[1].dot(xf_points2[1]);
  525. maxa=MAX(dp,maxa);
  526. mina=MIN(dp,mina);
  527. dp = p_xform.elements[1].dot(xf_points2[2]);
  528. maxa=MAX(dp,maxa);
  529. mina=MIN(dp,mina);
  530. dp = p_xform.elements[1].dot(xf_points2[3]);
  531. maxa=MAX(dp,maxa);
  532. mina=MIN(dp,mina);
  533. maxb=p_xform.elements[1].dot(xf_points[0]);
  534. minb=maxb;
  535. dp = p_xform.elements[1].dot(xf_points[1]);
  536. maxb=MAX(dp,maxb);
  537. minb=MIN(dp,minb);
  538. dp = p_xform.elements[1].dot(xf_points[2]);
  539. maxb=MAX(dp,maxb);
  540. minb=MIN(dp,minb);
  541. dp = p_xform.elements[1].dot(xf_points[3]);
  542. maxb=MAX(dp,maxb);
  543. minb=MIN(dp,minb);
  544. if ( mina > maxb )
  545. return false;
  546. if ( minb > maxa )
  547. return false;
  548. return true;
  549. }
  550. Vector2 Matrix32::basis_xform(const Vector2& v) const {
  551. return Vector2(
  552. tdotx(v),
  553. tdoty(v)
  554. );
  555. }
  556. Vector2 Matrix32::basis_xform_inv(const Vector2& v) const{
  557. return Vector2(
  558. elements[0].dot(v),
  559. elements[1].dot(v)
  560. );
  561. }
  562. Vector2 Matrix32::xform(const Vector2& v) const {
  563. return Vector2(
  564. tdotx(v),
  565. tdoty(v)
  566. ) + elements[2];
  567. }
  568. Vector2 Matrix32::xform_inv(const Vector2& p_vec) const {
  569. Vector2 v = p_vec - elements[2];
  570. return Vector2(
  571. elements[0].dot(v),
  572. elements[1].dot(v)
  573. );
  574. }
  575. Rect2 Matrix32::xform(const Rect2& p_rect) const {
  576. Vector2 x=elements[0]*p_rect.size.x;
  577. Vector2 y=elements[1]*p_rect.size.y;
  578. Vector2 pos = xform( p_rect.pos );
  579. Rect2 new_rect;
  580. new_rect.pos=pos;
  581. new_rect.expand_to( pos+x );
  582. new_rect.expand_to( pos+y );
  583. new_rect.expand_to( pos+x+y );
  584. return new_rect;
  585. }
  586. void Matrix32::set_rotation_and_scale(real_t p_rot,const Size2& p_scale) {
  587. elements[0][0]=Math::cos(p_rot)*p_scale.x;
  588. elements[1][1]=Math::cos(p_rot)*p_scale.y;
  589. elements[0][1]=-Math::sin(p_rot)*p_scale.x;
  590. elements[1][0]=Math::sin(p_rot)*p_scale.y;
  591. }
  592. Rect2 Matrix32::xform_inv(const Rect2& p_rect) const {
  593. Vector2 ends[4]={
  594. xform_inv( p_rect.pos ),
  595. xform_inv( Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y ) ),
  596. xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y ) ),
  597. xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y ) )
  598. };
  599. Rect2 new_rect;
  600. new_rect.pos=ends[0];
  601. new_rect.expand_to(ends[1]);
  602. new_rect.expand_to(ends[2]);
  603. new_rect.expand_to(ends[3]);
  604. return new_rect;
  605. }
  606. #endif