/// <summary>
        /// Calculates a screen space rectangle based on world space bounds.
        /// </summary>        
        public void GetScissorRectangle(Light light, out BoundingRectangle scissor)
        {
            Matrix transform;
            Matrix.Multiply(ref ViewProjection, ref _ndcToScreen, out transform);

            BoundingRectangle.Transform(ref light.Bounds, ref transform, out scissor);
        }
 public static void SetScissorRectangle(this GraphicsDevice device, ref BoundingRectangle bounds)
 {
     device.ScissorRectangle = new Rectangle(
         (int)bounds.Min.X,
         (int)bounds.Min.Y,
         (int)(bounds.Max.X - bounds.Min.X),
         (int)(bounds.Max.Y - bounds.Min.Y));
 }
        public bool Intersects(ref BoundingRectangle other)
        {
            Vector2 d1, d2;
            Vector2.Subtract(ref other.Min, ref Max, out d1);
            Vector2.Subtract(ref Min, ref other.Max, out d2);

            if (d1.X > 0.0f || d1.Y > 0.0f)
                return false;

            if (d2.X > 0.0f || d2.Y > 0.0f)
                return false;

            return true;
        }
        public static void GetBounds(this Polygon polygon, out BoundingRectangle bounds)
        {
            Vector2 lowerBound = new Vector2(float.MaxValue, float.MaxValue);
            Vector2 upperBound = new Vector2(float.MinValue, float.MinValue);

            int polyCount = polygon.Count;
            for (int i = 0; i < polyCount; i++)
            {
                if (polygon[i].X < lowerBound.X)
                    lowerBound.X = polygon[i].X;
                if (polygon[i].X > upperBound.X)
                    upperBound.X = polygon[i].X;

                if (polygon[i].Y < lowerBound.Y)
                    lowerBound.Y = polygon[i].Y;
                if (polygon[i].Y > upperBound.Y)
                    upperBound.Y = polygon[i].Y;
            }

            bounds = new BoundingRectangle(lowerBound, upperBound);
        }
        public static void Transform(ref BoundingRectangle bounds, ref Matrix transform, out BoundingRectangle result)
        {
            var c1 = new Vector2(bounds.Min.X, bounds.Max.Y);
            var c2 = bounds.Max;
            var c3 = new Vector2(bounds.Max.X, bounds.Min.Y);
            var c4 = bounds.Min;

            Vector2.Transform(ref c1, ref transform, out c1);
            Vector2.Transform(ref c2, ref transform, out c2);
            Vector2.Transform(ref c3, ref transform, out c3);
            Vector2.Transform(ref c4, ref transform, out c4);

            Vector2 min, max;

            Vector2.Min(ref c1, ref c2, out min);
            Vector2.Min(ref min, ref c3, out min);
            Vector2.Min(ref min, ref c4, out min);

            Vector2.Max(ref c1, ref c2, out max);
            Vector2.Max(ref max, ref c3, out max);
            Vector2.Max(ref max, ref c4, out max);

            result = new BoundingRectangle(min, max);
        }
 public bool Intersects(BoundingRectangle other)
 {
     return Intersects(ref other);
 }
        private void CalculateViewProjectionAndBounds()
        {
            // Calculate view projection transform.
            PresentationParameters pp = Engine.Device.PresentationParameters;

            ViewProjection = Matrix.Identity;
            if ((_transforms & Transforms.Custom) != 0)
                ViewProjection *= Custom;
            if ((_transforms & Transforms.SpriteBatch) != 0)
                ViewProjection *= Matrix.CreateOrthographicOffCenter(
                    0,
                    pp.BackBufferWidth,
                    pp.BackBufferHeight,
                    0,
                    0.0f, 1.0f);
            if ((_transforms & Transforms.OriginCenter_XRight_YUp) != 0)
                ViewProjection *= Matrix.CreateOrthographicOffCenter(
                    -pp.BackBufferWidth / 2.0f,
                    pp.BackBufferWidth / 2.0f,
                    -pp.BackBufferHeight / 2.0f,
                    pp.BackBufferHeight / 2.0f,
                    0.0f, 1.0f);
            if ((_transforms & Transforms.OriginBottomLeft_XRight_YUp) != 0)
                ViewProjection *= Matrix.CreateOrthographicOffCenter(
                    0,
                    pp.BackBufferWidth,
                    0,
                    pp.BackBufferHeight,
                    0.0f, 1.0f);
            //LogViewProjection();

            // Calculate inversion of viewprojection.
            Matrix.Invert(ref ViewProjection, out _inverseViewProjection);

            // Determine if we are dealing with an inverted (upside down) Y axis.
            InvertedY = ViewProjection.M22 < 0 && ViewProjection.M11 >= 0 ||
                        ViewProjection.M22 >= 0 && ViewProjection.M11 < 0;

            var size = new Vector2(1.0f);
            var bounds = new BoundingRectangle(-size, size);
            BoundingRectangle.Transform(ref bounds, ref _inverseViewProjection, out Bounds);
        }