Debian Codegraph @

core/math/vector2.cpp

raw symbols SCIP
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(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(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(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::clampf(real_t p_min, real_t p_max) const {
139 return Vector2(
140 CLAMP(x, p_min, p_max),
141 CLAMP(y, p_min, p_max));
142 }
143
144 Vector2 Vector2::snapped(const Vector2 &p_step) const {
145 return Vector2(
146 Math::snapped(x, p_step.x),
147 Math::snapped(y, p_step.y));
148 }
149
150 Vector2 Vector2::snappedf(real_t p_step) const {
151 return Vector2(
152 Math::snapped(x, p_step),
153 Math::snapped(y, p_step));
154 }
155
156 Vector2 Vector2::limit_length(real_t p_len) const {
157 const real_t l = length();
158 Vector2 v = *this;
159 if (l > 0 && p_len < l) {
160 v /= l;
161 v *= p_len;
162 }
163
164 return v;
165 }
166
167 Vector2 Vector2::move_toward(const Vector2 &p_to, real_t p_delta) const {
168 Vector2 v = *this;
169 Vector2 vd = p_to - v;
170 real_t len = vd.length();
171 return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
172 }
173
174 // slide returns the component of the vector along the given plane, specified by its normal vector.
175 Vector2 Vector2::slide(const Vector2 &p_normal) const {
176 #ifdef MATH_CHECKS
177 ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 " + p_normal.operator String() + "must be normalized.");
178 #endif
179 return *this - p_normal * dot(p_normal);
180 }
181
182 Vector2 Vector2::bounce(const Vector2 &p_normal) const {
183 return -reflect(p_normal);
184 }
185
186 Vector2 Vector2::reflect(const Vector2 &p_normal) const {
187 #ifdef MATH_CHECKS
188 ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 " + p_normal.operator String() + "must be normalized.");
189 #endif
190 return 2.0f * p_normal * dot(p_normal) - *this;
191 }
192
193 bool Vector2::is_equal_approx(const Vector2 &p_v) const {
194 return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
195 }
196
197 bool Vector2::is_same(const Vector2 &p_v) const {
198 return Math::is_same(x, p_v.x) && Math::is_same(y, p_v.y);
199 }
200
201 bool Vector2::is_zero_approx() const {
202 return Math::is_zero_approx(x) && Math::is_zero_approx(y);
203 }
204
205 bool Vector2::is_finite() const {
206 return Math::is_finite(x) && Math::is_finite(y);
207 }
208
209 Vector2::operator String() const {
210 return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ")";
211 }
212
213 Vector2::operator Vector2i() const {
214 return Vector2i(x, y);
215 }