Skip to content

Commit

Permalink
Merge pull request #20 from irihitech/math
Browse files Browse the repository at this point in the history
Add MathHelper.
  • Loading branch information
rabbitism authored May 22, 2024
2 parents 3e3cf55 + 53c52b6 commit 40ed1ed
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindingExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LogicalHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\MathHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ObservableExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RoutedEventExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Reactive\ReadonlyDisposableCollection.cs" />
Expand Down
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));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindingExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LogicalHelpersTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\MathHelperTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ObservableExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\PropertyPseudoClassHelperTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\RoutedEventExtensionTest.cs" />
Expand Down

0 comments on commit 40ed1ed

Please sign in to comment.