Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }