/// <summary>
        ///     Computes the closest <see cref="Point2" /> on this <see cref="BoundingRectangle" /> to a specified
        ///     <see cref="Point2" />.
        /// </summary>
        /// <param name="point">The point.</param>
        /// <returns>The closest <see cref="Point2" /> on this <see cref="BoundingRectangle" /> to the <paramref name="point" />.</returns>
        public Point2 ClosestPointTo(Point2 point)
        {
            Point2 result;

            PrimitivesHelper.ClosestPointToPointFromRectangle(Center - HalfExtents, Center + HalfExtents, point, out result);
            return(result);
        }
 /// <summary>
 ///     Computes the <see cref="BoundingRectangle" /> from the specified <see cref="BoundingRectangle" /> transformed by
 ///     the
 ///     specified <see cref="Matrix2" />.
 /// </summary>
 /// <param name="boundingRectangle">The bounding rectangle.</param>
 /// <param name="transformMatrix">The transform matrix.</param>
 /// <param name="result">The resulting bounding rectangle.</param>
 /// <returns>
 ///     The <see cref="BoundingRectangle" /> from the <paramref name="boundingRectangle" /> transformed by the
 ///     <paramref name="transformMatrix" />.
 /// </returns>
 /// <remarks>
 ///     <para>
 ///         If a transformed <see cref="BoundingRectangle" /> is used for <paramref name="boundingRectangle" /> then the
 ///         resulting <see cref="BoundingRectangle" /> will have the compounded transformation, which most likely is
 ///         not desired.
 ///     </para>
 /// </remarks>
 public static void Transform(ref BoundingRectangle boundingRectangle,
                              ref Matrix2 transformMatrix, out BoundingRectangle result)
 {
     PrimitivesHelper.TransformRectangle(ref boundingRectangle.Center, ref boundingRectangle.HalfExtents, ref transformMatrix);
     result.Center      = boundingRectangle.Center;
     result.HalfExtents = boundingRectangle.HalfExtents;
 }
Exemplo n.º 3
0
        /// <summary>
        ///     Computes the closest <see cref="Point2" /> on this <see cref="RectangleF" /> to a specified
        ///     <see cref="Point2" />.
        /// </summary>
        /// <param name="point">The point.</param>
        /// <returns>The closest <see cref="Point2" /> on this <see cref="RectangleF" /> to the <paramref name="point" />.</returns>
        public Point2 ClosestPointTo(Point2 point)
        {
            Point2 result;

            PrimitivesHelper.ClosestPointToPointFromRectangle(TopLeft, BottomRight, point, out result);
            return(result);
        }
        /// <summary>
        ///     Computes the <see cref="BoundingRectangle" /> from a list of <see cref="Point2" /> structures.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="result">The resulting bounding rectangle.</param>
        public static void CreateFrom(IReadOnlyList <Point2> points, out BoundingRectangle result)
        {
            Point2 minimum;
            Point2 maximum;

            PrimitivesHelper.CreateRectangleFromPoints(points, out minimum, out maximum);
            CreateFrom(minimum, maximum, out result);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Computes the <see cref="RectangleF" /> from the specified <see cref="RectangleF" /> transformed by
        ///     the specified <see cref="Matrix2" />.
        /// </summary>
        /// <param name="rectangle">The rectangle to be transformed.</param>
        /// <param name="transformMatrix">The transform matrix.</param>
        /// <param name="result">The resulting transformed rectangle.</param>
        /// <returns>
        ///     The <see cref="BoundingRectangle" /> from the <paramref name="rectangle" /> transformed by the
        ///     <paramref name="transformMatrix" />.
        /// </returns>
        /// <remarks>
        ///     <para>
        ///         If a transformed <see cref="BoundingRectangle" /> is used for <paramref name="rectangle" /> then the
        ///         resulting <see cref="BoundingRectangle" /> will have the compounded transformation, which most likely is
        ///         not desired.
        ///     </para>
        /// </remarks>
        public static void Transform(ref RectangleF rectangle,
                                     ref Matrix2 transformMatrix, out RectangleF result)
        {
            var center      = rectangle.Center;
            var halfExtents = (Vector2)rectangle.Size * 0.5f;

            PrimitivesHelper.TransformRectangle(ref center, ref halfExtents, ref transformMatrix);

            result.X      = center.X - halfExtents.X;
            result.Y      = center.Y - halfExtents.Y;
            result.Width  = halfExtents.X * 2;
            result.Height = halfExtents.Y * 2;
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Determines whether this <see cref="Segment2" /> intersects with the specified <see cref="BoundingRectangle" />.
        /// </summary>
        /// <param name="boundingRectangle">The bounding box.</param>
        /// <param name="intersectionPoint">
        ///     When this method returns, contains the <see cref="Point2" /> of intersection, if an
        ///     intersection was found; otherwise, the <see cref="Point2.NaN" />. This parameter is passed uninitialized.
        /// </param>
        /// <returns>
        ///     <c>true</c> if this <see cref="Segment2" /> intersects with <paramref name="boundingRectangle" />; otherwise,
        ///     <c>false</c>.
        /// </returns>
        public bool Intersects(BoundingRectangle boundingRectangle, out Point2 intersectionPoint)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 5.3; Basic Primitive Tests - Intersecting Lines, Rays, and (Directed Segments). pg 179-181

            var minimumPoint    = boundingRectangle.Center - boundingRectangle.HalfExtents;
            var maximumPoint    = boundingRectangle.Center + boundingRectangle.HalfExtents;
            var minimumDistance = float.MinValue;
            var maximumDistance = float.MaxValue;

            var direction = End - Start;

            if (
                !PrimitivesHelper.IntersectsSlab(Start.X, direction.X, minimumPoint.X, maximumPoint.X, ref minimumDistance,
                                                 ref maximumDistance))
            {
                intersectionPoint = Point2.NaN;
                return(false);
            }

            if (
                !PrimitivesHelper.IntersectsSlab(Start.Y, direction.Y, minimumPoint.Y, maximumPoint.Y, ref minimumDistance,
                                                 ref maximumDistance))
            {
                intersectionPoint = Point2.NaN;
                return(false);
            }

            // Segment intersects the 2 slabs.

            if (minimumDistance <= 0)
            {
                intersectionPoint = Start;
            }
            else
            {
                intersectionPoint    = minimumDistance * direction;
                intersectionPoint.X += Start.X;
                intersectionPoint.Y += Start.Y;
            }

            return(true);
        }
 /// <summary>
 ///     Computes the squared distance from this <see cref="BoundingRectangle"/> to a <see cref="Point2"/>.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>The squared distance from this <see cref="BoundingRectangle"/> to the <paramref name="point"/>.</returns>
 public float SquaredDistanceTo(Point2 point)
 {
     return(PrimitivesHelper.SquaredDistanceToPointFromRectangle(Center - HalfExtents, Center + HalfExtents, point));
 }
Exemplo n.º 8
0
 /// <summary>
 ///     Computes the squared distance from this <see cref="RectangleF"/> to a <see cref="Point2"/>.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>The squared distance from this <see cref="RectangleF"/> to the <paramref name="point"/>.</returns>
 public float SquaredDistanceTo(Point2 point)
 {
     return(PrimitivesHelper.SquaredDistanceToPointFromRectangle(TopLeft, BottomRight, point));
 }