math_2d.cpp 14 KB

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