-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from irihitech/math
Add MathHelper.
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Avalonia.Controls.Platform; | ||
using Avalonia.Utilities; | ||
|
||
namespace Irihi.Avalonia.Shared.Helpers; | ||
|
||
public static class MathHelpers | ||
{ | ||
private static (double min, double max) GetMinMax(double a, double b) | ||
{ | ||
return a >= b ? (b, a) : (a, b); | ||
} | ||
|
||
private static (float min, float max) GetMinMax(float a, float b) | ||
{ | ||
return a >= b ? (b, a) : (a, b); | ||
} | ||
|
||
private static (decimal min, decimal max) GetMinMax(decimal a, decimal b) | ||
{ | ||
return a >= b ? (b, a) : (a, b); | ||
} | ||
|
||
private static (int min, int max) GetMinMax(int a, int b) | ||
{ | ||
return a >= b ? (b, a) : (a, b); | ||
} | ||
|
||
public static double SafeClamp(double value, double min, double max) | ||
{ | ||
(min, max) = GetMinMax(min, max); | ||
if (value < min) return min; | ||
return value > max ? max : value; | ||
} | ||
|
||
public static decimal SafeClamp(decimal value, decimal min, decimal max) | ||
{ | ||
(min, max) = GetMinMax(min, max); | ||
if (value < min) return min; | ||
return value > max ? max : value; | ||
} | ||
|
||
public static int SafeClamp(int value, int min, int max) | ||
{ | ||
(min, max) = GetMinMax(min, max); | ||
if (value < min) return min; | ||
return value > max ? max : value; | ||
} | ||
|
||
public static float SafeClamp(float value, float min, float max) | ||
{ | ||
(min, max) = GetMinMax(min, max); | ||
if (value < min) return min; | ||
return value > max ? max : value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/MathHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Irihi.Avalonia.Shared.Helpers; | ||
|
||
namespace Irihi.Avalonia.Shared.UnitTest.Helpers; | ||
|
||
public class MathHelperTests | ||
{ | ||
[Theory] | ||
[InlineData(0, 1, 2, 1)] | ||
[InlineData(1, 1, 2, 1)] | ||
[InlineData(2, 1, 2, 2)] | ||
[InlineData(3, 1, 2, 2)] | ||
[InlineData(0, 2, 1, 1)] | ||
[InlineData(1, 2, 1, 1)] | ||
[InlineData(2, 2, 1, 2)] | ||
[InlineData(3, 2, 1, 2)] | ||
[InlineData(3, 1, 1, 1)] | ||
public void SafeClamp_Decimal_Success(decimal value, decimal min, decimal max, decimal expected) | ||
{ | ||
Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, 1, 2, 1)] | ||
[InlineData(1, 1, 2, 1)] | ||
[InlineData(2, 1, 2, 2)] | ||
[InlineData(3, 1, 2, 2)] | ||
[InlineData(0, 2, 1, 1)] | ||
[InlineData(1, 2, 1, 1)] | ||
[InlineData(2, 2, 1, 2)] | ||
[InlineData(3, 2, 1, 2)] | ||
[InlineData(3, 1, 1, 1)] | ||
public void SafeClamp_Double_Success(double value, double min, double max, double expected) | ||
{ | ||
Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, 1, 2, 1)] | ||
[InlineData(1, 1, 2, 1)] | ||
[InlineData(2, 1, 2, 2)] | ||
[InlineData(3, 1, 2, 2)] | ||
[InlineData(0, 2, 1, 1)] | ||
[InlineData(1, 2, 1, 1)] | ||
[InlineData(2, 2, 1, 2)] | ||
[InlineData(3, 2, 1, 2)] | ||
[InlineData(3, 1, 1, 1)] | ||
public void SafeClamp_Float_Success(float value, float min, float max, float expected) | ||
{ | ||
Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, 1, 2, 1)] | ||
[InlineData(1, 1, 2, 1)] | ||
[InlineData(2, 1, 2, 2)] | ||
[InlineData(3, 1, 2, 2)] | ||
[InlineData(0, 2, 1, 1)] | ||
[InlineData(1, 2, 1, 1)] | ||
[InlineData(2, 2, 1, 2)] | ||
[InlineData(3, 2, 1, 2)] | ||
[InlineData(3, 1, 1, 1)] | ||
public void SafeClamp_Int_Success(int value, int min, int max, int expected) | ||
{ | ||
Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters