/// <summary> /// Can this rectangle hold the subject rectangle /// </summary> /// <param name="subject">The rectangle to compare to</param> /// <returns></returns> public bool CanHold(Rectangle subject) => Width >= subject.Width && Height >= subject.Height;
/// <summary> /// Resizes this rectangle to fit inside a target rectangle /// </summary> /// <param name="target">The target rectangle</param> /// <returns>A new rectangle that fits within the target rectangle</returns> public Rectangle EnsureFit(Rectangle target) => target.CanHold(this) ? new Rectangle(Width, Height) : Match(target);
/// <summary> /// Resizes the rectangle to match the target rectangle on the shortest side /// </summary> /// <param name="target">The target rectangle</param> /// <returns>A new rectangle that matches the shortest side of the target rectangle.</returns> public Rectangle Match(Rectangle target) { var ratio = Ratio(target); return Resize(ratio); }
/// <summary> /// The ratio between this rectangle and a target rectangle /// </summary> /// <param name="target">The rectangle to compare to</param> /// <returns></returns> public double Ratio(Rectangle target) { var heightRatio = Convert.ToDouble(target.Height) / Height; var widthRatio = Convert.ToDouble(target.Width) / Width; return heightRatio <= widthRatio ? heightRatio : widthRatio; }