From 6217257444c3eed2d4b1e91606882e333b6dc4f8 Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Wed, 25 Feb 2015 10:04:03 +0100 Subject: [PATCH] Changes the method to return a factor instead of a percentage Former-commit-id: 75fa0d3daf53263ab1b411b910d1b90580e7ec7d Former-commit-id: c19f6cbbf92b6fca98a0f05eec8ccc4d31f9934b --- .../Imaging/RotationUnitTests.cs | 11 +++++++++++ src/ImageProcessor/Imaging/Rotation.cs | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/ImageProcessor.UnitTests/Imaging/RotationUnitTests.cs b/src/ImageProcessor.UnitTests/Imaging/RotationUnitTests.cs index 32eb72e3ea..6e71079c62 100644 --- a/src/ImageProcessor.UnitTests/Imaging/RotationUnitTests.cs +++ b/src/ImageProcessor.UnitTests/Imaging/RotationUnitTests.cs @@ -18,5 +18,16 @@ result.Width.Should().Be(expectedWidth, "because the rotated width should have been calculated"); result.Height.Should().Be(expectedHeight, "because the rotated height should have been calculated"); } + + [Test] + [TestCase(100, 100, 45, 1.41f)] + [TestCase(100, 100, 15, 1.22f)] + [TestCase(100, 200, 45, 2.12f)] + public void RotationZoomIsCalculated(int imageWidth, int imageHeight, float angle, float expected) + { + float result = Rotation.ZoomAfterRotation(imageWidth, imageHeight, angle); + + result.Should().BeApproximately(expected, 0.01f, "because the zoom level after rotation should have been calculated"); + } } } \ No newline at end of file diff --git a/src/ImageProcessor/Imaging/Rotation.cs b/src/ImageProcessor/Imaging/Rotation.cs index 8ae02f719d..b2ecd30cc8 100644 --- a/src/ImageProcessor/Imaging/Rotation.cs +++ b/src/ImageProcessor/Imaging/Rotation.cs @@ -39,5 +39,24 @@ return result; } + + /// + /// Calculates the zoom needed after the rotation. + /// + /// Width of the image. + /// Height of the image. + /// The angle. + /// The zoom needed + public static float ZoomAfterRotation(int imageWidth, int imageHeight, float angle) + { + double radians = angle * Math.PI / 180d; + double radiansSin = Math.Sin(radians); + double radiansCos = Math.Cos(radians); + + double widthRotated = (imageWidth * radiansCos) + (imageHeight * radiansSin); + double heightRotated = (imageWidth * radiansSin) + (imageHeight * radiansCos); + + return (float)(Math.Max(widthRotated, heightRotated) / 100); + } } } \ No newline at end of file