using UnityEngine;
namespace BracerLib.Utility
{
///
/// Inclusivity determination for a comparison between a lower and upper bound number.
///
public enum BoundsInclusivity
{
///
/// Neither bound is included.
///
None,
///
/// Both bounds are included.
///
Both,
///
/// Left bound is included.
///
Left,
///
/// Right bound is included.
///
Right
}
public static class MathUtility
{
public const float TWO_PI = 2f * Mathf.PI;
///
/// Overflow of a value between two bounds. Values lower than the lowerbound will cycle from the upperbound and vice versa.
///
/// The value to overflow, if needed.
/// The lowerbound number to be checked against.
/// The upperbound number to be checked against.
/// Inclusive value within the bounds.
public static int KeepWithinBounds(int value, int lowerBound, int upperBound)
{
var diff = upperBound - lowerBound + 1;
var v = (value - lowerBound) % diff + diff;
v = v % diff + lowerBound;
return v;
}
///
/// Determine if a value is between a lower and upper bound set of numbers.
///
/// The value to check.
/// The lower bound of the comparison.
/// The upper bound of the comparison.
/// The inclusivity bounds for the comparison.
/// Whether the value is between the two numbers based on the inclusivity passed.
public static bool IsBetween(
int value,
int lowerBound,
int upperBound,
BoundsInclusivity inclusivity = BoundsInclusivity.None
)
{
var result = inclusivity switch
{
BoundsInclusivity.Both => value >= lowerBound && value <= upperBound,
BoundsInclusivity.Left => value >= lowerBound && value < upperBound,
BoundsInclusivity.Right => value > lowerBound && value <= upperBound,
_ => value > lowerBound && value < upperBound
};
return result;
}
///
public static bool IsBetween(
float value,
float lowerBound,
float upperBound,
BoundsInclusivity inclusivity = BoundsInclusivity.None
)
{
var result = inclusivity switch
{
BoundsInclusivity.Both => value >= lowerBound && value <= upperBound,
BoundsInclusivity.Left => value >= lowerBound && value < upperBound,
BoundsInclusivity.Right => value > lowerBound && value <= upperBound,
_ => value > lowerBound && value < upperBound
};
return result;
}
}
}