Merge pull request #1437 from AThousandShips/vec_elem_scalar

Add scalar versions of `Vector*` `min/max/clamp/snap(ped)`
This commit is contained in:
David Snopek
2024-05-07 12:55:23 -05:00
committed by GitHub
15 changed files with 191 additions and 19 deletions

View File

@@ -137,12 +137,24 @@ Vector2 Vector2::clamp(const Vector2 &p_min, const Vector2 &p_max) const {
CLAMP(y, p_min.y, p_max.y));
}
Vector2 Vector2::clampf(real_t p_min, real_t p_max) const {
return Vector2(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max));
}
Vector2 Vector2::snapped(const Vector2 &p_step) const {
return Vector2(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y));
}
Vector2 Vector2::snappedf(real_t p_step) const {
return Vector2(
Math::snapped(x, p_step),
Math::snapped(y, p_step));
}
Vector2 Vector2::limit_length(const real_t p_len) const {
const real_t l = length();
Vector2 v = *this;

View File

@@ -35,12 +35,30 @@
namespace godot {
Vector2i Vector2i::snapped(const Vector2i &p_step) const {
return Vector2i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y));
}
Vector2i Vector2i::snappedi(int32_t p_step) const {
return Vector2i(
Math::snapped(x, p_step),
Math::snapped(y, p_step));
}
Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const {
return Vector2i(
CLAMP(x, p_min.x, p_max.x),
CLAMP(y, p_min.y, p_max.y));
}
Vector2i Vector2i::clampi(int32_t p_min, int32_t p_max) const {
return Vector2i(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max));
}
int64_t Vector2i::length_squared() const {
return x * (int64_t)x + y * (int64_t)y;
}

View File

@@ -54,18 +54,37 @@ Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
CLAMP(z, p_min.z, p_max.z));
}
Vector3 Vector3::clampf(real_t p_min, real_t p_max) const {
return Vector3(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max));
}
void Vector3::snap(const Vector3 p_step) {
x = Math::snapped(x, p_step.x);
y = Math::snapped(y, p_step.y);
z = Math::snapped(z, p_step.z);
}
void Vector3::snapf(real_t p_step) {
x = Math::snapped(x, p_step);
y = Math::snapped(y, p_step);
z = Math::snapped(z, p_step);
}
Vector3 Vector3::snapped(const Vector3 p_step) const {
Vector3 v = *this;
v.snap(p_step);
return v;
}
Vector3 Vector3::snappedf(real_t p_step) const {
Vector3 v = *this;
v.snapf(p_step);
return v;
}
Vector3 Vector3::limit_length(const real_t p_len) const {
const real_t l = length();
Vector3 v = *this;

View File

@@ -35,6 +35,20 @@
namespace godot {
Vector3i Vector3i::snapped(const Vector3i &p_step) const {
return Vector3i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z));
}
Vector3i Vector3i::snappedi(int32_t p_step) const {
return Vector3i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step));
}
Vector3i::Axis Vector3i::min_axis_index() const {
return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z);
}
@@ -50,6 +64,13 @@ Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const {
CLAMP(z, p_min.z, p_max.z));
}
Vector3i Vector3i::clampi(int32_t p_min, int32_t p_max) const {
return Vector3i(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max));
}
Vector3i::operator String() const {
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ")";
}

View File

@@ -173,12 +173,25 @@ void Vector4::snap(const Vector4 &p_step) {
w = Math::snapped(w, p_step.w);
}
void Vector4::snapf(real_t p_step) {
x = Math::snapped(x, p_step);
y = Math::snapped(y, p_step);
z = Math::snapped(z, p_step);
w = Math::snapped(w, p_step);
}
Vector4 Vector4::snapped(const Vector4 &p_step) const {
Vector4 v = *this;
v.snap(p_step);
return v;
}
Vector4 Vector4::snappedf(real_t p_step) const {
Vector4 v = *this;
v.snapf(p_step);
return v;
}
Vector4 Vector4::inverse() const {
return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w);
}
@@ -191,6 +204,14 @@ Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
CLAMP(w, p_min.w, p_max.w));
}
Vector4 Vector4::clampf(real_t p_min, real_t p_max) const {
return Vector4(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max),
CLAMP(w, p_min, p_max));
}
Vector4::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
}

View File

@@ -35,6 +35,22 @@
namespace godot {
Vector4i Vector4i::snapped(const Vector4i &p_step) const {
return Vector4i(
Math::snapped(x, p_step.x),
Math::snapped(y, p_step.y),
Math::snapped(z, p_step.z),
Math::snapped(w, p_step.w));
}
Vector4i Vector4i::snappedi(int32_t p_step) const {
return Vector4i(
Math::snapped(x, p_step),
Math::snapped(y, p_step),
Math::snapped(z, p_step),
Math::snapped(w, p_step));
}
Vector4i::Axis Vector4i::min_axis_index() const {
uint32_t min_index = 0;
int32_t min_value = x;
@@ -67,6 +83,14 @@ Vector4i Vector4i::clamp(const Vector4i &p_min, const Vector4i &p_max) const {
CLAMP(w, p_min.w, p_max.w));
}
Vector4i Vector4i::clampi(int32_t p_min, int32_t p_max) const {
return Vector4i(
CLAMP(x, p_min, p_max),
CLAMP(y, p_min, p_max),
CLAMP(z, p_min, p_max),
CLAMP(w, p_min, p_max));
}
Vector4i::operator String() const {
return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ", " + itos(w) + ")";
}