Search code examples
c#system.drawing

Find remaining outer rectangles from Rectangle.Intersection


I have an outer rectangle and a rectangle inside. How can I return all remaining rectangle after cut of intersecting rectangle?


Solution

  • The natural way to represent this would be to use the Region class, something like:

    var result = new Region(outer);
    result.Exclude(inner);
    

    If you really want a list of Rectangle structures, you can look at converting to RectangleFs using GetRegionScans using an identity matrix and then converting them to Rectangles using Ceiling or Floor.