Example #1
0
        /// <summary>
        /// Вычитание прямоугольников
        /// </summary>
        /// <param name="rect">Прямоугольник</param>
        /// <param name="subtract">Вычитаемый прямоугольник</param>
        /// <returns>До 4-х прямоугольников, описывающие область первого прямоугольника, которая не входит во второй</returns>
        public static List <RectInt> Subtract(RectInt rect, RectInt subtract)
        {
            List <RectInt> results = new List <RectInt>();

            if (IsIntersect(rect, subtract) == false)
            {
                return(results);
            }

            var top = new RectInt(rect.xMin, rect.yMin, rect.width, subtract.yMin - rect.yMin);

            if (top.IsEmpty() == false)
            {
                results.Add(top);
            }

            int topHeight = Math.Max(top.height, 0);

            var left = new RectInt(rect.xMin, rect.yMin + topHeight, subtract.xMin - rect.xMin, rect.height - topHeight);

            if (left.IsEmpty() == false)
            {
                results.Add(left);
            }

            var leftWidth = Math.Max(left.width, 0);

            var rightWidth = rect.xMax - subtract.xMax;

            var right = new RectInt(rect.xMax - rightWidth, rect.yMin + topHeight, rightWidth, rect.height - topHeight);

            rightWidth = Math.Max(rightWidth, 0);

            if (right.IsEmpty() == false)
            {
                results.Add(right);
            }

            var bottomHeight = rect.yMax - subtract.yMax;

            var bottom = new RectInt(rect.xMin + leftWidth, rect.yMax - bottomHeight, rect.width - leftWidth - rightWidth, bottomHeight);

            if (bottom.IsEmpty() == false)
            {
                results.Add(bottom);
            }

            return(results);
        }