/// <summary> /// Gets the bounding box of a rectangle rotated at a specified angle. /// </summary> /// <param name="rectangle">The rectangle to rotate.</param> /// <param name="angleInRadians">The angle of rotation (measured in radians).</param> /// <returns>The bounding box of the rotated rectangle.</returns> /// <remarks>The width and height of the bounding box are calculated using the following formulae: /// w' = w * abs(cos( alpha )) + h * abs(sin( alpha )) /// h' = h * abs(cos( alpha )) + w * abs(sin( alpha )), /// where w and h are the width and height of the rectangle and alpha - the angle of rotation. /// </remarks> /// <seealso cref="http://unlogic.co.uk/posts/bounding-box-of-rotated-image.html"/> public static Rectangle GetBoundingBoxAfterRotation(Rectangle rectangle, double angleInRadians) { double width = (rectangle.Width * Math.Abs(Math.Cos(angleInRadians))) + (rectangle.Height * Math.Abs(Math.Sin(angleInRadians))); double height = (rectangle.Height * Math.Abs(Math.Cos(angleInRadians))) + (rectangle.Width * Math.Abs(Math.Sin(angleInRadians))); return new Rectangle(width, height); }
/// <summary> /// The entry point of the program. /// </summary> private static void Main() { Point point = new Point(1, 0); double theta = Math.PI / 2; Point pointRotated = AffineTransformer.RotatePoint(point, theta); Console.WriteLine(pointRotated); Rectangle rectangle = new Rectangle(20, 10); Rectangle boundingBox = AffineTransformer.GetBoundingBoxAfterRotation(rectangle, theta / 3); Console.WriteLine(boundingBox); }