コード例 #1
0
        /// <summary> Finds point the edge of the rectangle nearest to the specified point. </summary>
        /// <param name="target">The target point.</param>
        /// <param name="closestEdgePoint">Point the edge of the rectangle nearest to the specified point.</param>
        public void ClosestEdgePoint(ref Vector2 target, out Vector2 closestEdgePoint)
        {
            float x, y;

            NumberTools.Clamp(ref target.X, ref Min.X, ref Max.X, out x);
            NumberTools.Clamp(ref target.Y, ref Min.Y, ref Max.Y, out y);

            float dl = Math.Abs(x - Min.X);
            float dr = Math.Abs(x - Max.X);
            float dt = Math.Abs(y - Min.Y);
            float db = Math.Abs(y - Max.Y);
            float m  = Math.Min(Math.Min(Math.Min(dl, dr), dt), db);

            if (m.NearlyEquals(dt))
            {
                closestEdgePoint.X = x;
                closestEdgePoint.Y = Min.Y;
            }
            else if (m.NearlyEquals(db))
            {
                closestEdgePoint.X = x;
                closestEdgePoint.Y = Max.Y;
            }
            else if (m.NearlyEquals(dl))
            {
                closestEdgePoint.X = Min.X;
                closestEdgePoint.Y = y;
            }
            else
            {
                closestEdgePoint.X = Max.X;
                closestEdgePoint.Y = y;
            }
        }
コード例 #2
0
 /// <summary>Initializes a new instance of the <see cref="BoundingRectangle"/> struct.</summary>
 /// <param name="minX">The lower bound on the X axis.</param>
 /// <param name="minY">The lower bound on the Y axis.</param>
 /// <param name="maxX">The upper bound on the X axis.</param>
 /// <param name="maxY">The upper bound on the Y axis.</param>
 public BoundingRectangle(float minX, float minY, float maxX, float maxY)
 {
     NumberTools.Sort(ref minX, ref maxX, out minX, out maxX);
     NumberTools.Sort(ref minY, ref maxY, out minY, out maxY);
     Max.X = maxX;
     Max.Y = maxY;
     Min.X = minX;
     Min.Y = minY;
 }
コード例 #3
0
        public static bool TryParse(string source, out Line result)
        {
            bool validParse = NumberTools.ParseVectorAndNumber(source, out result.Normal, out result.D);

            if (validParse)
            {
                return(true);
            }

            result = Zero;
            return(false);
        }
コード例 #4
0
        public static bool TryParse(string source, out BoundingCircle result)
        {
            bool validParse = NumberTools.ParseVectorAndNumber(source, out result.Center, out result.Radius);

            if (validParse)
            {
                return(true);
            }

            result = Zero;
            return(false);
        }
コード例 #5
0
        public static bool TryParse(string source, out BoundingRectangle result)
        {
            bool validParse = NumberTools.ParseTwoVectors(source, out result.Min, out result.Max);

            if (validParse)
            {
                return(true);
            }

            result = Empty;
            return(false);
        }
コード例 #6
0
        public static bool TryParse(string source, out LineSegment result)
        {
            bool validParse = NumberTools.ParseTwoVectors(source, out result.Start, out result.End);

            if (validParse)
            {
                return(true);
            }

            result = Zero;
            return(false);
        }
コード例 #7
0
        public static bool TryParse(string source, out Ray2D result)
        {
            bool validParse = NumberTools.ParseTwoVectors(source, out result.Origin, out result.Direction);

            if (validParse)
            {
                return(true);
            }

            result = Zero;
            return(false);
        }
コード例 #8
0
        public void Intersects(ref Ray2D ray, bool discardInside, out float result)
        {
            Vector2 rayOriginRelativeToCircle2D;

            Vector2.Subtract(ref ray.Origin, ref Center, out rayOriginRelativeToCircle2D);
            var radiusSq = Radius * Radius;
            var magSq    = rayOriginRelativeToCircle2D.LengthSquared();

            if ((magSq <= radiusSq) && !discardInside)
            {
                result = 0;
                return;
            }
            var     a = ray.Direction.LengthSquared();
            Vector2 rayOriginRelativeToCircle2DTwice;

            Vector2.Multiply(ref rayOriginRelativeToCircle2D, 2, out rayOriginRelativeToCircle2DTwice);
            float b;

            Vector2.Dot(ref rayOriginRelativeToCircle2DTwice, ref ray.Direction, out b);
            var   c = magSq - radiusSq;
            float minus;
            float plus;

            if (NumberTools.TrySolveQuadratic(ref a, ref b, ref c, out plus, out minus))
            {
                if (minus < 0)
                {
                    if (plus > 0)
                    {
                        result = plus;
                        return;
                    }
                }
                else
                {
                    result = minus;
                    return;
                }
            }
            result = -1;
        }
コード例 #9
0
        public void Contains(ref BoundingRectangle rect, out Containment result)
        {
            float   mag;
            Vector2 maxDistance, minDistance;

            Vector2.Subtract(ref rect.Max, ref Center, out maxDistance);
            Vector2.Subtract(ref Center, ref rect.Min, out minDistance);
            NumberTools.Sort(ref minDistance.X, ref maxDistance.X, out minDistance.X, out maxDistance.X);
            NumberTools.Sort(ref minDistance.Y, ref maxDistance.Y, out minDistance.Y, out maxDistance.Y);
            mag = maxDistance.Length();
            if (mag <= Radius)
            {
                result = Containment.Contains;
            }
            else
            {
                mag    = minDistance.Length();
                result = (mag <= Radius) ? Containment.Intersects : Containment.Disjoint;
            }
        }
コード例 #10
0
 /// <summary>Clamps the given vector, so its coordinates will be within the specified bounds (inclusive).
 /// </summary>
 /// <param name="current">Vector to clamp.</param>
 /// <param name="min">The minimum bound.</param>
 /// <param name="max">The maximum bound.</param>
 /// <param name="clamped">Clamped vector.</param>
 public static void Clamp(ref Vector2 current, ref Vector2 min, ref Vector2 max, out Vector2 clamped)
 {
     NumberTools.Clamp(ref current.X, ref min.X, ref max.X, out clamped.X);
     NumberTools.Clamp(ref current.Y, ref min.Y, ref max.Y, out clamped.Y);
 }