Esempio n. 1
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 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="Matrix2D" />.
 /// </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 Matrix2D transformMatrix, out BoundingRectangle result)
 {
     PrimitivesHelper.TransformRectangle(ref boundingRectangle.Center, ref boundingRectangle.HalfExtents, ref transformMatrix);
     result.Center      = boundingRectangle.Center;
     result.HalfExtents = boundingRectangle.HalfExtents;
 }
        /// <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);
        }
Esempio 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;
        }
Esempio 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);
        }
Esempio n. 7
0
        /// <summary>
        ///     Determines whether this <see cref="Segment2" /> intersects with the specified <see cref="BoundingRectangle" />.
        /// </summary>
        /// <param name="rectangle">The bounding box.</param>
        /// <param name="intersectionPoint">
        ///     When this method returns, contains the <see cref="PointF" /> of intersection, if an
        ///     intersection was found; otherwise, the <see cref="PointF.NaN" />. This parameter is passed uninitialized.
        /// </param>
        /// <returns>
        ///     <c>true</c> if this <see cref="Segment2" /> intersects with <paramref name="rectangle" />; otherwise,
        ///     <c>false</c>.
        /// </returns>
        public bool Intersects(RectangleF rectangle, out PointF intersectionPoint)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 5.3; Basic Primitive Tests - Intersecting Lines, Rays, and (Directed Segments). pg 179-181

            PointF minimumPoint    = rectangle.TopLeft;
            PointF maximumPoint    = rectangle.BottomRight;
            float  minimumDistance = float.MinValue;
            float  maximumDistance = float.MaxValue;

            var direction = End - Start;

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

            if (!PrimitivesHelper.IntersectsSlab(
                    Start.Y, direction.Y, minimumPoint.Y, maximumPoint.Y, ref minimumDistance, ref maximumDistance))
            {
                intersectionPoint = PointF.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);
        }
Esempio n. 8
0
        /// <summary>
        ///     Determines whether this <see cref="Ray2D" /> intersects with a specified <see cref="BoundingRectangle" />.
        /// </summary>
        /// <param name="boundingRectangle">The bounding rectangle.</param>
        /// <param name="rayNearDistance">
        ///     When this method returns, contains the distance along the ray to the first intersection
        ///     point with the <paramref name="boundingRectangle" />, if an intersection was found; otherwise,
        ///     <see cref="float.NaN" />.
        ///     This parameter is passed uninitialized.
        /// </param>
        /// <param name="rayFarDistance">
        ///     When this method returns, contains the distance along the ray to the second intersection
        ///     point with the <paramref name="boundingRectangle" />, if an intersection was found; otherwise,
        ///     <see cref="float.NaN" />.
        ///     This parameter is passed uninitialized.
        /// </param>
        /// <returns>
        ///     <c>true</c> if this <see cref="Ray2D" /> intersects with <paramref name="boundingRectangle" />; otherwise,
        ///     <c>false</c>.
        /// </returns>
        public bool Intersects(BoundingRectangle boundingRectangle, out float rayNearDistance, out float rayFarDistance)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 5.3; Basic Primitive Tests - Intersecting Lines, Rays, and (Directed Segments). pg 179-181

            var minimum = boundingRectangle.Center - boundingRectangle.HalfExtents;
            var maximum = boundingRectangle.Center + boundingRectangle.HalfExtents;

            // Set to the smallest possible value so the algorithm can find the first hit along the ray
            var minimumDistanceAlongRay = float.MinValue;
            // Set to the maximum possible value so the algorithm can find the last hit along the ray
            var maximumDistanceAlongRay = float.MaxValue;

            // For all relevant slabs which in this case is two.

            // The first, horizontal, slab.
            if (!PrimitivesHelper.IntersectsSlab(Position.X, Direction.X, minimum.X, maximum.X,
                                                 ref minimumDistanceAlongRay,
                                                 ref maximumDistanceAlongRay))
            {
                rayNearDistance = rayFarDistance = float.NaN;
                return(false);
            }

            // The second, vertical, slab.
            if (!PrimitivesHelper.IntersectsSlab(Position.Y, Direction.Y, minimum.Y, maximum.Y,
                                                 ref minimumDistanceAlongRay,
                                                 ref maximumDistanceAlongRay))
            {
                rayNearDistance = rayFarDistance = float.NaN;
                return(false);
            }

            // Ray intersects the 2 slabs.
            rayNearDistance = minimumDistanceAlongRay < 0 ? 0 : minimumDistanceAlongRay;
            rayFarDistance  = maximumDistanceAlongRay;
            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));
 }
Esempio n. 10
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));
 }
Esempio n. 11
0
 /// <summary>
 ///     Computes the closest <see cref="PointF" /> on this <see cref="RectangleF" /> to a specified
 ///     <see cref="PointF" />.
 /// </summary>
 /// <param name="rect">The rectangle.</param>
 /// <param name="point">The point.</param>
 /// <returns>The closest <see cref="PointF" /> on this <see cref="RectangleF" /> to the <paramref name="point" />.</returns>
 public static PointF ClosestPointTo(this RectangleF rect, PointF point)
 {
     PrimitivesHelper.ClosestPointToPointFromRectangle(rect.TopLeft, rect.BottomRight, point, out PointF result);
     return(result);
 }
Esempio n. 12
0
 /// <summary>
 ///     Computes the <see cref="RectangleF" /> from a list of <see cref="PointF" /> structures.
 /// </summary>
 /// <param name="points">The points.</param>
 /// <param name="result">The resulting rectangle.</param>
 public static void CreateFrom(IReadOnlyList <PointF> points, out RectangleF result)
 {
     PrimitivesHelper.CreateRectangleFromPoints(points, out PointF minimum, out PointF maximum);
     RectangleF.CreateFrom(minimum, maximum, out result);
 }