using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Starpelly
{
public static class Mathp
{
///
/// Rounds float to nearest interval.
///
public static float Round2Nearest(float a, float interval)
{
return a = a - (a % interval);
}
///
/// Gets the difference between two floats.
///
public static float Difference(float num1, float num2)
{
float cout;
cout = Mathf.Max(num2, num1) - Mathf.Min(num1, num2);
return cout;
}
///
/// Returns the closest value in a list compared to value given.
///
public static float GetClosestInList(List list, float compareTo)
{
if (list.Count > 0)
return list.Aggregate((x, y) => Mathf.Abs(x - compareTo) < Mathf.Abs(y - compareTo) ? x : y);
else
return -40;
}
///
/// Get the numbers after a decimal.
///
public static float GetDecimalFromFloat(float number)
{
return number % 1; // this is simple as fuck, but i'm dumb and forget this all the time
}
///
/// Converts two numbers to a range of 0 - 1
///
/// The input value.
/// The min input.
/// The max input.
public static float Normalize(float val, float min, float max)
{
return (val - min) / (max - min);
}
///
/// Converts a normalized value to a normal float.
///
/// The normalized value.
/// The min input.
/// The max input.
public static float DeNormalize(float val, float min, float max)
{
return (val * (max - min) + min);
}
///
/// Returns true if a value is within a certain range.
///
public static bool IsWithin(this float val, float min, float max)
{
return val >= min && val <= max;
}
///
/// Returns true if a value is within a certain range.
///
public static bool IsWithin(this Vector2 val, Vector2 min, Vector2 max)
{
return val.x.IsWithin(min.x, max.x) && val.y.IsWithin(min.y, max.y);
}
///
/// Returns true if value is between two numbers.
///
public static bool IsBetween(this T item, T start, T end)
{
return Comparer.Default.Compare(item, start) >= 0
&& Comparer.Default.Compare(item, end) <= 0;
}
}
}