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