vector2.h 10 KB

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