vector2.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* vector2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. union {
  37. real_t x;
  38. real_t width;
  39. };
  40. union {
  41. real_t y;
  42. real_t height;
  43. };
  44. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  45. return p_idx ? y : x;
  46. }
  47. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  48. return p_idx ? y : x;
  49. }
  50. void normalize();
  51. Vector2 normalized() const;
  52. bool is_normalized() const;
  53. real_t length() const;
  54. real_t length_squared() const;
  55. real_t distance_to(const Vector2 &p_vector2) const;
  56. real_t distance_squared_to(const Vector2 &p_vector2) const;
  57. real_t angle_to(const Vector2 &p_vector2) const;
  58. real_t angle_to_point(const Vector2 &p_vector2) const;
  59. real_t dot(const Vector2 &p_other) const;
  60. real_t cross(const Vector2 &p_other) const;
  61. Vector2 project(const Vector2 &p_b) const;
  62. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  63. Vector2 clamped(real_t p_len) const;
  64. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t);
  65. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const;
  66. _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_b, real_t p_t) const;
  67. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
  68. Vector2 slide(const Vector2 &p_normal) const;
  69. Vector2 bounce(const Vector2 &p_normal) const;
  70. Vector2 reflect(const Vector2 &p_normal) const;
  71. Vector2 operator+(const Vector2 &p_v) const;
  72. void operator+=(const Vector2 &p_v);
  73. Vector2 operator-(const Vector2 &p_v) const;
  74. void operator-=(const Vector2 &p_v);
  75. Vector2 operator*(const Vector2 &p_v1) const;
  76. Vector2 operator*(const real_t &rvalue) const;
  77. void operator*=(const real_t &rvalue);
  78. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  79. Vector2 operator/(const Vector2 &p_v1) const;
  80. Vector2 operator/(const real_t &rvalue) const;
  81. void operator/=(const real_t &rvalue);
  82. Vector2 operator-() const;
  83. bool operator==(const Vector2 &p_vec2) const;
  84. bool operator!=(const Vector2 &p_vec2) const;
  85. bool operator<(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  86. bool operator<=(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y <= p_vec2.y) : (x <= p_vec2.x); }
  87. real_t angle() const;
  88. void set_rotation(real_t p_radians) {
  89. x = Math::cos(p_radians);
  90. y = Math::sin(p_radians);
  91. }
  92. _FORCE_INLINE_ Vector2 abs() const {
  93. return Vector2(Math::abs(x), Math::abs(y));
  94. }
  95. Vector2 rotated(real_t p_by) const;
  96. Vector2 tangent() const {
  97. return Vector2(y, -x);
  98. }
  99. Vector2 floor() const;
  100. Vector2 ceil() const;
  101. Vector2 round() const;
  102. Vector2 snapped(const Vector2 &p_by) const;
  103. real_t aspect() const { return width / height; }
  104. operator String() const { return String::num(x) + ", " + String::num(y); }
  105. _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
  106. x = p_x;
  107. y = p_y;
  108. }
  109. _FORCE_INLINE_ Vector2() {
  110. x = 0;
  111. y = 0;
  112. }
  113. };
  114. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  115. return p_vec - *this * (dot(p_vec) - p_d);
  116. }
  117. _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  118. return p_vec * p_scalar;
  119. }
  120. _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
  121. return Vector2(x + p_v.x, y + p_v.y);
  122. }
  123. _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
  124. x += p_v.x;
  125. y += p_v.y;
  126. }
  127. _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
  128. return Vector2(x - p_v.x, y - p_v.y);
  129. }
  130. _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
  131. x -= p_v.x;
  132. y -= p_v.y;
  133. }
  134. _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  135. return Vector2(x * p_v1.x, y * p_v1.y);
  136. };
  137. _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
  138. return Vector2(x * rvalue, y * rvalue);
  139. };
  140. _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
  141. x *= rvalue;
  142. y *= rvalue;
  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 {
  155. return Vector2(-x, -y);
  156. }
  157. _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
  158. return x == p_vec2.x && y == p_vec2.y;
  159. }
  160. _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
  161. return x != p_vec2.x || y != p_vec2.y;
  162. }
  163. Vector2 Vector2::linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  164. Vector2 res = *this;
  165. res.x += (p_t * (p_b.x - x));
  166. res.y += (p_t * (p_b.y - y));
  167. return res;
  168. }
  169. Vector2 Vector2::slerp(const Vector2 &p_b, real_t p_t) const {
  170. #ifdef MATH_CHECKS
  171. ERR_FAIL_COND_V(!is_normalized(), Vector2());
  172. #endif
  173. real_t theta = angle_to(p_b);
  174. return rotated(theta * p_t);
  175. }
  176. Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  177. Vector2 res = p_a;
  178. res.x += (p_t * (p_b.x - p_a.x));
  179. res.y += (p_t * (p_b.y - p_a.y));
  180. return res;
  181. }
  182. typedef Vector2 Size2;
  183. typedef Vector2 Point2;
  184. /* INTEGER STUFF */
  185. struct Vector2i {
  186. union {
  187. int x;
  188. int width;
  189. };
  190. union {
  191. int y;
  192. int height;
  193. };
  194. _FORCE_INLINE_ int &operator[](int p_idx) {
  195. return p_idx ? y : x;
  196. }
  197. _FORCE_INLINE_ const int &operator[](int p_idx) const {
  198. return p_idx ? y : x;
  199. }
  200. Vector2i operator+(const Vector2i &p_v) const;
  201. void operator+=(const Vector2i &p_v);
  202. Vector2i operator-(const Vector2i &p_v) const;
  203. void operator-=(const Vector2i &p_v);
  204. Vector2i operator*(const Vector2i &p_v1) const;
  205. Vector2i operator*(const int &rvalue) const;
  206. void operator*=(const int &rvalue);
  207. Vector2i operator/(const Vector2i &p_v1) const;
  208. Vector2i operator/(const int &rvalue) const;
  209. void operator/=(const int &rvalue);
  210. Vector2i operator-() const;
  211. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  212. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  213. bool operator==(const Vector2i &p_vec2) const;
  214. bool operator!=(const Vector2i &p_vec2) const;
  215. real_t get_aspect() const { return width / (real_t)height; }
  216. operator String() const { return String::num(x) + ", " + String::num(y); }
  217. operator Vector2() const { return Vector2(x, y); }
  218. inline Vector2i(const Vector2 &p_vec2) {
  219. x = (int)p_vec2.x;
  220. y = (int)p_vec2.y;
  221. }
  222. inline Vector2i(int p_x, int p_y) {
  223. x = p_x;
  224. y = p_y;
  225. }
  226. inline Vector2i() {
  227. x = 0;
  228. y = 0;
  229. }
  230. };
  231. typedef Vector2i Size2i;
  232. typedef Vector2i Point2i;
  233. #endif // VECTOR2_H