예제 #1
0
        /// <summary>
        /// Calculates an axis aligned rectangle which fully contains an arbitrarily transformed axis aligned rectangle.
        /// </summary>
        /// <param name="rectangle">Original bounding rectangle.</param>
        /// <param name="transform">World transform of the rectangle.</param>
        /// <returns>A new rectangle which contains the trasnformed rectangle.</returns>
        public static void CalculateBoundingRectangle(ref RectangleF rectangle, ref Matrix transform, out RectangleF boundingRect)
        {
            // Get all four corners in local space
            Vector2 leftTop;

            leftTop.X = rectangle.Left;
            leftTop.Y = rectangle.Top;

            Vector2 rightTop;

            rightTop.X = rectangle.Right;
            rightTop.Y = rectangle.Top;

            Vector2 leftBottom;

            leftBottom.X = rectangle.Left;
            leftBottom.Y = rectangle.Bottom;

            Vector2 rightBottom;

            rightBottom.X = rectangle.Right;
            rightBottom.Y = rectangle.Bottom;

            // Transform all four corners into work space
            Vector2Helper.Transform(ref leftTop, ref transform, out leftTop);
            Vector2Helper.Transform(ref rightTop, ref transform, out rightTop);
            Vector2Helper.Transform(ref leftBottom, ref transform, out leftBottom);
            Vector2Helper.Transform(ref rightBottom, ref transform, out rightBottom);

            // Find the minimum and maximum extents of the rectangle in world space
            Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), Vector2.Min(leftBottom, rightBottom));
            Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), Vector2.Max(leftBottom, rightBottom));

            // Return that as a rectangle
            boundingRect        = new RectangleF();
            boundingRect.X      = min.X;
            boundingRect.Y      = min.Y;
            boundingRect.Width  = (max.X - min.X);
            boundingRect.Height = (max.Y - min.Y);
        }