namespace ImageProcessor.Imaging
{
using System;
using System.Drawing;
///
/// Provides rotation calculation methods
///
internal class Rotation
{
///
/// Calculates the new size after rotation.
///
/// The width of the image.
/// The height of the image.
/// The angle of rotation.
/// The new size of the image
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;
}
///
/// 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);
}
}
}