API FloatRange - shmellyorc/Box GitHub Wiki

FloatRange

Namespace: Box.Utils.Ranges

Represents a floating-point range with an enforced minimum and maximum, and a current value automatically clamped between those bounds. Provides utilities for normalization, random sampling, and range queries.


Constructors

public FloatRange(float value, float min, float max)

Create a range where Min = min, Max = max (adjusted so Max ≥ Min), and Value is clamped accordingly.

public FloatRange(float value, float max)

Shortcut constructor: Min = 0, Max = max, and Value is clamped.

public FloatRange(float value)

Shortcut constructor: Min = 0, Max = value, and Value = max.


Properties

Name Type Description
static FloatRange Zero FloatRange A range with Min=0, Max=0, Value=0.
bool IsZero bool True when Min, Max, and Value are all zero.
float Min float Minimum bound. Setting above Max adjusts Max = Min. Triggers re-clamp of Value.
float Max float Maximum bound. Setting below Min adjusts Min = Max. Triggers re-clamp of Value.
float Value float Current value, always clamped between Min and Max.
float Sum float Returns Min + Max.
float Total float Returns Max - Min.
float Percent float Normalized position of Value within [Min,Max] (0–1).
bool AtStart bool True if Value is at or below Min.
bool AtEnd bool True if Value is at or above Max.

Methods

Signature Description
float Clamp(float value) Clamp an arbitrary value to the current range.
float Random() Generate a random float between Min and Max via FastRandom.Instance.
override string ToString() Returns a string in "[Min .. Max] = Value" format.

Usage Example

// Create a range from 10 to 20, initial Value=15
var range = new FloatRange(15f, 10f, 20f);

// Access properties
debug.Assert(range.Min == 10f);
debug.Assert(range.Max == 20f);
debug.Assert(range.Value == 15f);

// Clamp assignment
range.Value = 25f; // Value becomes 20 (clamped)

// Normalized percent
float p = range.Percent; // (20-10)/(20-10) = 1.0

// Random sample within the range
float sample = range.Random(); // e.g. 13.7

// Adjust bounds
tange.Min = 5f; // Min=5, Max=20, Value re-clamped if needed

// Utility methods
float c = range.Clamp(100f); // 20
string s = range.ToString(); // "[5 .. 20] = 20"