Skip to content

Commit

Permalink
Adds normalize() method
Browse files Browse the repository at this point in the history
  • Loading branch information
CrayolaEater committed Dec 29, 2024
1 parent f952bfe commit 4696dfe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
}

template <typename T>
constexpr auto NORMALIZE(const T m_x, const T m_min, const T m_max, const T m_a, const T m_b) {
return m_a + (((m_x - m_min) * (m_b - m_a)) / (m_max + m_min));
}

// Generic swap template.
#ifndef SWAP
#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
Expand Down
7 changes: 7 additions & 0 deletions core/variant/variant_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,11 @@ double VariantUtilityFunctions::minf(double x, double y) {
return MIN(x, y);
}

double VariantUtilityFunctions::normalize(double x, double min, double max, double range_left, double range_right)
{
return NORMALIZE(x, min, max, range_left, range_right);
}

int64_t VariantUtilityFunctions::mini(int64_t x, int64_t y) {
return MIN(x, y);
}
Expand Down Expand Up @@ -1780,6 +1785,8 @@ void Variant::_register_variant_utility_functions() {
FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);

FUNCBINDR(normalize, sarray("x", "min", "max", "a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);

FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct VariantUtilityFunctions {
static Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
static double minf(double x, double y);
static int64_t mini(int64_t x, int64_t y);
static double normalize(double x, double min, double max, double range_left, double range_right);
static Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error);
static double clampf(double x, double min, double max);
static int64_t clampi(int64_t x, int64_t min, int64_t max);
Expand Down
18 changes: 18 additions & 0 deletions doc/classes/@GlobalScope.xml
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,24 @@
[/codeblock]
</description>
</method>
<method name="normalize">
<return type="float" />
<param index="0" name="x" type="float" />
<param index="1" name="min" type="float" />
<param index="2" name="max" type="float" />
<param index="1" name="a" type="float" />
<param index="1" name="b" type="float" />
<description>
Returns a normalized value of [param x] accross the interval [[param a], [param b]].
[param min] and [param max] are the minimum and maximum values that initial [param x] can have.
The most common use is to map any values to the range [code][0, 1][/code] but any range can be set.
[codeblock]
normalize(30, 0, 100, 0, 1) # Returns 0.3
normalize(0.25, 0, 1, 0, 10) # Returns 2.5
normalize(0.5, 0, 1, 2, 6) # Returns 4.0
[/codeblock]
</description>
</method>
<method name="move_toward">
<return type="float" />
<param index="0" name="from" type="float" />
Expand Down

0 comments on commit 4696dfe

Please sign in to comment.