math_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*************************************************************************/
  2. /* math_2d.cpp */
  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. #include "math_2d.h"
  30. real_t Vector2::atan2() const {
  31. return Math::atan2(x,y);
  32. }
  33. float Vector2::length() const {
  34. return Math::sqrt( x*x + y*y );
  35. }
  36. float Vector2::length_squared() const {
  37. return x*x + y*y;
  38. }
  39. void Vector2::normalize() {
  40. float l = x*x + y*y;
  41. if (l!=0) {
  42. l=Math::sqrt(l);
  43. x/=l;
  44. y/=l;
  45. }
  46. }
  47. Vector2 Vector2::normalized() const {
  48. Vector2 v=*this;
  49. v.normalize();
  50. return v;
  51. }
  52. float Vector2::distance_to(const Vector2& p_vector2) const {
  53. return Math::sqrt( (x-p_vector2.x)*(x-p_vector2.x) + (y-p_vector2.y)*(y-p_vector2.y));
  54. }
  55. float Vector2::distance_squared_to(const Vector2& p_vector2) const {
  56. return (x-p_vector2.x)*(x-p_vector2.x) + (y-p_vector2.y)*(y-p_vector2.y);
  57. }
  58. float Vector2::angle_to(const Vector2& p_vector2) const {
  59. return Math::atan2( x-p_vector2.x, y - p_vector2.y );
  60. }
  61. float Vector2::dot(const Vector2& p_other) const {
  62. return x*p_other.x + y*p_other.y;
  63. }
  64. float Vector2::cross(const Vector2& p_other) const {
  65. return x*p_other.y - y*p_other.x;
  66. }
  67. Vector2 Vector2::cross(real_t p_other) const {
  68. return Vector2(p_other*y,-p_other*x);
  69. }
  70. Vector2 Vector2::operator+(const Vector2& p_v) const {
  71. return Vector2(x+p_v.x,y+p_v.y);
  72. }
  73. void Vector2::operator+=(const Vector2& p_v) {
  74. x+=p_v.x; y+=p_v.y;
  75. }
  76. Vector2 Vector2::operator-(const Vector2& p_v) const {
  77. return Vector2(x-p_v.x,y-p_v.y);
  78. }
  79. void Vector2::operator-=(const Vector2& p_v) {
  80. x-=p_v.x; y-=p_v.y;
  81. }
  82. Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  83. return Vector2(x * p_v1.x, y * p_v1.y);
  84. };
  85. Vector2 Vector2::operator*(const float &rvalue) const {
  86. return Vector2(x * rvalue, y * rvalue);
  87. };
  88. void Vector2::operator*=(const float &rvalue) {
  89. x *= rvalue; y *= rvalue;
  90. };
  91. Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  92. return Vector2(x / p_v1.x, y / p_v1.y);
  93. };
  94. Vector2 Vector2::operator/(const float &rvalue) const {
  95. return Vector2(x / rvalue, y / rvalue);
  96. };
  97. void Vector2::operator/=(const float &rvalue) {
  98. x /= rvalue; y /= rvalue;
  99. };
  100. Vector2 Vector2::operator-() const {
  101. return Vector2(-x,-y);
  102. }
  103. bool Vector2::operator==(const Vector2& p_vec2) const {
  104. return x==p_vec2.x && y==p_vec2.y;
  105. }
  106. bool Vector2::operator!=(const Vector2& p_vec2) const {
  107. return x!=p_vec2.x || y!=p_vec2.y;
  108. }
  109. Vector2 Vector2::floor() const {
  110. return Vector2( Math::floor(x), Math::floor(y) );
  111. }
  112. Vector2 Vector2::rotated(float p_by) const {
  113. Vector2 v;
  114. v.set_rotation(atan2()+p_by);
  115. v*=length();
  116. return v;
  117. }
  118. Vector2 Vector2::project(const Vector2& p_vec) const {
  119. Vector2 v1=p_vec;
  120. Vector2 v2=*this;
  121. return v2 * ( v1.dot(v2) / v2.dot(v2));
  122. }
  123. Vector2 Vector2::snapped(const Vector2& p_by) const {
  124. return Vector2(
  125. Math::stepify(x,p_by.x),
  126. Math::stepify(y,p_by.y)
  127. );
  128. }
  129. Vector2 Vector2::clamped(real_t p_len) const {
  130. return *this;
  131. real_t l = length();
  132. Vector2 v = *this;
  133. if (l>0 && p_len<l) {
  134. v/=l;
  135. v*=p_len;
  136. }
  137. return v;
  138. }
  139. Vector2 Vector2::cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const {
  140. #if 0
  141. k[0] = ((*this) (vi[0] + 1, vi[1], vi[2])) - ((*this) (vi[0],
  142. vi[1],vi[2])); //fk = a0
  143. k[1] = (((*this) (vi[0] + 1, vi[1], vi[2])) - ((*this) ((int) (v(0) -
  144. 1), vi[1],vi[2])))*0.5; //dk = a1
  145. k[2] = (((*this) ((int) (v(0) + 2), vi[1], vi[2])) - ((*this) (vi[0],
  146. vi[1],vi[2])))*0.5; //dk+1
  147. k[3] = k[0]*3 - k[1]*2 - k[2];//a2
  148. k[4] = k[1] + k[2] - k[0]*2;//a3
  149. //ip = a3(t-tk)³ + a2(t-tk)² + a1(t-tk) + a0
  150. //
  151. //a3 = dk + dk+1 - Dk
  152. //a2 = 3Dk - 2dk - dk+1
  153. //a1 = dk
  154. //a0 = fk
  155. //
  156. //dk = (fk+1 - fk-1)*0.5
  157. //Dk = (fk+1 - fk)
  158. float dk =
  159. #endif
  160. return Vector2();
  161. }
  162. Vector2 Vector2::cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,float p_t) const {
  163. float mu = p_t;
  164. float mu2 = mu*mu;
  165. Vector2 a0 = p_post_b - p_b - p_pre_a + *this;
  166. Vector2 a1 = p_pre_a - *this - a0;
  167. Vector2 a2 = p_b - p_pre_a;
  168. Vector2 a3 = *this;
  169. return ( a0*mu*mu2 + a1*mu2 + a2*mu + a3 );
  170. /*
  171. float t = p_t;
  172. real_t t2 = t*t;
  173. real_t t3 = t2*t;
  174. real_t a = 2.0*t3- 3.0*t2 + 1;
  175. real_t b = -2.0*t3+ 3.0*t2;
  176. real_t c = t3- 2.0*t2 + t;
  177. real_t d = t3- t2;
  178. Vector2 p_a=*this;
  179. return Vector2(
  180. (a * p_a.x) + (b *p_b.x) + (c * p_pre_a.x) + (d * p_post_b.x),
  181. (a * p_a.y) + (b *p_b.y) + (c * p_pre_a.y) + (d * p_post_b.y)
  182. );
  183. */
  184. }
  185. Vector2 Vector2::slide(const Vector2& p_vec) const {
  186. return p_vec - *this * this->dot(p_vec);
  187. }
  188. Vector2 Vector2::reflect(const Vector2& p_vec) const {
  189. return p_vec - *this * this->dot(p_vec) * 2.0;
  190. }
  191. bool Rect2::intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos,Point2* r_normal) const {
  192. real_t min=0,max=1;
  193. int axis=0;
  194. float sign=0;
  195. for(int i=0;i<2;i++) {
  196. real_t seg_from=p_from[i];
  197. real_t seg_to=p_to[i];
  198. real_t box_begin=pos[i];
  199. real_t box_end=box_begin+size[i];
  200. real_t cmin,cmax;
  201. float csign;
  202. if (seg_from < seg_to) {
  203. if (seg_from > box_end || seg_to < box_begin)
  204. return false;
  205. real_t length=seg_to-seg_from;
  206. cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
  207. cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
  208. csign=-1.0;
  209. } else {
  210. if (seg_to > box_end || seg_from < box_begin)
  211. return false;
  212. real_t length=seg_to-seg_from;
  213. cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
  214. cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
  215. csign=1.0;
  216. }
  217. if (cmin > min) {
  218. min = cmin;
  219. axis=i;
  220. sign=csign;
  221. }
  222. if (cmax < max)
  223. max = cmax;
  224. if (max < min)
  225. return false;
  226. }
  227. Vector2 rel=p_to-p_from;
  228. if (r_normal) {
  229. Vector2 normal;
  230. normal[axis]=sign;
  231. *r_normal=normal;
  232. }
  233. if (r_pos)
  234. *r_pos=p_from+rel*min;
  235. return true;
  236. }
  237. /* Point2i */
  238. Point2i Point2i::operator+(const Point2i& p_v) const {
  239. return Point2i(x+p_v.x,y+p_v.y);
  240. }
  241. void Point2i::operator+=(const Point2i& p_v) {
  242. x+=p_v.x; y+=p_v.y;
  243. }
  244. Point2i Point2i::operator-(const Point2i& p_v) const {
  245. return Point2i(x-p_v.x,y-p_v.y);
  246. }
  247. void Point2i::operator-=(const Point2i& p_v) {
  248. x-=p_v.x; y-=p_v.y;
  249. }
  250. Point2i Point2i::operator*(const Point2i &p_v1) const {
  251. return Point2i(x * p_v1.x, y * p_v1.y);
  252. };
  253. Point2i Point2i::operator*(const int &rvalue) const {
  254. return Point2i(x * rvalue, y * rvalue);
  255. };
  256. void Point2i::operator*=(const int &rvalue) {
  257. x *= rvalue; y *= rvalue;
  258. };
  259. Point2i Point2i::operator/(const Point2i &p_v1) const {
  260. return Point2i(x / p_v1.x, y / p_v1.y);
  261. };
  262. Point2i Point2i::operator/(const int &rvalue) const {
  263. return Point2i(x / rvalue, y / rvalue);
  264. };
  265. void Point2i::operator/=(const int &rvalue) {
  266. x /= rvalue; y /= rvalue;
  267. };
  268. Point2i Point2i::operator-() const {
  269. return Point2i(-x,-y);
  270. }
  271. bool Point2i::operator==(const Point2i& p_vec2) const {
  272. return x==p_vec2.x && y==p_vec2.y;
  273. }
  274. bool Point2i::operator!=(const Point2i& p_vec2) const {
  275. return x!=p_vec2.x || y!=p_vec2.y;
  276. }
  277. void Matrix32::invert() {
  278. SWAP(elements[0][1],elements[1][0]);
  279. elements[2] = basis_xform(-elements[2]);
  280. }
  281. Matrix32 Matrix32::inverse() const {
  282. Matrix32 inv=*this;
  283. inv.invert();
  284. return inv;
  285. }
  286. void Matrix32::affine_invert() {
  287. float det = elements[0][0]*elements[1][1] - elements[1][0]*elements[0][1];
  288. ERR_FAIL_COND(det==0);
  289. float idet = 1.0 / det;
  290. SWAP( elements[0][0],elements[1][1] );
  291. elements[0]*=Vector2(idet,-idet);
  292. elements[1]*=Vector2(-idet,idet);
  293. elements[2] = basis_xform(-elements[2]);
  294. }
  295. Matrix32 Matrix32::affine_inverse() const {
  296. Matrix32 inv=*this;
  297. inv.affine_invert();
  298. return inv;
  299. }
  300. void Matrix32::rotate(real_t p_phi) {
  301. Matrix32 rot(p_phi,Vector2());
  302. *this *= rot;
  303. }
  304. real_t Matrix32::get_rotation() const {
  305. return Math::atan2(elements[1].x,elements[1].y);
  306. }
  307. void Matrix32::set_rotation(real_t p_rot) {
  308. real_t cr = Math::cos(p_rot);
  309. real_t sr = Math::sin(p_rot);
  310. elements[0][0]=cr;
  311. elements[1][1]=cr;
  312. elements[0][1]=-sr;
  313. elements[1][0]=sr;
  314. }
  315. Matrix32::Matrix32(real_t p_rot, const Vector2& p_pos) {
  316. real_t cr = Math::cos(p_rot);
  317. real_t sr = Math::sin(p_rot);
  318. elements[0][0]=cr;
  319. elements[1][1]=cr;
  320. elements[0][1]=-sr;
  321. elements[1][0]=sr;
  322. elements[2]=p_pos;
  323. }
  324. Vector2 Matrix32::get_scale() const {
  325. return Vector2( elements[0].length(), elements[1].length() );
  326. }
  327. void Matrix32::scale(const Vector2& p_scale) {
  328. elements[0]*=p_scale;
  329. elements[1]*=p_scale;
  330. elements[2]*=p_scale;
  331. }
  332. void Matrix32::scale_basis(const Vector2& p_scale) {
  333. elements[0]*=p_scale;
  334. elements[1]*=p_scale;
  335. }
  336. void Matrix32::translate( real_t p_tx, real_t p_ty) {
  337. translate(Vector2(p_tx,p_ty));
  338. }
  339. void Matrix32::translate( const Vector2& p_translation ) {
  340. elements[2]+=basis_xform(p_translation);
  341. }
  342. void Matrix32::orthonormalize() {
  343. // Gram-Schmidt Process
  344. Vector2 x=elements[0];
  345. Vector2 y=elements[1];
  346. x.normalize();
  347. y = (y-x*(x.dot(y)));
  348. y.normalize();
  349. elements[0]=x;
  350. elements[1]=y;
  351. }
  352. Matrix32 Matrix32::orthonormalized() const {
  353. Matrix32 on=*this;
  354. on.orthonormalize();
  355. return on;
  356. }
  357. bool Matrix32::operator==(const Matrix32& p_transform) const {
  358. for(int i=0;i<3;i++) {
  359. if (elements[i]!=p_transform.elements[i])
  360. return false;
  361. }
  362. return true;
  363. }
  364. bool Matrix32::operator!=(const Matrix32& p_transform) const {
  365. for(int i=0;i<3;i++) {
  366. if (elements[i]!=p_transform.elements[i])
  367. return true;
  368. }
  369. return false;
  370. }
  371. void Matrix32::operator*=(const Matrix32& p_transform) {
  372. elements[2] = xform(p_transform.elements[2]);
  373. float x0,x1,y0,y1;
  374. /*
  375. x0 = p_transform.tdotx(elements[0]);
  376. x1 = p_transform.tdoty(elements[0]);
  377. y0 = p_transform.tdotx(elements[1]);
  378. y1 = p_transform.tdoty(elements[1]);*/
  379. x0 = tdotx(p_transform.elements[0]);
  380. x1 = tdoty(p_transform.elements[0]);
  381. y0 = tdotx(p_transform.elements[1]);
  382. y1 = tdoty(p_transform.elements[1]);
  383. elements[0][0]=x0;
  384. elements[0][1]=x1;
  385. elements[1][0]=y0;
  386. elements[1][1]=y1;
  387. }
  388. Matrix32 Matrix32::operator*(const Matrix32& p_transform) const {
  389. Matrix32 t = *this;
  390. t*=p_transform;
  391. return t;
  392. }
  393. Matrix32 Matrix32::scaled(const Vector2& p_scale) const {
  394. Matrix32 copy=*this;
  395. copy.scale(p_scale);
  396. return copy;
  397. }
  398. Matrix32 Matrix32::basis_scaled(const Vector2& p_scale) const {
  399. Matrix32 copy=*this;
  400. copy.scale_basis(p_scale);
  401. return copy;
  402. }
  403. Matrix32 Matrix32::untranslated() const {
  404. Matrix32 copy=*this;
  405. copy.elements[2]=Vector2();
  406. return copy;
  407. }
  408. Matrix32 Matrix32::translated(const Vector2& p_offset) const {
  409. Matrix32 copy=*this;
  410. copy.translate(p_offset);
  411. return copy;
  412. }
  413. Matrix32 Matrix32::rotated(float p_phi) const {
  414. Matrix32 copy=*this;
  415. copy.rotate(p_phi);
  416. return copy;
  417. }
  418. Matrix32 Matrix32::interpolate_with(const Matrix32& p_transform, float p_c) const {
  419. return Matrix32();
  420. }
  421. Matrix32::operator String() const {
  422. return String(String()+elements[0]+", "+elements[1]+", "+elements[2]);
  423. }