From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- Impostor-dev/src/Impostor.Api/Unity/Mathf.cs | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Impostor-dev/src/Impostor.Api/Unity/Mathf.cs (limited to 'Impostor-dev/src/Impostor.Api/Unity/Mathf.cs') diff --git a/Impostor-dev/src/Impostor.Api/Unity/Mathf.cs b/Impostor-dev/src/Impostor.Api/Unity/Mathf.cs new file mode 100644 index 0000000..4b03417 --- /dev/null +++ b/Impostor-dev/src/Impostor.Api/Unity/Mathf.cs @@ -0,0 +1,54 @@ +namespace Impostor.Api.Unity +{ + public static class Mathf + { + /// + /// Clamps the given value between the given minimum float and maximum float values. Returns the given value if it is within the min and max range. + /// + /// The floating point value to restrict inside the range defined by the min and max values. + /// The minimum floating point value to compare against. + /// The maximum floating point value to compare against. + /// + /// The float result between the min and max values. + /// + public static float Clamp(float value, float min, float max) + { + if (value < (double)min) + { + value = min; + } + else if (value > (double)max) + { + value = max; + } + + return value; + } + + /// + /// Clamps value between 0 and 1 and returns value. + /// + /// Value. + /// Clamped value. + public static float Clamp01(float value) + { + if (value < 0.0) + { + return 0.0f; + } + + return (double)value > 1.0 ? 1f : value; + } + + /// + /// Linearly interpolates between a and b by t. + /// + /// The start value. + /// The end value. + /// The interpolation value between the two floats. + /// + /// The interpolated float result between the two float values. + /// + public static float Lerp(float a, float b, float t) => a + ((b - a) * Clamp01(t)); + } +} \ No newline at end of file -- cgit v1.1-26-g67d0