math_2d.h 20 KB

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