vector2.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. real_t sine = Math::sin(p_by);
  89. real_t cosi = Math::cos(p_by);
  90. return Vector2(
  91. x * cosi - y * sine,
  92. x * sine + y * cosi);
  93. }
  94. Vector2 Vector2::posmod(const real_t p_mod) const {
  95. return Vector2(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod));
  96. }
  97. Vector2 Vector2::posmodv(const Vector2 &p_modv) const {
  98. return Vector2(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y));
  99. }
  100. Vector2 Vector2::project(const Vector2 &p_to) const {
  101. return p_to * (dot(p_to) / p_to.length_squared());
  102. }
  103. Vector2 Vector2::snapped(const Vector2 &p_step) const {
  104. return Vector2(
  105. Math::snapped(x, p_step.x),
  106. Math::snapped(y, p_step.y));
  107. }
  108. Vector2 Vector2::clamped(real_t p_len) const {
  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::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const {
  118. Vector2 p0 = p_pre_a;
  119. Vector2 p1 = *this;
  120. Vector2 p2 = p_b;
  121. Vector2 p3 = p_post_b;
  122. real_t t = p_weight;
  123. real_t t2 = t * t;
  124. real_t t3 = t2 * t;
  125. Vector2 out;
  126. out = 0.5 * ((p1 * 2.0) +
  127. (-p0 + p2) * t +
  128. (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
  129. (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
  130. return out;
  131. }
  132. Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const {
  133. Vector2 v = *this;
  134. Vector2 vd = p_to - v;
  135. real_t len = vd.length();
  136. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  137. }
  138. // slide returns the component of the vector along the given plane, specified by its normal vector.
  139. Vector2 Vector2::slide(const Vector2 &p_normal) const {
  140. #ifdef MATH_CHECKS
  141. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
  142. #endif
  143. return *this - p_normal * this->dot(p_normal);
  144. }
  145. Vector2 Vector2::bounce(const Vector2 &p_normal) const {
  146. return -reflect(p_normal);
  147. }
  148. Vector2 Vector2::reflect(const Vector2 &p_normal) const {
  149. #ifdef MATH_CHECKS
  150. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized.");
  151. #endif
  152. return 2.0 * p_normal * this->dot(p_normal) - *this;
  153. }
  154. bool Vector2::is_equal_approx(const Vector2 &p_v) const {
  155. return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
  156. }
  157. /* Vector2i */
  158. Vector2i Vector2i::operator+(const Vector2i &p_v) const {
  159. return Vector2i(x + p_v.x, y + p_v.y);
  160. }
  161. void Vector2i::operator+=(const Vector2i &p_v) {
  162. x += p_v.x;
  163. y += p_v.y;
  164. }
  165. Vector2i Vector2i::operator-(const Vector2i &p_v) const {
  166. return Vector2i(x - p_v.x, y - p_v.y);
  167. }
  168. void Vector2i::operator-=(const Vector2i &p_v) {
  169. x -= p_v.x;
  170. y -= p_v.y;
  171. }
  172. Vector2i Vector2i::operator*(const Vector2i &p_v1) const {
  173. return Vector2i(x * p_v1.x, y * p_v1.y);
  174. }
  175. Vector2i Vector2i::operator*(const int &rvalue) const {
  176. return Vector2i(x * rvalue, y * rvalue);
  177. }
  178. void Vector2i::operator*=(const int &rvalue) {
  179. x *= rvalue;
  180. y *= rvalue;
  181. }
  182. Vector2i Vector2i::operator/(const Vector2i &p_v1) const {
  183. return Vector2i(x / p_v1.x, y / p_v1.y);
  184. }
  185. Vector2i Vector2i::operator/(const int &rvalue) const {
  186. return Vector2i(x / rvalue, y / rvalue);
  187. }
  188. void Vector2i::operator/=(const int &rvalue) {
  189. x /= rvalue;
  190. y /= rvalue;
  191. }
  192. Vector2i Vector2i::operator%(const Vector2i &p_v1) const {
  193. return Vector2i(x % p_v1.x, y % p_v1.y);
  194. }
  195. Vector2i Vector2i::operator%(const int &rvalue) const {
  196. return Vector2i(x % rvalue, y % rvalue);
  197. }
  198. void Vector2i::operator%=(const int &rvalue) {
  199. x %= rvalue;
  200. y %= rvalue;
  201. }
  202. Vector2i Vector2i::operator-() const {
  203. return Vector2i(-x, -y);
  204. }
  205. bool Vector2i::operator==(const Vector2i &p_vec2) const {
  206. return x == p_vec2.x && y == p_vec2.y;
  207. }
  208. bool Vector2i::operator!=(const Vector2i &p_vec2) const {
  209. return x != p_vec2.x || y != p_vec2.y;
  210. }