/// <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; } }
/// <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); }