using System;
namespace MonoGame.Extended
{
///
/// Represents a closed interval defined by a minimum and a maximum value of a give type.
///
public struct Range : IEquatable> where T : IComparable
{
public Range(T min, T max)
{
if (min.CompareTo(max) > 0 || max.CompareTo(min) < 0)
throw new ArgumentException("Min has to be smaller than or equal to max.");
Min = min;
Max = max;
}
public Range(T value)
: this(value, value)
{
}
///
/// Gets the minium value of the .
///
public T Min { get; }
///
/// Gets the maximum value of the .
///
public T Max { get; }
///
/// Returns wheter or not this is degenerate.
/// (Min and Max are the same)
///
public bool IsDegenerate => Min.Equals(Max);
///
/// Returns wheter or not this is proper.
/// (Min and Max are not the same)
///
public bool IsProper => !Min.Equals(Max);
public bool Equals(Range value) => Min.Equals(value.Min) && Max.Equals(value.Max);
public override bool Equals(object obj) => obj is Range && Equals((Range) obj);
public override int GetHashCode() => Min.GetHashCode() ^ Max.GetHashCode();
public static bool operator ==(Range value1, Range value2) => value1.Equals(value2);
public static bool operator !=(Range value1, Range value2) => !value1.Equals(value2);
public static implicit operator Range(T value) => new Range(value, value);
public override string ToString() => $"Range<{typeof(T).Name}> [{Min} {Max}]";
///
/// Returns wheter or not the value falls in this .
///
public bool IsInBetween(T value, bool minValueExclusive = false, bool maxValueExclusive = false)
{
if (minValueExclusive)
{
if (value.CompareTo(Min) <= 0)
return false;
}
if (value.CompareTo(Min) < 0)
return false;
if (maxValueExclusive)
{
if (value.CompareTo(Max) >= 0)
return false;
}
return value.CompareTo(Max) <= 0;
}
}
}