AForge.Imaging.Filters.BaseRotateFilter.CalculateNewImageSize C# (CSharp) Method

CalculateNewImageSize() protected method

Calculates new image size.
protected CalculateNewImageSize ( UnmanagedImage sourceData ) : Size
sourceData UnmanagedImage Source image data.
return System.Drawing.Size
        protected override System.Drawing.Size CalculateNewImageSize( UnmanagedImage sourceData )
        {
            // return same size if original image size should be kept
            if ( keepSize )
                return new Size( sourceData.Width, sourceData.Height );

            // angle's sine and cosine
            double angleRad = -angle * Math.PI / 180;
            double angleCos = Math.Cos( angleRad );
            double angleSin = Math.Sin( angleRad );

            // calculate half size
            double halfWidth  = (double) sourceData.Width / 2;
            double halfHeight = (double) sourceData.Height / 2;

            // rotate corners
            double cx1 = halfWidth * angleCos;
            double cy1 = halfWidth * angleSin;

            double cx2 = halfWidth * angleCos - halfHeight * angleSin;
            double cy2 = halfWidth * angleSin + halfHeight * angleCos;

            double cx3 = -halfHeight * angleSin;
            double cy3 =  halfHeight * angleCos;

            double cx4 = 0;
            double cy4 = 0;

            // recalculate image size
            halfWidth  = Math.Max( Math.Max( cx1, cx2 ), Math.Max( cx3, cx4 ) ) - Math.Min( Math.Min( cx1, cx2 ), Math.Min( cx3, cx4 ) );
            halfHeight = Math.Max( Math.Max( cy1, cy2 ), Math.Max( cy3, cy4 ) ) - Math.Min( Math.Min( cy1, cy2 ), Math.Min( cy3, cy4 ) );

            return new Size( (int) ( halfWidth * 2 + 0.5 ), (int) ( halfHeight * 2 + 0.5 ) );
        }
    }