public void TestMethod()
        {
            MergeableRectangle mRect = new MergeableRectangle(new Rectangle(100, 100, 200, 300));
            mRect.Merge(new Rectangle(150, 150, 500, 600));

            Assert.AreEqual(new Rectangle(100, 100, 550, 650), mRect.Bounds);
        }
        Rectangle getLinkInvalidatedRegion(LinkDrawing linkDrawing)
        {
            MergeableRectangle rectangle = new MergeableRectangle(linkDrawing.Bounds);
            linkDrawing.Invalidated = true;
            ILinkableDrawing sourceDrawing = linkDrawing.SourceDrawing;
            ILinkableDrawing destinationDrawing = linkDrawing.DestinationDrawing;
            sourceDrawing.Invalidated = true;
            destinationDrawing.Invalidated = true;
            rectangle.Merge(sourceDrawing.InvalidatedRegion);
            rectangle.Merge(sourceDrawing.InvalidatedRegion);

            foreach (IDrawing drawing in containerDrawer.Drawings) {
                if (drawing.Bounds.IntersectsWith(rectangle.Bounds)) {
                    drawing.Invalidated = true;
                    rectangle.Merge(drawing.Bounds);
                }
            }
            return rectangle.Bounds;
        }
        Rectangle getStructureInvalidatedRegion(IDrawing targetDrawing)
        {
            targetDrawing.Invalidated = true;
            MergeableRectangle rectangle = new MergeableRectangle(targetDrawing.InvalidatedRegion);

            for (int i = 0; i < containerDrawer.Drawings.Count; i++) {
                IDrawing drawing = containerDrawer.Drawings[i];
                if (drawing.Bounds.IntersectsWith(rectangle.Bounds) && drawing.Invalidated == false) {
                    drawing.Invalidated = true;
                    rectangle.Merge(drawing.Bounds);
                }
            }
            return rectangle.Bounds;
        }
 void OnDrawingResized(object sender, SamDiagrams.Events.DrawingResizedEventArgs e)
 {
     containerDrawer.LinkOrchestrator.linkStrategy.DirectLinks(e.Drawing);
     InvalidateDrawing(e.Drawing);
     MergeableRectangle rectangleToInvalidate = new MergeableRectangle(e.Drawing.InvalidatedRegion);
     rectangleToInvalidate.Merge(e.PreviousBounds);
     invalidateOverlappingDrawings(rectangleToInvalidate.Bounds);
     containerDrawer.DiagramContainer.Invalidate(e.PreviousBounds);
 }
        void InvalidateLinkDrawing(LinkDrawing linkDrawing)
        {
            MergeableRectangle newRectangleToInvalidate = new MergeableRectangle(linkDrawing.Bounds);
            newRectangleToInvalidate.Merge(getLinkInvalidatedRegion(linkDrawing));
            newRectangleToInvalidate.Merge(
                getStructureInvalidatedRegion(linkDrawing.SourceDrawing));
            newRectangleToInvalidate.Merge(
                getStructureInvalidatedRegion(linkDrawing.DestinationDrawing));
            Rectangle auxRectangle = newRectangleToInvalidate.Bounds;
            invalidateOverlappingDrawings(previouslyInvalidatedRectangle);
            newRectangleToInvalidate.Merge(previouslyInvalidatedRectangle);
            invalidateOverlappingDrawings(newRectangleToInvalidate.Bounds);
            previouslyInvalidatedRectangle = auxRectangle;

            int scaleFactor = containerDrawer.DiagramContainer.ZoomFactor / 100;
            Rectangle r = new Rectangle(newRectangleToInvalidate.Bounds.X * scaleFactor,
                              newRectangleToInvalidate.Bounds.Y * scaleFactor,
                              newRectangleToInvalidate.Bounds.Width * scaleFactor,
                              newRectangleToInvalidate.Bounds.Height * scaleFactor);
            containerDrawer.DiagramContainer.Invalidate(r);
            previouslyInvalidatedRectangle = newRectangleToInvalidate.Bounds;
        }
        void InvalidateDrawingGroup(List<IDrawing> drawings)
        {
            if (drawings.Count < 1)
                return;

            MergeableRectangle newRectangleToInvalidate = new MergeableRectangle(drawings[0].InvalidatedRegion);
            foreach (IDrawing drawing in drawings) {
                drawing.Invalidated = true;
                newRectangleToInvalidate.Merge(drawing.InvalidatedRegion);
            }

            Rectangle auxRectangle = newRectangleToInvalidate.Bounds;

            newRectangleToInvalidate.Merge(previouslyInvalidatedRectangle);
            invalidateOverlappingDrawings(newRectangleToInvalidate.Bounds);
            previouslyInvalidatedRectangle = auxRectangle;
            double scaleFactor = (float)containerDrawer.DiagramContainer.ZoomFactor / 100;
            Rectangle r = new Rectangle((int)(Math.Ceiling(newRectangleToInvalidate.Bounds.X * scaleFactor)),
                              (int)(Math.Ceiling(newRectangleToInvalidate.Bounds.Y * scaleFactor)),
                              (int)(Math.Ceiling(newRectangleToInvalidate.Bounds.Width * scaleFactor)),
                              (int)(Math.Ceiling(newRectangleToInvalidate.Bounds.Height * scaleFactor)));

            containerDrawer.DiagramContainer.Invalidate(r);
        }
        void InvalidateDrawing(IDrawing drawing)
        {
            drawing.Invalidated = true;
            MergeableRectangle newRectangleToInvalidate = new MergeableRectangle(drawing.InvalidatedRegion);
            Rectangle auxRectangle = newRectangleToInvalidate.Bounds;
            invalidateOverlappingDrawings(previouslyInvalidatedRectangle);
            newRectangleToInvalidate.Merge(previouslyInvalidatedRectangle);
            previouslyInvalidatedRectangle = auxRectangle;
            invalidateOverlappingDrawings(newRectangleToInvalidate.Bounds);

            double scaleFactor = containerDrawer.DiagramContainer.ZoomFactor / 100;
            Rectangle r = new Rectangle((int)(Math.Ceiling(newRectangleToInvalidate.Bounds.X * scaleFactor)),
                              (int)(Math.Ceiling(newRectangleToInvalidate.Bounds.Y * scaleFactor)),
                              (int)(Math.Ceiling(newRectangleToInvalidate.Bounds.Width * scaleFactor)),
                              (int)(Math.Ceiling(newRectangleToInvalidate.Bounds.Height * scaleFactor)));

            containerDrawer.DiagramContainer.Invalidate(r);
        }