Creating and Painting Regions

A region is a combination of rectangles. Regions may be used to approximate complex objects, not unlike integration uses rectangles to calculate the area under a curve. For example, an arbitrary path may be converted to a region. Like pens and brushes, regions are separate graphics objects.

The following functions may be used to create a region.

CreateRectangularRegion Creates a rectangular region.
CreateEllipticRegion Creates an elliptic region.
CreateRoundedRectangularRegion Creates a region consisting of an elliptically rounded rectangle.
CreatePolygonalRegion Creates a region consisting of a polygon.
CreatePolypolygonalRegion Creates a region consisting of a sequence of polygons.
PathToRegion Constructs a region from the path of a device context.
CreateRegion - Extended Creates a region given explicit region data.

Regions may be destroyed using the function DeleteObject

The function CombineRegion is a powerful function by virtue of its variety. It gives rise to a number of operations including union, intersection, symmetric difference and subtraction.

Functions that render regions are:

FillRegion Fills a region.
FrameRegion Frames a region.
InvertRegion Inverts a region.
PaintRegion Paints a region using the current brush.

Clipping With Rectangles and Regions

Regions are also used for clipping. Selecting a region into a device context with the function SelectClippingRegion causes the region to become the current clipping region. The operating system takes a copy of the region, so that the region used may be subsequently deleted. When a region is made the clipping region, all graphics sent to the device context is clipped to that region (where, portions of the graphics that lie outside the region are not displayed). Three functions that affect the clipping region are:

A region may be used to invalidate a portion of a window via the function InvalidateRegion. Invalidating a region causes the generation of a paint message. A region of a window may be validated via function ValidateRegion.

The Clover Program

The clover program demonstrates the use of regions and its output is shown below.

The clover program forms a region using four ellipses. A series of lines is then rendered, subject to the given region being the clipping region. The clipping region is formed as shown below.

Region A = new Region(new Rectangle(0,H/3,W/2,2*H/3),true);
Region B = new Region(new Rectangle(W/2,H/3,W,2*H/3),true);
Region C = new Region(new Rectangle(W/3,0,2*W/3,H/2),true);
Region D = new Region(new Rectangle(W/3,H/2,2*W/3,H),true);

Clip = (A | B) ^ (C ^ D);

Once the clipping region has been set, the statement

for (double Angle = 0; Angle < 2 * Maths.Pi; Angle += 2 * Maths.Pi / 360)
    deviceContext.CurrentPosition = new Point(0, 0);

    deviceContext.DrawLineTo(new Point((Int32)(Radius * Math.Cos(Angle) + 0.5),
                                               (Int32)(-Radius * Math.Sin(Angle) + 0.5)));

draws lines from the center of the client (at 1 degree intervals) subject to the clipping region.