/// <summary> A submethod of Add. Adds the specified rectangle to this polygon, skipping overlap checking with the first i elements of this polygon. </summary> /// <param name="rect"> The rectangle to add to this collection. </param> /// <param name="i"> The number of rectangles in this collection with which the specified rectangle is guaranteed not overlap with. </param> private void Add(Rect rect, int i) { if (rect.Height == 0 || rect.Width == 0) { return; } Contract.Assert(i == 0 || this.rectangles.Take(i - 1).All(element => { bool intersectsIncludingBoundaries = element.IntersectsWith(rect); if (!intersectsIncludingBoundaries) { return(true); } var original = element; //else check if boundaries intersect (which don't count as intersections) element.Intersect(rect); return(element.Width == 0 || element.Height == 0 || element == Rect.Empty); }), "The specified rectangle may not intersect with the first i elements"); for (; i < this.rectangles.Count; i++) { Rect elementAti = this[i]; Rect[] remainder = RectangleExtensions.Subtract(rect, ref elementAti); this[i] = elementAti; if (remainder.Length == 1 && remainder[0] == rect) //if no part of rect was subtracted { continue; //then continue with the next rectangle in this collection } //otherwise a part of rect was subtracted and isn't contained in the remainder any more //that means the remainder doesn't intersect with the first i + 1 rectangles in this collection and the remainder is added accordingly for (int j = 0; j < remainder.Length; j++) { this.Add(remainder[j], i + 1); //is correct even considering the swap this[i] = elementAti } return; } //this can only be reached if the continue statement was reached for every rectangle in this collection. //That implies rect does not intersect with any rectangle in this collection and can therefore simply be added this.rectangles.Add(rect); }
/// <summary> Removes the area in the specified rectangle from the current polygon. </summary> /// <param name="rect"></param> public void Remove(Rect rect) { this.rectangles = this.rectangles.Select(r => RectangleExtensions.Subtract(r, rect)).Concat().ToList(); this.Simplify(); //this simplications are probably redundant, but are added for the sake of robustness }