vector2.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* vector2.h */
  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. #ifndef VECTOR2_H
  31. #define VECTOR2_H
  32. #include "core/math/math_funcs.h"
  33. #include "core/ustring.h"
  34. struct Vector2i;
  35. struct Vector2 {
  36. enum Axis {
  37. AXIS_X,
  38. AXIS_Y,
  39. };
  40. union {
  41. real_t x;
  42. real_t width;
  43. };
  44. union {
  45. real_t y;
  46. real_t height;
  47. };
  48. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  49. return p_idx ? y : x;
  50. }
  51. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  52. return p_idx ? y : x;
  53. }
  54. void normalize();
  55. Vector2 normalized() const;
  56. bool is_normalized() const;
  57. real_t length() const;
  58. real_t length_squared() const;
  59. real_t distance_to(const Vector2 &p_vector2) const;
  60. real_t distance_squared_to(const Vector2 &p_vector2) const;
  61. real_t angle_to(const Vector2 &p_vector2) const;
  62. real_t angle_to_point(const Vector2 &p_vector2) const;
  63. _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_b) const;
  64. real_t dot(const Vector2 &p_other) const;
  65. real_t cross(const Vector2 &p_other) const;
  66. Vector2 posmod(const real_t p_mod) const;
  67. Vector2 posmodv(const Vector2 &p_modv) const;
  68. Vector2 project(const Vector2 &p_b) const;
  69. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  70. Vector2 clamped(real_t p_len) const;
  71. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t);
  72. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const;
  73. _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_b, real_t p_t) const;
  74. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
  75. Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
  76. Vector2 slide(const Vector2 &p_normal) const;
  77. Vector2 bounce(const Vector2 &p_normal) const;
  78. Vector2 reflect(const Vector2 &p_normal) const;
  79. bool is_equal_approx(const Vector2 &p_v) const;
  80. Vector2 operator+(const Vector2 &p_v) const;
  81. void operator+=(const Vector2 &p_v);
  82. Vector2 operator-(const Vector2 &p_v) const;
  83. void operator-=(const Vector2 &p_v);
  84. Vector2 operator*(const Vector2 &p_v1) const;
  85. Vector2 operator*(const real_t &rvalue) const;
  86. void operator*=(const real_t &rvalue);
  87. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  88. Vector2 operator/(const Vector2 &p_v1) const;
  89. Vector2 operator/(const real_t &rvalue) const;
  90. void operator/=(const real_t &rvalue);
  91. void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
  92. Vector2 operator-() const;
  93. bool operator==(const Vector2 &p_vec2) const;
  94. bool operator!=(const Vector2 &p_vec2) const;
  95. bool operator<(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  96. bool operator>(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  97. bool operator<=(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y <= p_vec2.y) : (x < p_vec2.x); }
  98. bool operator>=(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y >= p_vec2.y) : (x > p_vec2.x); }
  99. real_t angle() const;
  100. void set_rotation(real_t p_radians) {
  101. x = Math::cos(p_radians);
  102. y = Math::sin(p_radians);
  103. }
  104. _FORCE_INLINE_ Vector2 abs() const {
  105. return Vector2(Math::abs(x), Math::abs(y));
  106. }
  107. Vector2 rotated(real_t p_by) const;
  108. Vector2 tangent() const {
  109. return Vector2(y, -x);
  110. }
  111. Vector2 sign() const;
  112. Vector2 floor() const;
  113. Vector2 ceil() const;
  114. Vector2 round() const;
  115. Vector2 snapped(const Vector2 &p_by) const;
  116. real_t aspect() const { return width / height; }
  117. operator String() const { return String::num(x) + ", " + String::num(y); }
  118. _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
  119. x = p_x;
  120. y = p_y;
  121. }
  122. _FORCE_INLINE_ Vector2() { x = y = 0; }
  123. };
  124. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  125. return p_vec - *this * (dot(p_vec) - p_d);
  126. }
  127. _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  128. return p_vec * p_scalar;
  129. }
  130. _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
  131. return Vector2(x + p_v.x, y + p_v.y);
  132. }
  133. _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
  134. x += p_v.x;
  135. y += p_v.y;
  136. }
  137. _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
  138. return Vector2(x - p_v.x, y - p_v.y);
  139. }
  140. _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
  141. x -= p_v.x;
  142. y -= p_v.y;
  143. }
  144. _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  145. return Vector2(x * p_v1.x, y * p_v1.y);
  146. };
  147. _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
  148. return Vector2(x * rvalue, y * rvalue);
  149. };
  150. _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
  151. x *= rvalue;
  152. y *= rvalue;
  153. };
  154. _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  155. return Vector2(x / p_v1.x, y / p_v1.y);
  156. };
  157. _FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
  158. return Vector2(x / rvalue, y / rvalue);
  159. };
  160. _FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
  161. x /= rvalue;
  162. y /= rvalue;
  163. };
  164. _FORCE_INLINE_ Vector2 Vector2::operator-() const {
  165. return Vector2(-x, -y);
  166. }
  167. _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
  168. return x == p_vec2.x && y == p_vec2.y;
  169. }
  170. _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
  171. return x != p_vec2.x || y != p_vec2.y;
  172. }
  173. Vector2 Vector2::linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  174. Vector2 res = *this;
  175. res.x += (p_t * (p_b.x - x));
  176. res.y += (p_t * (p_b.y - y));
  177. return res;
  178. }
  179. Vector2 Vector2::slerp(const Vector2 &p_b, real_t p_t) const {
  180. #ifdef MATH_CHECKS
  181. ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized.");
  182. #endif
  183. real_t theta = angle_to(p_b);
  184. return rotated(theta * p_t);
  185. }
  186. Vector2 Vector2::direction_to(const Vector2 &p_b) const {
  187. Vector2 ret(p_b.x - x, p_b.y - y);
  188. ret.normalize();
  189. return ret;
  190. }
  191. Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  192. Vector2 res = p_a;
  193. res.x += (p_t * (p_b.x - p_a.x));
  194. res.y += (p_t * (p_b.y - p_a.y));
  195. return res;
  196. }
  197. typedef Vector2 Size2;
  198. typedef Vector2 Point2;
  199. /* INTEGER STUFF */
  200. struct Vector2i {
  201. enum Axis {
  202. AXIS_X,
  203. AXIS_Y,
  204. };
  205. union {
  206. int x;
  207. int width;
  208. };
  209. union {
  210. int y;
  211. int height;
  212. };
  213. _FORCE_INLINE_ int &operator[](int p_idx) {
  214. return p_idx ? y : x;
  215. }
  216. _FORCE_INLINE_ const int &operator[](int p_idx) const {
  217. return p_idx ? y : x;
  218. }
  219. Vector2i operator+(const Vector2i &p_v) const;
  220. void operator+=(const Vector2i &p_v);
  221. Vector2i operator-(const Vector2i &p_v) const;
  222. void operator-=(const Vector2i &p_v);
  223. Vector2i operator*(const Vector2i &p_v1) const;
  224. Vector2i operator*(const int &rvalue) const;
  225. void operator*=(const int &rvalue);
  226. Vector2i operator/(const Vector2i &p_v1) const;
  227. Vector2i operator/(const int &rvalue) const;
  228. void operator/=(const int &rvalue);
  229. Vector2i operator-() const;
  230. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  231. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  232. bool operator==(const Vector2i &p_vec2) const;
  233. bool operator!=(const Vector2i &p_vec2) const;
  234. real_t get_aspect() const { return width / (real_t)height; }
  235. operator String() const { return String::num(x) + ", " + String::num(y); }
  236. operator Vector2() const { return Vector2(x, y); }
  237. inline Vector2i(const Vector2 &p_vec2) {
  238. x = (int)p_vec2.x;
  239. y = (int)p_vec2.y;
  240. }
  241. inline Vector2i(int p_x, int p_y) {
  242. x = p_x;
  243. y = p_y;
  244. }
  245. inline Vector2i() {
  246. x = 0;
  247. y = 0;
  248. }
  249. };
  250. typedef Vector2i Size2i;
  251. typedef Vector2i Point2i;
  252. #endif // VECTOR2_H