2
0

vector2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*************************************************************************/
  2. /* vector2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/string/ustring.h"
  34. struct Vector2i;
  35. struct 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] = { 0 };
  53. };
  54. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  55. return p_idx ? y : x;
  56. }
  57. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  58. return p_idx ? y : x;
  59. }
  60. _FORCE_INLINE_ void set_all(const real_t p_value) {
  61. x = y = p_value;
  62. }
  63. _FORCE_INLINE_ int min_axis() const {
  64. return x < y ? 0 : 1;
  65. }
  66. _FORCE_INLINE_ int max_axis() const {
  67. return x < y ? 1 : 0;
  68. }
  69. void normalize();
  70. Vector2 normalized() const;
  71. bool is_normalized() const;
  72. real_t length() const;
  73. real_t length_squared() const;
  74. Vector2 limit_length(const real_t p_len = 1.0) const;
  75. Vector2 min(const Vector2 &p_vector2) const {
  76. return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
  77. }
  78. Vector2 max(const Vector2 &p_vector2) const {
  79. return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
  80. }
  81. real_t distance_to(const Vector2 &p_vector2) const;
  82. real_t distance_squared_to(const Vector2 &p_vector2) const;
  83. real_t angle_to(const Vector2 &p_vector2) const;
  84. real_t angle_to_point(const Vector2 &p_vector2) const;
  85. _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
  86. real_t dot(const Vector2 &p_other) const;
  87. real_t cross(const Vector2 &p_other) const;
  88. Vector2 posmod(const real_t p_mod) const;
  89. Vector2 posmodv(const Vector2 &p_modv) const;
  90. Vector2 project(const Vector2 &p_to) const;
  91. Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const;
  92. _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const;
  93. _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const;
  94. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const;
  95. Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
  96. Vector2 slide(const Vector2 &p_normal) const;
  97. Vector2 bounce(const Vector2 &p_normal) const;
  98. Vector2 reflect(const Vector2 &p_normal) const;
  99. bool is_equal_approx(const Vector2 &p_v) const;
  100. Vector2 operator+(const Vector2 &p_v) const;
  101. void operator+=(const Vector2 &p_v);
  102. Vector2 operator-(const Vector2 &p_v) const;
  103. void operator-=(const Vector2 &p_v);
  104. Vector2 operator*(const Vector2 &p_v1) const;
  105. Vector2 operator*(const real_t &rvalue) const;
  106. void operator*=(const real_t &rvalue);
  107. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  108. Vector2 operator/(const Vector2 &p_v1) const;
  109. Vector2 operator/(const real_t &rvalue) const;
  110. void operator/=(const real_t &rvalue);
  111. void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
  112. Vector2 operator-() const;
  113. bool operator==(const Vector2 &p_vec2) const;
  114. bool operator!=(const Vector2 &p_vec2) const;
  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. bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  118. bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  119. real_t angle() const;
  120. static Vector2 from_angle(const real_t p_angle);
  121. _FORCE_INLINE_ Vector2 abs() const {
  122. return Vector2(Math::abs(x), Math::abs(y));
  123. }
  124. Vector2 rotated(const real_t p_by) const;
  125. Vector2 orthogonal() const {
  126. return Vector2(y, -x);
  127. }
  128. Vector2 sign() const;
  129. Vector2 floor() const;
  130. Vector2 ceil() const;
  131. Vector2 round() const;
  132. Vector2 snapped(const Vector2 &p_by) const;
  133. Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
  134. real_t aspect() const { return width / height; }
  135. operator String() const;
  136. _FORCE_INLINE_ Vector2() {}
  137. _FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) {
  138. x = p_x;
  139. y = p_y;
  140. }
  141. };
  142. _FORCE_INLINE_ Vector2 Vector2::plane_project(const 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*(const float p_scalar, const Vector2 &p_vec) {
  146. return p_vec * p_scalar;
  147. }
  148. _FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) {
  149. return p_vec * p_scalar;
  150. }
  151. _FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) {
  152. return p_vec * p_scalar;
  153. }
  154. _FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) {
  155. return p_vec * p_scalar;
  156. }
  157. _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
  158. return Vector2(x + p_v.x, y + p_v.y);
  159. }
  160. _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
  161. x += p_v.x;
  162. y += p_v.y;
  163. }
  164. _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
  165. return Vector2(x - p_v.x, y - p_v.y);
  166. }
  167. _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
  168. x -= p_v.x;
  169. y -= p_v.y;
  170. }
  171. _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  172. return Vector2(x * p_v1.x, y * p_v1.y);
  173. }
  174. _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
  175. return Vector2(x * rvalue, y * rvalue);
  176. }
  177. _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
  178. x *= rvalue;
  179. y *= rvalue;
  180. }
  181. _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  182. return Vector2(x / p_v1.x, y / p_v1.y);
  183. }
  184. _FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
  185. return Vector2(x / rvalue, y / rvalue);
  186. }
  187. _FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
  188. x /= rvalue;
  189. y /= rvalue;
  190. }
  191. _FORCE_INLINE_ Vector2 Vector2::operator-() const {
  192. return Vector2(-x, -y);
  193. }
  194. _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
  195. return x == p_vec2.x && y == p_vec2.y;
  196. }
  197. _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
  198. return x != p_vec2.x || y != p_vec2.y;
  199. }
  200. Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const {
  201. Vector2 res = *this;
  202. res.x += (p_weight * (p_to.x - x));
  203. res.y += (p_weight * (p_to.y - y));
  204. return res;
  205. }
  206. Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
  207. #ifdef MATH_CHECKS
  208. ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized.");
  209. #endif
  210. real_t theta = angle_to(p_to);
  211. return rotated(theta * p_weight);
  212. }
  213. Vector2 Vector2::direction_to(const Vector2 &p_to) const {
  214. Vector2 ret(p_to.x - x, p_to.y - y);
  215. ret.normalize();
  216. return ret;
  217. }
  218. typedef Vector2 Size2;
  219. typedef Vector2 Point2;
  220. /* INTEGER STUFF */
  221. struct Vector2i {
  222. enum Axis {
  223. AXIS_X,
  224. AXIS_Y,
  225. };
  226. union {
  227. int32_t x = 0;
  228. int32_t width;
  229. };
  230. union {
  231. int32_t y = 0;
  232. int32_t height;
  233. };
  234. _FORCE_INLINE_ int32_t &operator[](int p_idx) {
  235. return p_idx ? y : x;
  236. }
  237. _FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
  238. return p_idx ? y : x;
  239. }
  240. _FORCE_INLINE_ int min_axis() const {
  241. return x < y ? 0 : 1;
  242. }
  243. _FORCE_INLINE_ int max_axis() const {
  244. return x < y ? 1 : 0;
  245. }
  246. Vector2i min(const Vector2i &p_vector2i) const {
  247. return Vector2(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
  248. }
  249. Vector2i max(const Vector2i &p_vector2i) const {
  250. return Vector2(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
  251. }
  252. Vector2i operator+(const Vector2i &p_v) const;
  253. void operator+=(const Vector2i &p_v);
  254. Vector2i operator-(const Vector2i &p_v) const;
  255. void operator-=(const Vector2i &p_v);
  256. Vector2i operator*(const Vector2i &p_v1) const;
  257. Vector2i operator*(const int32_t &rvalue) const;
  258. void operator*=(const int32_t &rvalue);
  259. Vector2i operator/(const Vector2i &p_v1) const;
  260. Vector2i operator/(const int32_t &rvalue) const;
  261. void operator/=(const int32_t &rvalue);
  262. Vector2i operator%(const Vector2i &p_v1) const;
  263. Vector2i operator%(const int32_t &rvalue) const;
  264. void operator%=(const int32_t &rvalue);
  265. Vector2i operator-() const;
  266. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  267. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  268. bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  269. bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  270. bool operator==(const Vector2i &p_vec2) const;
  271. bool operator!=(const Vector2i &p_vec2) const;
  272. real_t aspect() const { return width / (real_t)height; }
  273. Vector2i sign() const { return Vector2i(SGN(x), SGN(y)); }
  274. Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
  275. Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
  276. operator String() const;
  277. operator Vector2() const { return Vector2(x, y); }
  278. inline Vector2i() {}
  279. inline Vector2i(const Vector2 &p_vec2) {
  280. x = (int32_t)p_vec2.x;
  281. y = (int32_t)p_vec2.y;
  282. }
  283. inline Vector2i(const int32_t p_x, const int32_t p_y) {
  284. x = p_x;
  285. y = p_y;
  286. }
  287. };
  288. _FORCE_INLINE_ Vector2i operator*(const int32_t &p_scalar, const Vector2i &p_vector) {
  289. return p_vector * p_scalar;
  290. }
  291. _FORCE_INLINE_ Vector2i operator*(const int64_t &p_scalar, const Vector2i &p_vector) {
  292. return p_vector * p_scalar;
  293. }
  294. _FORCE_INLINE_ Vector2i operator*(const float &p_scalar, const Vector2i &p_vector) {
  295. return p_vector * p_scalar;
  296. }
  297. _FORCE_INLINE_ Vector2i operator*(const double &p_scalar, const Vector2i &p_vector) {
  298. return p_vector * p_scalar;
  299. }
  300. typedef Vector2i Size2i;
  301. typedef Vector2i Point2i;
  302. #endif // VECTOR2_H