vector2.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**************************************************************************/
  2. /* vector2.cpp */
  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. #include "vector2.h"
  31. real_t Vector2::angle() const {
  32. return Math::atan2(y, x);
  33. }
  34. real_t Vector2::length() const {
  35. return Math::sqrt(x * x + y * y);
  36. }
  37. real_t Vector2::length_squared() const {
  38. return x * x + y * y;
  39. }
  40. void Vector2::normalize() {
  41. real_t 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. bool Vector2::is_normalized() const {
  54. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  55. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  56. }
  57. real_t Vector2::distance_to(const Vector2 &p_vector2) const {
  58. return Math::sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
  59. }
  60. real_t Vector2::distance_squared_to(const Vector2 &p_vector2) const {
  61. return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y);
  62. }
  63. real_t Vector2::angle_to(const Vector2 &p_vector2) const {
  64. return Math::atan2(cross(p_vector2), dot(p_vector2));
  65. }
  66. real_t Vector2::angle_to_point(const Vector2 &p_vector2) const {
  67. return Math::atan2(y - p_vector2.y, x - p_vector2.x);
  68. }
  69. real_t Vector2::dot(const Vector2 &p_other) const {
  70. return x * p_other.x + y * p_other.y;
  71. }
  72. real_t Vector2::cross(const Vector2 &p_other) const {
  73. return x * p_other.y - y * p_other.x;
  74. }
  75. Vector2 Vector2::sign() const {
  76. return Vector2(SGN(x), SGN(y));
  77. }
  78. Vector2 Vector2::floor() const {
  79. return Vector2(Math::floor(x), Math::floor(y));
  80. }
  81. Vector2 Vector2::ceil() const {
  82. return Vector2(Math::ceil(x), Math::ceil(y));
  83. }
  84. Vector2 Vector2::round() const {
  85. return Vector2(Math::round(x), Math::round(y));
  86. }
  87. Vector2 Vector2::rotated(real_t p_by) const {
  88. Vector2 v;
  89. v.set_rotation(angle() + p_by);
  90. v *= length();
  91. return v;
  92. }
  93. Vector2 Vector2::posmod(real_t p_mod) const {
  94. return Vector2(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod));
  95. }
  96. Vector2 Vector2::posmodv(const Vector2 &p_modv) const {
  97. return Vector2(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y));
  98. }
  99. Vector2 Vector2::project(const Vector2 &p_to) const {
  100. return p_to * (dot(p_to) / p_to.length_squared());
  101. }
  102. Vector2 Vector2::snapped(const Vector2 &p_by) const {
  103. return Vector2(
  104. Math::stepify(x, p_by.x),
  105. Math::stepify(y, p_by.y));
  106. }
  107. Vector2 Vector2::clamped(real_t p_len) const {
  108. WARN_DEPRECATED_MSG("'Vector2.clamped()' is deprecated because it has been renamed to 'limit_length'.");
  109. real_t l = length();
  110. Vector2 v = *this;
  111. if (l > 0 && p_len < l) {
  112. v /= l;
  113. v *= p_len;
  114. }
  115. return v;
  116. }
  117. Vector2 Vector2::limit_length(real_t p_len) const {
  118. const real_t l = length();
  119. Vector2 v = *this;
  120. if (l > 0 && p_len < l) {
  121. v /= l;
  122. v *= p_len;
  123. }
  124. return v;
  125. }
  126. Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const {
  127. Vector2 p0 = p_pre_a;
  128. Vector2 p1 = *this;
  129. Vector2 p2 = p_b;
  130. Vector2 p3 = p_post_b;
  131. real_t t = p_weight;
  132. real_t t2 = t * t;
  133. real_t t3 = t2 * t;
  134. Vector2 out;
  135. out = 0.5f *
  136. ((p1 * 2) +
  137. (-p0 + p2) * t +
  138. (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
  139. (-p0 + 3 * p1 - 3 * p2 + p3) * t3);
  140. return out;
  141. }
  142. Vector2 Vector2::move_toward(const Vector2 &p_to, real_t p_delta) const {
  143. Vector2 v = *this;
  144. Vector2 vd = p_to - v;
  145. real_t len = vd.length();
  146. return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
  147. }
  148. // slide returns the component of the vector along the given plane, specified by its normal vector.
  149. Vector2 Vector2::slide(const Vector2 &p_normal) const {
  150. #ifdef MATH_CHECKS
  151. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
  152. #endif
  153. return *this - p_normal * this->dot(p_normal);
  154. }
  155. Vector2 Vector2::bounce(const Vector2 &p_normal) const {
  156. return -reflect(p_normal);
  157. }
  158. Vector2 Vector2::reflect(const Vector2 &p_normal) const {
  159. #ifdef MATH_CHECKS
  160. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
  161. #endif
  162. return 2 * p_normal * this->dot(p_normal) - *this;
  163. }
  164. bool Vector2::is_equal_approx(const Vector2 &p_v) const {
  165. return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
  166. }
  167. bool Vector2::is_zero_approx() const {
  168. return Math::is_zero_approx(x) && Math::is_zero_approx(y);
  169. }
  170. /* Vector2i */
  171. Vector2i Vector2i::operator+(const Vector2i &p_v) const {
  172. return Vector2i(x + p_v.x, y + p_v.y);
  173. }
  174. void Vector2i::operator+=(const Vector2i &p_v) {
  175. x += p_v.x;
  176. y += p_v.y;
  177. }
  178. Vector2i Vector2i::operator-(const Vector2i &p_v) const {
  179. return Vector2i(x - p_v.x, y - p_v.y);
  180. }
  181. void Vector2i::operator-=(const Vector2i &p_v) {
  182. x -= p_v.x;
  183. y -= p_v.y;
  184. }
  185. Vector2i Vector2i::operator*(const Vector2i &p_v) const {
  186. return Vector2i(x * p_v.x, y * p_v.y);
  187. }
  188. Vector2i Vector2i::operator*(int p_scalar) const {
  189. return Vector2i(x * p_scalar, y * p_scalar);
  190. }
  191. void Vector2i::operator*=(int p_scalar) {
  192. x *= p_scalar;
  193. y *= p_scalar;
  194. }
  195. Vector2i Vector2i::operator/(const Vector2i &p_v) const {
  196. return Vector2i(x / p_v.x, y / p_v.y);
  197. }
  198. Vector2i Vector2i::operator/(int p_scalar) const {
  199. return Vector2i(x / p_scalar, y / p_scalar);
  200. }
  201. void Vector2i::operator/=(int p_scalar) {
  202. x /= p_scalar;
  203. y /= p_scalar;
  204. }
  205. Vector2i Vector2i::operator-() const {
  206. return Vector2i(-x, -y);
  207. }
  208. bool Vector2i::operator==(const Vector2i &p_vec2) const {
  209. return x == p_vec2.x && y == p_vec2.y;
  210. }
  211. bool Vector2i::operator!=(const Vector2i &p_vec2) const {
  212. return x != p_vec2.x || y != p_vec2.y;
  213. }