Ejemplo n.º 1
0
 /// <summary>
 ///     Computes the <see cref="BoundingRectangle" /> from a minimum <see cref="Point2" /> and maximum
 ///     <see cref="Point2" />.
 /// </summary>
 /// <param name="minimum">The minimum point.</param>
 /// <param name="maximum">The maximum point.</param>
 /// <param name="result">The resulting bounding rectangle.</param>
 public static void CreateFrom(Point2 minimum, Point2 maximum, out BoundingRectangle result)
 {
     result.Center      = new Point2((maximum.X + minimum.X) * 0.5f, (maximum.Y + minimum.Y) * 0.5f);
     result.HalfExtents = new Vector2((maximum.X - minimum.X) * 0.5f, (maximum.Y - minimum.Y) * 0.5f);
 }
Ejemplo n.º 2
0
 /// <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));
 }
Ejemplo n.º 3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="BoundingRectangle" /> structure from the specified centre
 ///     <see cref="Point2" /> and the radii <see cref="Size2" />.
 /// </summary>
 /// <param name="center">The centre <see cref="Point2" />.</param>
 /// <param name="halfExtents">The radii <see cref="Vector2" />.</param>
 public BoundingRectangle(Point2 center, Size2 halfExtents)
 {
     Center      = center;
     HalfExtents = halfExtents;
 }
Ejemplo n.º 4
0
 /// <summary>
 ///     Determines whether the specified <see cref="BoundingRectangle" /> contains the specified
 ///     <see cref="Point2" />.
 /// </summary>
 /// <param name="boundingRectangle">The bounding rectangle.</param>
 /// <param name="point">The point.</param>
 /// <returns>
 ///     <c>true</c> if the <paramref name="boundingRectangle" /> contains the <paramref name="point" />; otherwise,
 ///     <c>false</c>.
 /// </returns>
 public static bool Contains(BoundingRectangle boundingRectangle, Point2 point)
 {
     return(Contains(ref boundingRectangle, ref point));
 }
Ejemplo n.º 5
0
 /// <summary>
 ///     Determines whether this <see cref="BoundingRectangle" /> contains the specified <see cref="Point2" />.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>
 ///     <c>true</c> if this <see cref="BoundingRectangle" /> contains the <paramref name="point" />; otherwise,
 ///     <c>false</c>.
 /// </returns>
 public bool Contains(Point2 point)
 {
     return(Contains(this, point));
 }
Ejemplo n.º 6
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));
 }
Ejemplo n.º 7
0
 /// <summary>
 ///     Computes the distance from this <see cref="RectangleF"/> to a <see cref="Point2"/>.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>The distance from this <see cref="RectangleF"/> to the <paramref name="point"/>.</returns>
 public float DistanceTo(Point2 point)
 {
     return((float)Math.Sqrt(SquaredDistanceTo(point)));
 }
Ejemplo n.º 8
0
 /// <summary>
 ///     Determines whether the specified <see cref="RectangleF" /> contains the specified
 ///     <see cref="Point2" />.
 /// </summary>
 /// <param name="rectangle">The rectangle.</param>
 /// <param name="point">The point.</param>
 /// <returns>
 ///     <c>true</c> if the <paramref name="rectangle" /> contains the <paramref name="point" />; otherwise,
 ///     <c>false</c>.
 /// </returns>
 public static bool Contains(RectangleF rectangle, Point2 point)
 {
     return(Contains(ref rectangle, ref point));
 }
Ejemplo n.º 9
0
 /// <summary>
 ///     Determines whether this <see cref="RectangleF" /> contains the specified
 ///     <see cref="Point2" />.
 /// </summary>
 /// <param name="point">The point.</param>
 /// <returns>
 ///     <c>true</c> if the this <see cref="RectangleF"/> contains the <paramref name="point" />; otherwise,
 ///     <c>false</c>.
 /// </returns>
 public bool Contains(Point2 point)
 {
     return(Contains(ref this, ref point));
 }
Ejemplo n.º 10
0
 /// <summary>
 ///     Determines whether the specified <see cref="RectangleF" /> contains the specified
 ///     <see cref="Point2" />.
 /// </summary>
 /// <param name="rectangle">The rectangle.</param>
 /// <param name="point">The point.</param>
 /// <returns>
 ///     <c>true</c> if the <paramref name="rectangle" /> contains the <paramref name="point" />; otherwise,
 ///     <c>false</c>.
 /// </returns>
 public static bool Contains(ref RectangleF rectangle, ref Point2 point)
 {
     return(rectangle.X <= point.X && point.X < rectangle.X + rectangle.Width && rectangle.Y <= point.Y && point.Y < rectangle.Y + rectangle.Height);
 }
Ejemplo n.º 11
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Ray2" /> structure from the specified position and direction.
 /// </summary>
 /// <param name="position">The starting point.</param>
 /// <param name="direction">The direction vector.</param>
 public Ray2(Point2 position, Vector2 direction)
 {
     Position  = position;
     Direction = direction;
 }
Ejemplo n.º 12
0
        internal static float SquaredDistanceToPointFromRectangle(Point2 minimum, Point2 maximum, Point2 point)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 5.1.3.1; Basic Primitive Tests - Closest-point Computations - Distance of Point to AABB.  pg 130-131
            var squaredDistance = 0.0f;

            // for each axis add up the excess distance outside the box

            // x-axis
            if (point.X < minimum.X)
            {
                var distance = minimum.X - point.X;
                squaredDistance += distance * distance;
            }
            else if (point.X > maximum.X)
            {
                var distance = maximum.X - point.X;
                squaredDistance += distance * distance;
            }

            // y-axis
            if (point.Y < minimum.Y)
            {
                var distance = minimum.Y - point.Y;
                squaredDistance += distance * distance;
            }
            else if (point.Y > maximum.Y)
            {
                var distance = maximum.Y - point.Y;
                squaredDistance += distance * distance;
            }
            return(squaredDistance);
        }
Ejemplo n.º 13
0
        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);
            }
        }
Ejemplo n.º 14
0
        internal static void ClosestPointToPointFromRectangle(Point2 minimum, Point2 maximum, Point2 point, out Point2 result)
        {
            // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 5.1.2; Basic Primitive Tests - Closest-point Computations. pg 130-131

            result = point;

            // For each coordinate axis, if the point coordinate value is outside box, clamp it to the box, else keep it as is
            if (result.X < minimum.X)
            {
                result.X = minimum.X;
            }
            else if (result.X > maximum.X)
            {
                result.X = maximum.X;
            }

            if (result.Y < minimum.Y)
            {
                result.Y = minimum.Y;
            }
            else if (result.Y > maximum.Y)
            {
                result.Y = maximum.Y;
            }
        }