/// <summary>
        ///     Computes the <see cref="BoundingRectangle" /> that contains the two specified
        ///     <see cref="BoundingRectangle" /> structures.
        /// </summary>
        /// <param name="first">The first bounding rectangle.</param>
        /// <param name="second">The second bounding rectangle.</param>
        /// <param name="result">The resulting bounding rectangle that contains both the <paramref name="first" /> and the
        ///     <paramref name="second" />.</param>
        public static void Union(ref BoundingRectangle first, ref BoundingRectangle second, out BoundingRectangle result)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 6.5; Bounding Volume Hierarchies - Merging Bounding Volumes. pg 267

            var firstMinimum  = first.Center - first.HalfExtents;
            var firstMaximum  = first.Center + first.HalfExtents;
            var secondMinimum = second.Center - second.HalfExtents;
            var secondMaximum = second.Center + second.HalfExtents;

            var minimum = Point2.Minimum(firstMinimum, secondMinimum);
            var maximum = Point2.Maximum(firstMaximum, secondMaximum);

            result = CreateFrom(minimum, maximum);
        }
        /// <summary>
        ///     Computes the <see cref="BoundingRectangle" /> that is in common between the two specified
        ///     <see cref="BoundingRectangle" /> structures.
        /// </summary>
        /// <param name="first">The first bounding rectangle.</param>
        /// <param name="second">The second bounding rectangle.</param>
        /// <param name="result">The resulting bounding rectangle that is in common between both the <paramref name="first" /> and
        ///     the <paramref name="second" />, if they intersect; otherwise, <see cref="Empty"/>.</param>
        public static void Intersection(ref BoundingRectangle first,
                                        ref BoundingRectangle second, out BoundingRectangle result)
        {
            var firstMinimum  = first.Center - first.HalfExtents;
            var firstMaximum  = first.Center + first.HalfExtents;
            var secondMinimum = second.Center - second.HalfExtents;
            var secondMaximum = second.Center + second.HalfExtents;

            var minimum = Point2.Maximum(firstMinimum, secondMinimum);
            var maximum = Point2.Minimum(firstMaximum, secondMaximum);

            if ((maximum.X < minimum.X) || (maximum.Y < minimum.Y))
            {
                result = new BoundingRectangle();
            }
            else
            {
                result = CreateFrom(minimum, maximum);
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Computes the <see cref="RectangleF" /> that is in common between the two specified
        ///     <see cref="RectangleF" /> structures.
        /// </summary>
        /// <param name="first">The first rectangle.</param>
        /// <param name="second">The second rectangle.</param>
        /// <param name="result">The resulting rectangle that is in common between both the <paramref name="first" /> and
        ///     the <paramref name="second" />, if they intersect; otherwise, <see cref="Empty"/>.</param>
        public static void Intersection(ref RectangleF first,
                                        ref RectangleF second, out RectangleF result)
        {
            var firstMinimum  = first.TopLeft;
            var firstMaximum  = first.BottomRight;
            var secondMinimum = second.TopLeft;
            var secondMaximum = second.BottomRight;

            var minimum = Point2.Maximum(firstMinimum, secondMinimum);
            var maximum = Point2.Minimum(firstMaximum, secondMaximum);

            if ((maximum.X < minimum.X) || (maximum.Y < minimum.Y))
            {
                result = new RectangleF();
            }
            else
            {
                result = CreateFrom(minimum, maximum);
            }
        }
        internal static void CreateRectangleFromPoints(IReadOnlyList <Point2> points, out Point2 minimum, out Point2 maximum)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 4.2; Bounding Volumes - Axis-aligned Bounding Boxes (AABBs). pg 82-84

            if (points == null || points.Count == 0)
            {
                minimum = Point2.Zero;
                maximum = Point2.Zero;
                return;
            }

            minimum = maximum = points[0];

            // ReSharper disable once ForCanBeConvertedToForeach
            for (var index = points.Count - 1; index > 0; --index)
            {
                var point = points[index];
                minimum = Point2.Minimum(minimum, point);
                maximum = Point2.Maximum(maximum, point);
            }
        }