summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorAaron Franke <arnfranke@yahoo.com>2021-11-29 11:13:31 -0600
committerAaron Franke <arnfranke@yahoo.com>2022-01-06 10:06:56 -0800
commit2c52f1646480506d6647ec71e6ea14935caf2e7e (patch)
tree0f23363d6388499376538e2fd340d412d87f3e99 /core/math
parent1dee3e0cc7590480a77d5bb50a4c90e0286c5e50 (diff)
Add length and length_squared to Vector2i/3i
Diffstat (limited to 'core/math')
-rw-r--r--core/math/vector2.cpp8
-rw-r--r--core/math/vector2.h3
-rw-r--r--core/math/vector3i.h12
3 files changed, 23 insertions, 0 deletions
diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp
index 38dad893f5..676a0004ea 100644
--- a/core/math/vector2.cpp
+++ b/core/math/vector2.cpp
@@ -210,6 +210,14 @@ Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
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 {
return Vector2i(x + p_v.x, y + p_v.y);
}
diff --git a/core/math/vector2.h b/core/math/vector2.h
index 493e0af27d..588c0201fb 100644
--- a/core/math/vector2.h
+++ b/core/math/vector2.h
@@ -344,6 +344,9 @@ struct Vector2i {
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; }
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
diff --git a/core/math/vector3i.h b/core/math/vector3i.h
index 0f9caa349b..1416c98057 100644
--- a/core/math/vector3i.h
+++ b/core/math/vector3i.h
@@ -31,6 +31,7 @@
#ifndef VECTOR3I_H
#define VECTOR3I_H
+#include "core/math/math_funcs.h"
#include "core/string/ustring.h"
#include "core/typedefs.h"
@@ -65,6 +66,9 @@ struct Vector3i {
Vector3i::Axis min_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_ 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 {
return Vector3i(ABS(x), ABS(y), ABS(z));
}