Rectangle.Center

weicco 23.03.09 12:02

Extension metodi, joka keskittää ja tarpeen mukaan kutistaa Rectanglen toisen Rectanglen sisään

 Tekstiversio  Arvo: 3 (3 ääntä)  Äänestä: +  -
namespace Weicco.Lib.Extensions
{
    public static class RectangleExtension
    {
        public static Rectangle Center(this Rectangle rect1, Rectangle rect2)
        {
            if (rect2.Width <= rect1.Width && rect2.Height <= rect1.Height)
            {
                // Just center rect2 in to rect1
                return new Rectangle(new Point(rect1.Width / 2 - rect2.Width / 2, rect1.Height / 2 - rect2.Height / 2), rect2.Size);
            }
            else
            {
                if (rect2.Width > rect2.Height)
                {
                    float m = (float)Math.Min(rect2.Width, rect1.Width) / (float)Math.Max(rect2.Width, rect1.Width);
                    return rect1.Center(new Rectangle(rect2.Location, new Size((int)(rect2.Width * m), (int)(rect2.Height * m))));
                }
                else
                {
                    float m = (float)Math.Min(rect2.Height, rect1.Height) / (float)Math.Max(rect2.Height, rect1.Height);
                    return rect1.Center(new Rectangle(rect2.Location, new Size((int)(rect2.Width * m), (int)(rect2.Height * m))));
                }
            }
        }
    }
}