Table of Contents

Class Region

Namespace
Terminal.Gui
Assembly
Terminal.Gui.dll

Represents a region composed of one or more rectangles, providing methods for geometric set operations such as union, intersection, exclusion, and complement. This class is designed for use in graphical or terminal-based user interfaces where regions need to be manipulated to manage screen areas, clipping, or drawing boundaries.

public class Region
Inheritance
Region
Inherited Members

Remarks

This class is thread-safe. All operations are synchronized to ensure consistent state when accessed concurrently.

The Region class adopts a philosophy of efficiency and flexibility, balancing performance with usability for GUI applications. It maintains a list of Rectangle objects, representing disjoint (non-overlapping) rectangular areas, and supports operations inspired by set theory. These operations allow combining regions in various ways, such as merging areas (Union or MinimalUnion), finding common areas (Intersect), or removing portions ( Difference or Exclude(Rectangle)).

To achieve high performance, the class employs a sweep-line algorithm for merging rectangles, which efficiently processes large sets of rectangles in O(n log n) time by scanning along the x-axis and tracking active vertical intervals. This approach ensures scalability for typical GUI scenarios with moderate numbers of rectangles. For operations like Union and MinimalUnion, an optional minimization step ( MinimizeRectangles(List<Rectangle>) ) is used to reduce the number of rectangles to a minimal set, producing the smallest possible collection of non-overlapping rectangles that cover the same area. This minimization, while O(n²) in worst-case complexity, is optimized for small-to-medium collections and provides a compact representation ideal for drawing or logical operations.

The class is immutable in its operations (returning new regions or modifying in-place via methods like Combine(Rectangle, RegionOp)), supports nullability for robustness, and implements IDisposable to manage resources by clearing internal state. Developers can choose between granular (detailed) or minimal (compact) outputs for union operations via Union and MinimalUnion, catering to diverse use cases such as rendering optimization, event handling, or visualization.

Constructors

Region()

Initializes a new instance of the Region class.

Region(Rectangle)

Initializes a new instance of the Region class with the specified rectangle.

Methods

Clone()

Creates an exact copy of the region.

Combine(Rectangle, RegionOp)

Combines rectangle with the region using the specified operation.

Combine(Region?, RegionOp)

Combines region with the region using the specified operation.

Complement(Rectangle)

Updates the region to be the complement of itself within the specified bounds.

Contains(Rectangle)

Determines whether the specified rectangle is contained within the region.

Contains(int, int)

Determines whether the specified point is contained within the region.

DrawBoundaries(LineCanvas, LineStyle, Attribute?)

Draws the boundaries of all rectangles in the region using the specified attributes, only if the rectangle is big enough.

DrawOuterBoundary(LineCanvas, LineStyle, Attribute?)

Draws the outer perimeter of the region to lineCanvas using style and attribute. The outer perimeter follows the shape of the rectangles in the region, even if non-rectangular, by drawing boundaries and excluding internal lines.

Equals(object?)

Determines whether the specified object is equal to this region.

Equals(Region?)

Determines whether the specified region is equal to this region.

Exclude(Rectangle)

Removes the specified rectangle from the region.

Exclude(Region?)

Removes the portion of the specified region from this region.

FillRectangles(Attribute, Rune?)

Fills the interior of all rectangles in the region with the specified attribute and fill rune.

GetBounds()

Gets a bounding rectangle for the entire region.

GetHashCode()

Returns a hash code for this region.

GetRectangles()

Returns an array of rectangles that represent the region.

Intersect(Rectangle)

Updates the region to be the intersection of itself with the specified rectangle.

Intersect(Region?)

Updates the region to be the intersection of itself with the specified region.

IsEmpty()

Determines whether the region is empty.

MinimalUnion(Rectangle)

Adds the specified rectangle to the region. Merges all rectangles into the smallest possible bounding shape.

MinimalUnion(Region?)

Adds the specified region to this region. Merges all rectangles into the smallest possible bounding shape.

Translate(int, int)

Translates all rectangles in the region by the specified offsets.

Union(Rectangle)

Adds the specified rectangle to the region. Merges all rectangles into a minimal or granular bounding shape.

Union(Region?)

Adds the specified region to this region. Merges all rectangles into a minimal or granular bounding shape.