vector2.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*************************************************************************/
  2. /* vector2.cpp */
  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. #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.0, 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(const 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_b) const {
  100. return p_b * (dot(p_b) / p_b.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. real_t l = length();
  109. Vector2 v = *this;
  110. if (l > 0 && p_len < l) {
  111. v /= l;
  112. v *= p_len;
  113. }
  114. return v;
  115. }
  116. Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const {
  117. Vector2 p0 = p_pre_a;
  118. Vector2 p1 = *this;
  119. Vector2 p2 = p_b;
  120. Vector2 p3 = p_post_b;
  121. real_t t = p_t;
  122. real_t t2 = t * t;
  123. real_t t3 = t2 * t;
  124. Vector2 out;
  125. out = 0.5 * ((p1 * 2.0) +
  126. (-p0 + p2) * t +
  127. (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
  128. (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
  129. return out;
  130. }
  131. Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const {
  132. Vector2 v = *this;
  133. Vector2 vd = p_to - v;
  134. real_t len = vd.length();
  135. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  136. }
  137. // slide returns the component of the vector along the given plane, specified by its normal vector.
  138. Vector2 Vector2::slide(const Vector2 &p_normal) const {
  139. #ifdef MATH_CHECKS
  140. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
  141. #endif
  142. return *this - p_normal * this->dot(p_normal);
  143. }
  144. Vector2 Vector2::bounce(const Vector2 &p_normal) const {
  145. return -reflect(p_normal);
  146. }
  147. Vector2 Vector2::reflect(const Vector2 &p_normal) const {
  148. #ifdef MATH_CHECKS
  149. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
  150. #endif
  151. return 2.0 * p_normal * this->dot(p_normal) - *this;
  152. }
  153. bool Vector2::is_equal_approx(const Vector2 &p_v) const {
  154. return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
  155. }
  156. /* Vector2i */
  157. Vector2i Vector2i::operator+(const Vector2i &p_v) const {
  158. return Vector2i(x + p_v.x, y + p_v.y);
  159. }
  160. void Vector2i::operator+=(const Vector2i &p_v) {
  161. x += p_v.x;
  162. y += p_v.y;
  163. }
  164. Vector2i Vector2i::operator-(const Vector2i &p_v) const {
  165. return Vector2i(x - p_v.x, y - p_v.y);
  166. }
  167. void Vector2i::operator-=(const Vector2i &p_v) {
  168. x -= p_v.x;
  169. y -= p_v.y;
  170. }
  171. Vector2i Vector2i::operator*(const Vector2i &p_v1) const {
  172. return Vector2i(x * p_v1.x, y * p_v1.y);
  173. };
  174. Vector2i Vector2i::operator*(const int &rvalue) const {
  175. return Vector2i(x * rvalue, y * rvalue);
  176. };
  177. void Vector2i::operator*=(const int &rvalue) {
  178. x *= rvalue;
  179. y *= rvalue;
  180. };
  181. Vector2i Vector2i::operator/(const Vector2i &p_v1) const {
  182. return Vector2i(x / p_v1.x, y / p_v1.y);
  183. };
  184. Vector2i Vector2i::operator/(const int &rvalue) const {
  185. return Vector2i(x / rvalue, y / rvalue);
  186. };
  187. void Vector2i::operator/=(const int &rvalue) {
  188. x /= rvalue;
  189. y /= rvalue;
  190. };
  191. Vector2i Vector2i::operator-() const {
  192. return Vector2i(-x, -y);
  193. }
  194. bool Vector2i::operator==(const Vector2i &p_vec2) const {
  195. return x == p_vec2.x && y == p_vec2.y;
  196. }
  197. bool Vector2i::operator!=(const Vector2i &p_vec2) const {
  198. return x != p_vec2.x || y != p_vec2.y;
  199. }