Skip to content

Instantly share code, notes, and snippets.

@cjacobwade
Created December 9, 2017 00:18
Show Gist options
  • Save cjacobwade/e35b887509a24f4be7ff93a94742a823 to your computer and use it in GitHub Desktop.
Save cjacobwade/e35b887509a24f4be7ff93a94742a823 to your computer and use it in GitHub Desktop.
public struct FloatRange
{
public float min;
public float max;
public FloatRange(float inMin, float inMax)
{
min = inMin;
max = inMax;
}
public float Clamp( float v )
{ return Mathf.Clamp( v, min, max ); }
public int Clamp(int v)
{ return Mathf.Clamp(v, (int)min, (int)max); }
public float Lerp( float t )
{ return Mathf.Lerp( min, max, t ); }
public static FloatRange Lerp(FloatRange a, FloatRange b, float alpha)
{
a.min = Mathf.Lerp(a.min, b.min, alpha);
a.max = Mathf.Lerp(a.max, b.max, alpha);
return a;
}
public float UnclampedLerp( float t )
{ return min + ( max - min ) * t; }
public float InverseLerp( float value )
{ return Mathf.InverseLerp(min, max, value); }
public float UnclampedInverseLerp( float value )
{ return (value - min) / (max - min); }
public float Range
{ get { return max - min; } }
public float Random
{ get { return UnityEngine.Random.Range( min, max ); } }
public bool Contains( float v )
{ return v > min && v < max; }
public bool Contains(int v)
{ return v > min && v < max; }
public float Midpoint
{ get { return ( min + max ) * 0.5f; } }
public static FloatRange operator +(FloatRange a, FloatRange b)
{
a.min += b.min;
a.max += b.max;
return a;
}
public static FloatRange operator *(FloatRange floatRange, float x)
{
floatRange.min *= x;
floatRange.max *= x;
return floatRange;
}
}
[System.Serializable]
public struct IntRange
{
public int min;
public int max;
public IntRange(int inMin, int inMax)
{
min = inMin;
max = inMax;
}
public float Clamp(float v)
{ return Mathf.Clamp(v, min, max); }
public int Clamp(int v)
{ return Mathf.Clamp(v, min, max); }
public float Lerp(float t)
{ return Mathf.Lerp(min, max, t); }
public float UnclampedLerp(float t)
{ return min + (max - min) * t; }
public float InverseLerp(float value)
{ return Mathf.InverseLerp(min, max, value); }
public float UnclampedInverseLerp(float value)
{ return (value - min) / (max - min); }
public int Range
{ get { return max - min; } }
public int Random
{ get { return UnityEngine.Random.Range(min, max); } }
public bool Contains(float v)
{ return v > min && v < max; }
public bool Contains(int v)
{ return v > min && v < max; }
public float Midpoint
{ get { return (min + max) * 0.5f; } }
public static IntRange operator +(IntRange a, IntRange b)
{
a.min += b.min;
a.max += b.max;
return a;
}
public static IntRange operator *(IntRange intRange, int x)
{
intRange.min *= x;
intRange.max *= x;
return intRange;
}
}
[System.Serializable]
public struct Vec2Range
{
public Vector2 min;
public Vector2 max;
public Vec2Range(Vector2 _min, Vector2 _max)
{
min = _min;
max = _max;
}
public Vector2 Clamp(Vector2 v)
{ return WadeUtils.Clamp(v, min, max); }
public Vector2 Lerp(float t)
{ return Vector2.Lerp(min, max, t); }
public Vector2 UnclampedLerp(float t)
{ return min + (max - min) * t; }
public Vector2 Range
{ get { return max - min; } }
public Vector2 Random
{
get
{
return new Vector2(UnityEngine.Random.Range(min.x, max.x),
UnityEngine.Random.Range(min.y, max.y));
}
}
public bool Contains(Vector2 v)
{
return v.x > min.x && v.x < max.x &&
v.y > min.y && v.y < max.y;
}
public Vector2 Midpoint
{ get { return (min + max) * 0.5f; } }
public static Vec2Range operator +(Vec2Range a, Vec2Range b)
{
a.min += b.min;
a.max += b.max;
return a;
}
public static Vec2Range operator *(Vec2Range floatRange, float x)
{
floatRange.min *= x;
floatRange.max *= x;
return floatRange;
}
public static Vec2Range Lerp(Vec2Range a, Vec2Range b, float alpha)
{
a.min = Vector2.Lerp(a.min, b.min, alpha);
a.max = Vector2.Lerp(a.max, b.max, alpha);
return a;
}
}
[System.Serializable]
public struct Vec3Range
{
public Vector3 min;
public Vector3 max;
public Vec3Range(Vector3 _min, Vector3 _max)
{
min = _min;
max = _max;
}
public Vector3 Clamp(Vector3 v)
{ return WadeUtils.Clamp(v, min, max); }
public Vector3 Lerp(float t)
{ return Vector3.Lerp(min, max, t); }
public Vector3 UnclampedLerp(float t)
{ return min + (max - min) * t; }
public Vector3 Range
{ get { return max - min; } }
public Vector3 Random
{
get
{
return new Vector3(UnityEngine.Random.Range(min.x, max.x),
UnityEngine.Random.Range(min.y, max.y),
UnityEngine.Random.Range(min.z, max.z));
}
}
public bool Contains(Vector3 v)
{
return v.x > min.x && v.x < max.x &&
v.y > min.y && v.y < max.y &&
v.z > min.z && v.z < max.z;
}
public Vector3 Midpoint
{ get { return (min + max) * 0.5f; } }
public static Vec3Range operator +(Vec3Range a, Vec3Range b)
{
a.min += b.min;
a.max += b.max;
return a;
}
public static Vec3Range operator *(Vec3Range floatRange, float x)
{
floatRange.min *= x;
floatRange.max *= x;
return floatRange;
}
public static Vec3Range Lerp(Vec3Range a, Vec3Range b, float alpha)
{
a.min = Vector3.Lerp(a.min, b.min, alpha);
a.max = Vector3.Lerp(a.max, b.max, alpha);
return a;
}
}
[System.Serializable]
public struct Vec4Range
{
public Vector4 min;
public Vector4 max;
public Vec4Range(Vector4 _min, Vector4 _max)
{
min = _min;
max = _max;
}
public Vector4 Clamp(Vector4 v)
{ return WadeUtils.Clamp(v, min, max); }
public Vector4 Lerp(float t)
{ return Vector4.Lerp(min, max, t); }
public Vector4 UnclampedLerp(float t)
{ return min + (max - min) * t; }
public Vector4 Range
{ get { return max - min; } }
public Vector4 Random
{
get
{
return new Vector4(UnityEngine.Random.Range(min.x, max.y),
UnityEngine.Random.Range(min.y, max.y),
UnityEngine.Random.Range(min.z, max.z),
UnityEngine.Random.Range(min.w, max.w));
}
}
public bool Contains(Vector4 v)
{
return v.x > min.x && v.x < max.x &&
v.y > min.y && v.y < max.y &&
v.z > min.z && v.z < max.z &&
v.w > min.w && v.w < max.w;
}
public Vector4 Midpoint
{ get { return (min + max) * 0.5f; } }
public static Vec4Range operator +(Vec4Range a, Vec4Range b)
{
a.min += b.min;
a.max += b.max;
return a;
}
public static Vec4Range operator *(Vec4Range floatRange, float x)
{
floatRange.min *= x;
floatRange.max *= x;
return floatRange;
}
public static Vec4Range Lerp(Vec4Range a, Vec4Range b, float alpha)
{
a.min = Vector4.Lerp(a.min, b.min, alpha);
a.max = Vector4.Lerp(a.max, b.max, alpha);
return a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment