rj-action-library/Runtime/Math/Range.cs

103 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Godot;
namespace Rokojori
{
[System.Serializable]
public class Range
{
public float min;
public float max;
public Range( float min, float max )
{
this.min = min;
this.max = max;
}
public void EnsureCorrectness()
{
if ( max < min )
{
var b = max; max = min; min = b;
}
}
public bool Contains( float value )
{
return min <= value && value <= max;
}
public bool Overlaps( Range other )
{
if ( other.max < min ) { return false; }
if ( other.min > max ) { return false; }
if ( other.Contains( min ) || other.Contains( max ) )
{
return true;
}
if ( Contains( min ) || Contains( max ) )
{
return true;
}
return false;
}
public float center { get { return 0.5f * ( max + min ); } }
public float range { get { return max - min; } }
public float length { get { return max - min; } }
public float DistanceTo( Range other )
{
var center = this.center;
var otherCenter = other.center;
var range = this.range;
var otherRange = other.range;
var distance = Mathf.Abs( center - otherCenter );
return Mathf.Max( 0, distance - 0.5f * ( range + otherRange ) );
}
public Range Copy()
{
return new Range( min, max );
}
public float SampleAt( float normalized )
{
return min + ( max - min ) * normalized;
}
public static Range Of_01
{
get { return new Range( 0, 1 ); }
}
public static bool Contains( float min, float max, float value )
{
return min <= value && value <= max;
}
public static bool ContainsExclusive( float min, float max, float value )
{
return min < value && value < max;
}
public static bool ContainsExclusiveMax( float min, float max, float value )
{
return min <= value && value < max;
}
}
}