Skip to content

Commit

Permalink
Merge pull request #83163 from Chubercik/vectorXi_dist_methods
Browse files Browse the repository at this point in the history
Implement `Vector2i/3i/4i` methods: `distance_to` and `distance_squared_to`
  • Loading branch information
akien-mga committed Jan 3, 2024
2 parents 7abaac6 + cb954c6 commit 22e880a
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/math/vector2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ struct _NO_DISCARD_ Vector2i {
return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
}

double distance_to(const Vector2i &p_to) const {
return (p_to - *this).length();
}

int64_t distance_squared_to(const Vector2i &p_to) const {
return (p_to - *this).length_squared();
}

Vector2i operator+(const Vector2i &p_v) const;
void operator+=(const Vector2i &p_v);
Vector2i operator-(const Vector2i &p_v) const;
Expand Down
11 changes: 11 additions & 0 deletions core/math/vector3i.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ struct _NO_DISCARD_ Vector3i {
Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const;
Vector3i snapped(const Vector3i &p_step) const;

_FORCE_INLINE_ double distance_to(const Vector3i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector3i &p_to) const;

/* Operators */

_FORCE_INLINE_ Vector3i &operator+=(const Vector3i &p_v);
Expand Down Expand Up @@ -143,6 +146,14 @@ Vector3i Vector3i::sign() const {
return Vector3i(SIGN(x), SIGN(y), SIGN(z));
}

double Vector3i::distance_to(const Vector3i &p_to) const {
return (p_to - *this).length();
}

int64_t Vector3i::distance_squared_to(const Vector3i &p_to) const {
return (p_to - *this).length_squared();
}

/* Operators */

Vector3i &Vector3i::operator+=(const Vector3i &p_v) {
Expand Down
11 changes: 11 additions & 0 deletions core/math/vector4i.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ struct _NO_DISCARD_ Vector4i {

_FORCE_INLINE_ void zero();

_FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
_FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;

_FORCE_INLINE_ Vector4i abs() const;
_FORCE_INLINE_ Vector4i sign() const;
Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
Expand Down Expand Up @@ -139,6 +142,14 @@ double Vector4i::length() const {
return Math::sqrt((double)length_squared());
}

double Vector4i::distance_to(const Vector4i &p_to) const {
return (p_to - *this).length();
}

int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
return (p_to - *this).length_squared();
}

Vector4i Vector4i::abs() const {
return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
}
Expand Down
6 changes: 6 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector2i, aspect, sarray(), varray());
bind_method(Vector2i, max_axis_index, sarray(), varray());
bind_method(Vector2i, min_axis_index, sarray(), varray());
bind_method(Vector2i, distance_to, sarray("to"), varray());
bind_method(Vector2i, distance_squared_to, sarray("to"), varray());
bind_method(Vector2i, length, sarray(), varray());
bind_method(Vector2i, length_squared, sarray(), varray());
bind_method(Vector2i, sign, sarray(), varray());
Expand Down Expand Up @@ -1897,6 +1899,8 @@ static void _register_variant_builtin_methods() {

bind_method(Vector3i, min_axis_index, sarray(), varray());
bind_method(Vector3i, max_axis_index, sarray(), varray());
bind_method(Vector3i, distance_to, sarray("to"), varray());
bind_method(Vector3i, distance_squared_to, sarray("to"), varray());
bind_method(Vector3i, length, sarray(), varray());
bind_method(Vector3i, length_squared, sarray(), varray());
bind_method(Vector3i, sign, sarray(), varray());
Expand Down Expand Up @@ -1943,6 +1947,8 @@ static void _register_variant_builtin_methods() {
bind_method(Vector4i, abs, sarray(), varray());
bind_method(Vector4i, clamp, sarray("min", "max"), varray());
bind_method(Vector4i, snapped, sarray("step"), varray());
bind_method(Vector4i, distance_to, sarray("to"), varray());
bind_method(Vector4i, distance_squared_to, sarray("to"), varray());

/* Plane */

Expand Down
15 changes: 15 additions & 0 deletions doc/classes/Vector2i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@
Returns a new vector with all components clamped between the components of [param min] and [param max], by running [method @GlobalScope.clamp] on each component.
</description>
</method>
<method name="distance_squared_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="Vector2i" />
<description>
Returns the squared distance between this vector and [param to].
This method runs faster than [method distance_to], so prefer it if you need to compare vectors or need the squared distance for some formula.
</description>
</method>
<method name="distance_to" qualifiers="const">
<return type="float" />
<param index="0" name="to" type="Vector2i" />
<description>
Returns the distance between this vector and [param to].
</description>
</method>
<method name="length" qualifiers="const">
<return type="float" />
<description>
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/Vector3i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@
Returns a new vector with all components clamped between the components of [param min] and [param max], by running [method @GlobalScope.clamp] on each component.
</description>
</method>
<method name="distance_squared_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="Vector3i" />
<description>
Returns the squared distance between this vector and [param to].
This method runs faster than [method distance_to], so prefer it if you need to compare vectors or need the squared distance for some formula.
</description>
</method>
<method name="distance_to" qualifiers="const">
<return type="float" />
<param index="0" name="to" type="Vector3i" />
<description>
Returns the distance between this vector and [param to].
</description>
</method>
<method name="length" qualifiers="const">
<return type="float" />
<description>
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/Vector4i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@
Returns a new vector with all components clamped between the components of [param min] and [param max], by running [method @GlobalScope.clamp] on each component.
</description>
</method>
<method name="distance_squared_to" qualifiers="const">
<return type="int" />
<param index="0" name="to" type="Vector4i" />
<description>
Returns the squared distance between this vector and [param to].
This method runs faster than [method distance_to], so prefer it if you need to compare vectors or need the squared distance for some formula.
</description>
</method>
<method name="distance_to" qualifiers="const">
<return type="float" />
<param index="0" name="to" type="Vector4i" />
<description>
Returns the distance between this vector and [param to].
</description>
</method>
<method name="length" qualifiers="const">
<return type="float" />
<description>
Expand Down
23 changes: 23 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2I.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ public readonly Vector2I Clamp(Vector2I min, Vector2I max)
);
}

/// <summary>
/// Returns the squared distance between this vector and <paramref name="to"/>.
/// This method runs faster than <see cref="DistanceTo"/>, so prefer it if
/// you need to compare vectors or need the squared distance for some formula.
/// </summary>
/// <param name="to">The other vector to use.</param>
/// <returns>The squared distance between the two vectors.</returns>
public readonly int DistanceSquaredTo(Vector2I to)
{
return (to - this).LengthSquared();
}

/// <summary>
/// Returns the distance between this vector and <paramref name="to"/>.
/// </summary>
/// <seealso cref="DistanceSquaredTo(Vector2I)"/>
/// <param name="to">The other vector to use.</param>
/// <returns>The distance between the two vectors.</returns>
public readonly real_t DistanceTo(Vector2I to)
{
return (to - this).Length();
}

/// <summary>
/// Returns the length (magnitude) of this vector.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3I.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ public readonly Vector3I Clamp(Vector3I min, Vector3I max)
);
}

/// <summary>
/// Returns the squared distance between this vector and <paramref name="to"/>.
/// This method runs faster than <see cref="DistanceTo"/>, so prefer it if
/// you need to compare vectors or need the squared distance for some formula.
/// </summary>
/// <param name="to">The other vector to use.</param>
/// <returns>The squared distance between the two vectors.</returns>
public readonly int DistanceSquaredTo(Vector3I to)
{
return (to - this).LengthSquared();
}

/// <summary>
/// Returns the distance between this vector and <paramref name="to"/>.
/// </summary>
/// <seealso cref="DistanceSquaredTo(Vector3I)"/>
/// <param name="to">The other vector to use.</param>
/// <returns>The distance between the two vectors.</returns>
public readonly real_t DistanceTo(Vector3I to)
{
return (to - this).Length();
}

/// <summary>
/// Returns the length (magnitude) of this vector.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Vector4I.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ public readonly Vector4I Clamp(Vector4I min, Vector4I max)
);
}

/// <summary>
/// Returns the squared distance between this vector and <paramref name="to"/>.
/// This method runs faster than <see cref="DistanceTo"/>, so prefer it if
/// you need to compare vectors or need the squared distance for some formula.
/// </summary>
/// <param name="to">The other vector to use.</param>
/// <returns>The squared distance between the two vectors.</returns>
public readonly int DistanceSquaredTo(Vector4I to)
{
return (to - this).LengthSquared();
}

/// <summary>
/// Returns the distance between this vector and <paramref name="to"/>.
/// </summary>
/// <seealso cref="DistanceSquaredTo(Vector4I)"/>
/// <param name="to">The other vector to use.</param>
/// <returns>The distance between the two vectors.</returns>
public readonly real_t DistanceTo(Vector4I to)
{
return (to - this).Length();
}

/// <summary>
/// Returns the length (magnitude) of this vector.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions tests/core/math/test_vector2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ TEST_CASE("[Vector2i] Length methods") {
CHECK_MESSAGE(
vector2.length() == doctest::Approx(36.05551275463989293119),
"Vector2i length should work as expected.");
CHECK_MESSAGE(
vector1.distance_squared_to(vector2) == 500,
"Vector2i distance_squared_to should work as expected and return exact result.");
CHECK_MESSAGE(
vector1.distance_to(vector2) == doctest::Approx(22.36067977499789696409),
"Vector2i distance_to should work as expected.");
}

TEST_CASE("[Vector2i] Operators") {
Expand Down
6 changes: 6 additions & 0 deletions tests/core/math/test_vector3i.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ TEST_CASE("[Vector3i] Length methods") {
CHECK_MESSAGE(
vector2.length() == doctest::Approx(53.8516480713450403125),
"Vector3i length should work as expected.");
CHECK_MESSAGE(
vector1.distance_squared_to(vector2) == 1400,
"Vector3i distance_squared_to should work as expected and return exact result.");
CHECK_MESSAGE(
vector1.distance_to(vector2) == doctest::Approx(37.41657386773941385584),
"Vector3i distance_to should work as expected.");
}

TEST_CASE("[Vector3i] Operators") {
Expand Down
6 changes: 6 additions & 0 deletions tests/core/math/test_vector4i.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ TEST_CASE("[Vector4i] Length methods") {
CHECK_MESSAGE(
vector2.length() == doctest::Approx(73.4846922835),
"Vector4i length should work as expected.");
CHECK_MESSAGE(
vector1.distance_squared_to(vector2) == 3000,
"Vector4i distance_squared_to should work as expected.");
CHECK_MESSAGE(
vector1.distance_to(vector2) == doctest::Approx(54.772255750517),
"Vector4i distance_to should work as expected.");
}

TEST_CASE("[Vector4i] Operators") {
Expand Down

0 comments on commit 22e880a

Please sign in to comment.