Skip to content

Commit

Permalink
Added Min, Max, Abs, and Clamp for VectorInt types
Browse files Browse the repository at this point in the history
Added missing Min, Max, Abs, and Clamp Functions for Vector2Int and Vector3Int.
  • Loading branch information
chadefranklin committed Aug 16, 2022
1 parent 405eea0 commit 4b557c4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
36 changes: 36 additions & 0 deletions Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public static Rect Encapsulate( this Rect r, Vector2 p ) {
/// <inheritdoc cref="Mathfs.Abs(Vector4)"/>
[MethodImpl( INLINE )] public static Vector4 Abs( this Vector4 v ) => Mathfs.Abs( v );

/// <inheritdoc cref="Mathfs.Abs(Vector2Int)"/>
[MethodImpl( INLINE )] public static Vector2Int Abs( this Vector2Int v ) => Mathfs.Abs( v );

/// <inheritdoc cref="Mathfs.Abs(Vector3Int)"/>
[MethodImpl( INLINE )] public static Vector3Int Abs( this Vector3Int v ) => Mathfs.Abs( v );

#endregion

#region Clamping
Expand All @@ -260,6 +266,12 @@ public static Rect Encapsulate( this Rect r, Vector2 p ) {
/// <inheritdoc cref="Mathfs.Clamp(Vector4,Vector4,Vector4)"/>
[MethodImpl( INLINE )] public static Vector4 Clamp( this Vector4 v, Vector4 min, Vector4 max ) => Mathfs.Clamp( v, min, max );

/// <inheritdoc cref="Mathfs.Clamp(Vector2Int,Vector2Int,Vector2Int)"/>
[MethodImpl( INLINE )] public static Vector2Int Clamp( this Vector2Int v, Vector2Int min, Vector2Int max ) => Mathfs.Clamp( v, min, max );

/// <inheritdoc cref="Mathfs.Clamp(Vector3Int,Vector3Int,Vector3Int)"/>
[MethodImpl( INLINE )] public static Vector3Int Clamp( this Vector3Int v, Vector3Int min, Vector3Int max ) => Mathfs.Clamp( v, min, max );

/// <inheritdoc cref="Mathfs.Clamp(int,int,int)"/>
[MethodImpl( INLINE )] public static int Clamp( this int value, int min, int max ) => Mathfs.Clamp( value, min, max );

Expand All @@ -275,6 +287,12 @@ public static Rect Encapsulate( this Rect r, Vector2 p ) {
/// <inheritdoc cref="Mathfs.Clamp01(Vector4)"/>
[MethodImpl( INLINE )] public static Vector4 Clamp01( this Vector4 v ) => Mathfs.Clamp01( v );

/// <inheritdoc cref="Mathfs.Clamp01(Vector2Int)"/>
[MethodImpl( INLINE )] public static Vector2Int Clamp01( this Vector2Int v ) => Mathfs.Clamp01( v );

/// <inheritdoc cref="Mathfs.Clamp01(Vector3Int)"/>
[MethodImpl( INLINE )] public static Vector3Int Clamp01( this Vector3Int v ) => Mathfs.Clamp01( v );

/// <inheritdoc cref="Mathfs.ClampNeg1to1(float)"/>
[MethodImpl( INLINE )] public static float ClampNeg1to1( this float value ) => Mathfs.ClampNeg1to1( value );

Expand All @@ -287,6 +305,12 @@ public static Rect Encapsulate( this Rect r, Vector2 p ) {
/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector4)"/>
[MethodImpl( INLINE )] public static Vector4 ClampNeg1to1( this Vector4 v ) => Mathfs.ClampNeg1to1( v );

/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector2Int)"/>
[MethodImpl( INLINE )] public static Vector2Int ClampNeg1to1( this Vector2Int v ) => Mathfs.ClampNeg1to1( v );

/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector3Int)"/>
[MethodImpl( INLINE )] public static Vector3Int ClampNeg1to1( this Vector3Int v ) => Mathfs.ClampNeg1to1( v );

#endregion

#region Min & Max
Expand All @@ -309,6 +333,18 @@ public static Rect Encapsulate( this Rect r, Vector2 p ) {
/// <inheritdoc cref="Mathfs.Max(Vector4)"/>
[MethodImpl( INLINE )] public static float Max( this Vector4 v ) => Mathfs.Max( v );

/// <inheritdoc cref="Mathfs.Min(Vector2Int)"/>
[MethodImpl( INLINE )] public static int Min( this Vector2Int v ) => Mathfs.Min( v );

/// <inheritdoc cref="Mathfs.Min(Vector3Int)"/>
[MethodImpl( INLINE )] public static int Min( this Vector3Int v ) => Mathfs.Min( v );

/// <inheritdoc cref="Mathfs.Max(Vector2Int)"/>
[MethodImpl( INLINE )] public static int Max( this Vector2Int v ) => Mathfs.Max( v );

/// <inheritdoc cref="Mathfs.Max(Vector3Int)"/>
[MethodImpl( INLINE )] public static int Max( this Vector3Int v ) => Mathfs.Max( v );

#endregion

#region Signs & Rounding
Expand Down
67 changes: 65 additions & 2 deletions Mathfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ public static ulong BinomialCoef( uint n, uint k ) {
/// <inheritdoc cref="Mathfs.Abs(Vector2)"/>
[MethodImpl( INLINE )] public static Vector4 Abs( Vector4 v ) => new Vector4( Abs( v.x ), Abs( v.y ), Abs( v.z ), Abs( v.w ) );

/// <inheritdoc cref="Mathfs.Abs(Vector2)"/>
[MethodImpl( INLINE )] public static Vector2Int Abs( Vector2Int v ) => new Vector2Int( Abs( v.x ), Abs( v.y ) );

/// <inheritdoc cref="Mathfs.Abs(Vector2)"/>
[MethodImpl( INLINE )] public static Vector3Int Abs( Vector3Int v ) => new Vector3Int( Abs( v.x ), Abs( v.y ), Abs( v.z ));

#endregion

#region Clamping
Expand Down Expand Up @@ -335,6 +341,21 @@ public static Vector4 Clamp( Vector4 v, Vector4 min, Vector4 max ) =>
v.w < min.w ? min.w : v.w > max.w ? max.w : v.w
);

/// <inheritdoc cref="Mathfs.Clamp(Vector2,Vector2,Vector2)"/>
public static Vector2Int Clamp( Vector2Int v, Vector2Int min, Vector2Int max ) =>
new Vector2Int(
v.x < min.x ? min.x : v.x > max.x ? max.x : v.x,
v.y < min.y ? min.y : v.y > max.y ? max.y : v.y
);

/// <inheritdoc cref="Mathfs.Clamp(Vector2,Vector2,Vector2)"/>
public static Vector3Int Clamp( Vector3Int v, Vector3Int min, Vector3Int max ) =>
new Vector3Int(
v.x < min.x ? min.x : v.x > max.x ? max.x : v.x,
v.y < min.y ? min.y : v.y > max.y ? max.y : v.y,
v.z < min.z ? min.z : v.z > max.z ? max.z : v.z
);

/// <inheritdoc cref="Mathfs.Clamp(float,float,float)"/>
public static int Clamp( int value, int min, int max ) => value < min ? min : value > max ? max : value;

Expand Down Expand Up @@ -365,6 +386,21 @@ public static Vector4 Clamp01( Vector4 v ) =>
v.w < 0f ? 0f : v.w > 1f ? 1f : v.w
);

/// <inheritdoc cref="Mathfs.Clamp01(Vector2)"/>
public static Vector2Int Clamp01( Vector2Int v ) =>
new Vector2Int(
v.x < 0 ? 0 : v.x > 1 ? 1 : v.x,
v.y < 0 ? 0 : v.y > 1 ? 1 : v.y
);

/// <inheritdoc cref="Mathfs.Clamp01(Vector2)"/>
public static Vector3Int Clamp01( Vector3Int v ) =>
new Vector3Int(
v.x < 0 ? 0 : v.x > 1 ? 1 : v.x,
v.y < 0 ? 0 : v.y > 1 ? 1 : v.y,
v.z < 0 ? 0 : v.z > 1 ? 1 : v.z
);

/// <summary>Clamps the value between -1 and 1</summary>
public static float ClampNeg1to1( float value ) => value < -1f ? -1f : value > 1f ? 1f : value;

Expand All @@ -375,15 +411,15 @@ public static Vector2 ClampNeg1to1( Vector2 v ) =>
v.y < -1f ? -1f : v.y > 1f ? 1f : v.y
);

/// <summary>Clamps each component between -1 and 1</summary>
/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector2)"/>
public static Vector3 ClampNeg1to1( Vector3 v ) =>
new Vector3(
v.x < -1f ? -1f : v.x > 1f ? 1f : v.x,
v.y < -1f ? -1f : v.y > 1f ? 1f : v.y,
v.z < -1f ? -1f : v.z > 1f ? 1f : v.z
);

/// <summary>Clamps each component between -1 and 1</summary>
/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector2)"/>
public static Vector4 ClampNeg1to1( Vector4 v ) =>
new Vector4(
v.x < -1f ? -1f : v.x > 1f ? 1f : v.x,
Expand All @@ -392,6 +428,21 @@ public static Vector4 ClampNeg1to1( Vector4 v ) =>
v.w < -1f ? -1f : v.w > 1f ? 1f : v.w
);

/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector2)"/>
public static Vector2Int ClampNeg1to1( Vector2Int v ) =>
new Vector2Int(
v.x < -1 ? -1 : v.x > 1 ? 1 : v.x,
v.y < -1 ? -1 : v.y > 1 ? 1 : v.y
);

/// <inheritdoc cref="Mathfs.ClampNeg1to1(Vector2)"/>
public static Vector3Int ClampNeg1to1( Vector3Int v ) =>
new Vector3Int(
v.x < -1 ? -1 : v.x > 1 ? 1 : v.x,
v.y < -1 ? -1 : v.y > 1 ? 1 : v.y,
v.z < -1 ? -1 : v.z > 1 ? 1 : v.z
);

#endregion

#region Min & Max
Expand Down Expand Up @@ -462,6 +513,18 @@ public static Vector4 ClampNeg1to1( Vector4 v ) =>
/// <inheritdoc cref="Mathfs.Max(Vector2)"/>
[MethodImpl( INLINE )] public static float Max( Vector4 v ) => Max( v.x, v.y, v.z, v.w );

/// <inheritdoc cref="Mathfs.Min(Vector2)"/>
[MethodImpl( INLINE )] public static int Min( Vector2Int v ) => Min( v.x, v.y );

/// <inheritdoc cref="Mathfs.Min(Vector2)"/>
[MethodImpl( INLINE )] public static int Min( Vector3Int v ) => Min( v.x, v.y, v.z );

/// <inheritdoc cref="Mathfs.Max(Vector2)"/>
[MethodImpl( INLINE )] public static int Max( Vector2Int v ) => Max( v.x, v.y );

/// <inheritdoc cref="Mathfs.Max(Vector2)"/>
[MethodImpl( INLINE )] public static int Max( Vector3Int v ) => Max( v.x, v.y, v.z );

#endregion

#region Signs & Rounding
Expand Down

0 comments on commit 4b557c4

Please sign in to comment.