diff --git a/SpectrumColorShift.Tests/ShiftGeometryTests.cs b/SpectrumColorShift.Tests/ShiftGeometryTests.cs index c17c1ca..c60ea8f 100644 --- a/SpectrumColorShift.Tests/ShiftGeometryTests.cs +++ b/SpectrumColorShift.Tests/ShiftGeometryTests.cs @@ -67,6 +67,8 @@ public sealed class ShiftGeometryTests [InlineData(10f, 0f, 1f, -2, -12, 42, 36)] [InlineData(10f, 0.5f, 0.25f, -7, -5, 47, 29)] [InlineData(0.25f, 1f, 0f, -3, -2, 43, 26)] + [InlineData(10.0005f, 1f, 0f, -12, -2, 52, 26)] + [InlineData(10.002f, 1f, 0f, -13, -2, 53, 26)] public void ALinearShiftGrowsTheBoundsByTheRoundedUpComponentsPlusTwoPixels(float distance, float x, float y, int left, int top, int right, int bottom) { var rect = new RawRect(0, 0, 40, 24); @@ -78,6 +80,19 @@ public sealed class ShiftGeometryTests Assert.Equal(output, input); } + [Theory] + [InlineData(90f, -2, -12, 42, 36)] + [InlineData(-90f, -2, -12, 42, 36)] + [InlineData(180f, -12, -2, 52, 26)] + [InlineData(270f, -2, -12, 42, 36)] + [InlineData(0f, -12, -2, 52, 26)] + public void AnAxisAlignedAngleAddsNoPixelFromRoundingNoise(float degrees, int left, int top, int right, int bottom) + { + var output = ShiftGeometry.Output(new RawRect(0, 0, 40, 24), ShiftMode.Linear, 10f, Unit(degrees), Vector2.Zero, 1f); + + Assert.Equal(new RawRect(left, top, right, bottom), output); + } + [Theory] [InlineData(25f, -69, -69, 69, 69)] [InlineData(-25f, -69, -69, 69, 69)] diff --git a/SpectrumColorShift/ShiftGeometry.cs b/SpectrumColorShift/ShiftGeometry.cs index cccca1a..09011d2 100644 --- a/SpectrumColorShift/ShiftGeometry.cs +++ b/SpectrumColorShift/ShiftGeometry.cs @@ -8,6 +8,8 @@ namespace SpectrumColorShift public const float MaxRadialShift = 0.5f; public const int Guard = 2; + private const double RoundingTolerance = 1e-3; + public static Vector2 Center(RawRect rect, Vector2 offset) => new((rect.Left + rect.Right) * 0.5f + offset.X, (rect.Top + rect.Bottom) * 0.5f + offset.Y); @@ -106,7 +108,7 @@ namespace SpectrumColorShift return MathF.Sqrt(dx * dx + dy * dy); } - private static int Margin(float distance) => Saturate((long)Math.Ceiling(distance) + Guard); + private static int Margin(float distance) => Saturate((long)Math.Ceiling(distance - RoundingTolerance) + Guard); private static RawRect Inflate(RawRect rect, int marginX, int marginY) => new(