Merge pull request #53618 from aaronfranke/signed-angle-vec3i

Add length and length_squared to Vector2i/3i
This commit is contained in:
Rémi Verschelde 2022-01-07 01:14:29 +01:00 committed by GitHub
commit e1766da4bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 0 deletions

View file

@ -210,6 +210,14 @@ Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
CLAMP(y, p_min.y, p_max.y)); CLAMP(y, p_min.y, p_max.y));
} }
int64_t Vector2i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y;
}
double Vector2i::length() const {
return Math::sqrt((double)length_squared());
}
Vector2i Vector2i::operator+(const Vector2i &p_v) const { Vector2i Vector2i::operator+(const Vector2i &p_v) const {
return Vector2i(x + p_v.x, y + p_v.y); return Vector2i(x + p_v.x, y + p_v.y);
} }

View file

@ -349,6 +349,9 @@ struct Vector2i {
bool operator==(const Vector2i &p_vec2) const; bool operator==(const Vector2i &p_vec2) const;
bool operator!=(const Vector2i &p_vec2) const; bool operator!=(const Vector2i &p_vec2) const;
int64_t length_squared() const;
double length() const;
real_t aspect() const { return width / (real_t)height; } real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); } Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); } Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }

View file

@ -31,6 +31,7 @@
#ifndef VECTOR3I_H #ifndef VECTOR3I_H
#define VECTOR3I_H #define VECTOR3I_H
#include "core/math/math_funcs.h"
#include "core/string/ustring.h" #include "core/string/ustring.h"
#include "core/typedefs.h" #include "core/typedefs.h"
@ -65,6 +66,9 @@ struct Vector3i {
Vector3i::Axis min_axis_index() const; Vector3i::Axis min_axis_index() const;
Vector3i::Axis max_axis_index() const; Vector3i::Axis max_axis_index() const;
_FORCE_INLINE_ int64_t length_squared() const;
_FORCE_INLINE_ double length() const;
_FORCE_INLINE_ void zero(); _FORCE_INLINE_ void zero();
_FORCE_INLINE_ Vector3i abs() const; _FORCE_INLINE_ Vector3i abs() const;
@ -110,6 +114,14 @@ struct Vector3i {
} }
}; };
int64_t Vector3i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
}
double Vector3i::length() const {
return Math::sqrt((double)length_squared());
}
Vector3i Vector3i::abs() const { Vector3i Vector3i::abs() const {
return Vector3i(ABS(x), ABS(y), ABS(z)); return Vector3i(ABS(x), ABS(y), ABS(z));
} }

View file

@ -1513,6 +1513,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector2i, aspect, sarray(), varray()); bind_method(Vector2i, aspect, sarray(), varray());
bind_method(Vector2i, max_axis_index, sarray(), varray()); bind_method(Vector2i, max_axis_index, sarray(), varray());
bind_method(Vector2i, min_axis_index, sarray(), varray()); bind_method(Vector2i, min_axis_index, sarray(), varray());
bind_method(Vector2i, length, sarray(), varray());
bind_method(Vector2i, length_squared, sarray(), varray());
bind_method(Vector2i, sign, sarray(), varray()); bind_method(Vector2i, sign, sarray(), varray());
bind_method(Vector2i, abs, sarray(), varray()); bind_method(Vector2i, abs, sarray(), varray());
bind_method(Vector2i, clamp, sarray("min", "max"), varray()); bind_method(Vector2i, clamp, sarray("min", "max"), varray());
@ -1594,6 +1596,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector3i, min_axis_index, sarray(), varray()); bind_method(Vector3i, min_axis_index, sarray(), varray());
bind_method(Vector3i, max_axis_index, sarray(), varray()); bind_method(Vector3i, max_axis_index, sarray(), varray());
bind_method(Vector3i, length, sarray(), varray());
bind_method(Vector3i, length_squared, sarray(), varray());
bind_method(Vector3i, sign, sarray(), varray()); bind_method(Vector3i, sign, sarray(), varray());
bind_method(Vector3i, abs, sarray(), varray()); bind_method(Vector3i, abs, sarray(), varray());
bind_method(Vector3i, clamp, sarray("min", "max"), varray()); bind_method(Vector3i, clamp, sarray("min", "max"), varray());

View file

@ -64,6 +64,19 @@
Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component. Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
</description> </description>
</method> </method>
<method name="length" qualifiers="const">
<return type="float" />
<description>
Returns the length (magnitude) of this vector.
</description>
</method>
<method name="length_squared" qualifiers="const">
<return type="int" />
<description>
Returns the squared length (squared magnitude) of this vector.
This method runs faster than [method length], so prefer it if you need to compare vectors or need the squared distance for some formula.
</description>
</method>
<method name="max_axis_index" qualifiers="const"> <method name="max_axis_index" qualifiers="const">
<return type="int" /> <return type="int" />
<description> <description>

View file

@ -58,6 +58,19 @@
Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component. Returns a new vector with all components clamped between the components of [code]min[/code] and [code]max[/code], by running [method @GlobalScope.clamp] on each component.
</description> </description>
</method> </method>
<method name="length" qualifiers="const">
<return type="float" />
<description>
Returns the length (magnitude) of this vector.
</description>
</method>
<method name="length_squared" qualifiers="const">
<return type="int" />
<description>
Returns the squared length (squared magnitude) of this vector.
This method runs faster than [method length], so prefer it if you need to compare vectors or need the squared distance for some formula.
</description>
</method>
<method name="max_axis_index" qualifiers="const"> <method name="max_axis_index" qualifiers="const">
<return type="int" /> <return type="int" />
<description> <description>