mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 1ae4da1184336db1522c1a35f68235c0a28b0413 Former-commit-id: e8a76a404d1d986a5e9e9469c274ce705879b964pull/17/head
9 changed files with 190 additions and 23 deletions
@ -0,0 +1,22 @@ |
|||
namespace ImageProcessor.UnitTests.Imaging |
|||
{ |
|||
using System.Drawing; |
|||
using FluentAssertions; |
|||
using ImageProcessor.Imaging; |
|||
using NUnit.Framework; |
|||
|
|||
public class RotationUnitTests |
|||
{ |
|||
[Test] |
|||
[TestCase(100, 100, 45, 141, 141)] |
|||
[TestCase(100, 100, 30, 137, 137)] |
|||
[TestCase(100, 200, 50, 217, 205)] |
|||
public void NewSizeAfterRotationIsCalculated(int width, int height, float angle, int expectedWidth, int expectedHeight) |
|||
{ |
|||
Size result = Rotation.NewSizeAfterRotation(width, height, angle); |
|||
|
|||
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"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
namespace ImageProcessor.UnitTests.Processors |
|||
{ |
|||
using System.Collections.Generic; |
|||
using ImageProcessor.Processors; |
|||
using NUnit.Framework; |
|||
|
|||
public class RotateInsideUnitTests |
|||
{ |
|||
[Test] |
|||
[TestCase(100, 100, 15, 150)] |
|||
public void ZoomIsCalculated(int width, int height, float angle, float expected) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
namespace ImageProcessor.Imaging |
|||
{ |
|||
using System; |
|||
using System.Drawing; |
|||
|
|||
/// <summary>
|
|||
/// Provides rotation calculation methods
|
|||
/// </summary>
|
|||
internal class Rotation |
|||
{ |
|||
/// <summary>
|
|||
/// Calculates the new size after rotation.
|
|||
/// </summary>
|
|||
/// <param name="width">The width of the image.</param>
|
|||
/// <param name="height">The height of the image.</param>
|
|||
/// <param name="angle">The angle of rotation.</param>
|
|||
/// <returns>The new size of the image</returns>
|
|||
public static Size NewSizeAfterRotation(int width, int height, float angle) |
|||
{ |
|||
double widthAsDouble = width; |
|||
double heightAsDouble = height; |
|||
|
|||
double radians = angle * Math.PI / 180d; |
|||
double radiansSin = Math.Sin(radians); |
|||
double radiansCos = Math.Cos(radians); |
|||
double width1 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos); |
|||
double height1 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos); |
|||
|
|||
// Find dimensions in the other direction
|
|||
radiansSin = Math.Sin(-radians); |
|||
radiansCos = Math.Cos(-radians); |
|||
double width2 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos); |
|||
double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos); |
|||
|
|||
// Get the external vertex for the rotation
|
|||
Size result = new Size(); |
|||
result.Width = Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2))); |
|||
result.Height = Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2))); |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue