vector2.cpp 8.2 KB

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