mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 8f97ef27a417cd76b6e4f12eaeb2411d2c4f261e Former-commit-id: ddddf7081deb89d2367964ac48227f60c05ce617pull/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