/// <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; NumericHelper.Clamp(ref target.X, ref Min.X, ref Max.X, out x); NumericHelper.Clamp(ref target.Y, ref Min.Y, ref Max.Y, out y); float dl = MathF.Abs(x - Min.X); float dr = MathF.Abs(x - Max.X); float dt = MathF.Abs(y - Min.Y); float db = MathF.Abs(y - Max.Y); float m = MathF.Min(MathF.Min(MathF.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; } }