using Azimuth; using System; using UnitTestSharp; namespace Annulus.UnitTests { public class RectTransformsTests : TestFixture { private static Rect Box(Scalar x, Scalar y, Scalar width, Scalar height) { return Rect.FromPositionAndSize(new Vector(x, y), new Vector(width, height)); } public class RectToRectTests : TestFixture { public void SameRect_IsTheIdentity() { CheckEqual(AffineMatrix.Identity, RectTransforms.RectToRect(Box(3, 4, 10, 20), Box(3, 4, 10, 20))); } public void RectOfTheSameSize_IsATranslation() { AffineMatrix matrix = RectTransforms.RectToRect(Box(0, 0, 10, 20), Box(7, -2, 10, 20)); CheckEqual(AffineMatrix.BuildFromTranslation(new Vector(7, -2)), matrix); } public void Always_CarriesEachCornerToTheMatchingCorner() { Rect source = Box(2, 3, 10, 20); Rect destination = Box(-5, 40, 30, 5); AffineMatrix matrix = RectTransforms.RectToRect(source, destination); CheckEqual(destination.TopLeft, matrix.TransformPoint(source.TopLeft)); CheckEqual(destination.BottomRight, matrix.TransformPoint(source.BottomRight)); } public void Always_StretchesEachAxisOnItsOwn() { AffineMatrix matrix = RectTransforms.RectToRect(Box(0, 0, 10, 20), Box(0, 0, 30, 10)); CheckEqual(AffineMatrix.BuildFromScale(new Vector(3, 0.5)), matrix); } public void NegativeSizes_MeanTheSameRectAsPositiveOnes() { Rect flipped = Rect.FromPositionAndSize(new Vector(12, 23), new Vector(-10, -20)); CheckEqual( RectTransforms.RectToRect(Box(2, 3, 10, 20), Box(5, 6, 30, 40)), RectTransforms.RectToRect(flipped, Box(5, 6, 30, 40))); } public void SourceWithNoWidth_Throws() { CheckThrow(typeof(ArgumentException)); RectTransforms.RectToRect(Box(0, 0, 0, 20), Box(0, 0, 10, 10)); } public void SourceWithNoHeight_Throws() { CheckThrow(typeof(ArgumentException)); RectTransforms.RectToRect(Box(0, 0, 10, 0), Box(0, 0, 10, 10)); } public void DestinationWithNoExtent_CollapsesTheSource() { AffineMatrix matrix = RectTransforms.RectToRect(Box(0, 0, 10, 20), Box(4, 5, 0, 0)); CheckEqual(new Vector(4, 5), matrix.TransformPoint(new Vector(10, 20))); } } public class TransformedBoundsTests : TestFixture { public void Identity_IsTheSameRect() { CheckEqual(Box(1, 2, 3, 4), AffineMatrix.Identity.TransformedBounds(Box(1, 2, 3, 4))); } public void ScaleAndTranslation_IsTheRectMoved() { var matrix = new AffineMatrix(0, new Vector(10, 20), new Vector(2, 3)); CheckEqual(Box(12, 26, 6, 12), matrix.TransformedBounds(Box(1, 2, 3, 4))); } public void Mirrored_StillHasPositiveSize() { AffineMatrix matrix = AffineMatrix.BuildFromScale(new Vector(-1, 1)); CheckEqual(Box(-4, 2, 3, 4), matrix.TransformedBounds(Box(1, 2, 3, 4))); } public void QuarterTurn_SwapsTheExtents() { var matrix = new AffineMatrix(Math.PI / 2, Vector.Zero); Rect bounds = matrix.TransformedBounds(Box(0, 0, 10, 20)); CheckEqual(new Vector(20, 10), bounds.Size); } public void EighthTurn_ContainsEveryCornerOfTheRotatedRect() { var matrix = new AffineMatrix(Math.PI / 4, Vector.Zero); Rect source = Box(0, 0, 100, 20); Rect bounds = matrix.TransformedBounds(source); CheckTrue(bounds.ContainsPoint(matrix.TransformPoint(source.TopLeft))); CheckTrue(bounds.ContainsPoint(matrix.TransformPoint(source.TopRight))); CheckTrue(bounds.ContainsPoint(matrix.TransformPoint(source.BottomLeft))); CheckTrue(bounds.ContainsPoint(matrix.TransformPoint(source.BottomRight))); } public void EighthTurn_ReachesTheExtremeCornersAndNoFarther() { var matrix = new AffineMatrix(Math.PI / 4, Vector.Zero); double diagonal = Math.Sqrt(0.5); Rect bounds = matrix.TransformedBounds(Box(0, 0, 100, 20)); CheckEqual(new Vector(-20 * diagonal, 0), bounds.TopLeft); CheckEqual(new Vector(100 * diagonal, 120 * diagonal), bounds.BottomRight); } } } }