In This Topic
The NRegion class represents a closed area in 2D space. The main purpose of the NRegion class is to implement generic support for set operations with regions (union, intersection, subtraction and exclusive-or). The result of a set operation between two regions is a region too. Another important ability of the NRegion class is that you can obtain a NGraphicsPath that represents its closed area border. Because of these region abilities, regions are used in all clipping related operations in the The Visual Tree and in the Painting in general.
The different operations with NRegion are described in detail by the following sections:
Creating Regions
You create instances of the NRegion class, by calling the following static methods of NRegion, as shown by the following sample:
Creating NRegions |
Copy Code
|
// create a rectangle region
NRegion rectRegion = NRegion.FromRectangle(new NRectangle(10, 10, 100, 100));
// create a quadrangle region
NRegion quadRegion = NRegion.FromQuadrangle(new NQuadrangle(new NPoint(10, 10), new NPoint(100, 20), new NPoint(100, 90), new NPoint(10, 100)));
// create an ellipse region (can actually be any graphics path).
NGraphicsPath ellipsePath = new NGraphicsPath();
ellipsePath.AddEllipse(new NRectangle(10, 10, 100, 100));
NRegion ellipseRegion = NRegion.FromPath(ellipsePath, ENFillRule.EvenOdd);
|
Set Operations with Regions
The NRegion class internally implements complex algorithms that help you perform the following Set-Operations with regions:
Operation |
Description |
Method |
Union |
Two regions can be "added" together.
The union of A and B, is a region that contains areas that are members of either A or B. |
NRegion Union(NRegion other)
|
Intersection |
A new region can be constructed by determining which areas the two sets have "in common".
The intersection of A and B, denoted by A ∩ B, is a region that contains sub-areas that are members of both A and B. |
NRegion Intersect(NRegion other)
|
Subtraction |
Two regions can also be "subtracted".
The subtraction of B from A, denoted as A - B, is a region that contains all areas of A that are not areas of B.
|
NRegion Subtract(NRegion other)
|
Exclusive Or |
A new region can also be constructed by the two regions "symmetric difference" (Exclusive-Or).
The symmetric difference of A and B, is a region the areas of which belong to either A or B and and not in their intersection. |
NRegion ExclusiveOr(NRegion other)
|
In the operation preview images the thick black border represents the outline of the resulting region, while the thin black borders represent the outlines of the two input circles.
Containment and Intersection Tests
You can query whether a certain point belongs to the area enclosed by a region, via the Contains method:
Test for Point Containment |
Copy Code
|
bool pointInRegion = region.Contains(new NPoint(20, 30));
|
You can also query whether another region intersects with a specific region, via the IntersectsWith method:
Test for Intersection |
Copy Code
|
bool regionsIntersect = region.IntersectsWith(otherRegion);
|
Creating a NGraphicsPath from a NRegion
A great ability of the NRegion class is that you can construct a NGraphicsPath that represents the region by calling its GetPath() method. The following code sample generates a graphics path that represents the Exclusive-Or between two ellipses, but can actually be used to perform any region-based geometry reconstruction:
Creating a NGraphicsPath from a NRegion |
Copy Code
|
NRectangle rect = new NRectangle(10, 10, 200, 100);
// create the first ellipse region
NGraphicsPath ellipse1Path = new NGraphicsPath();
ellipse1Path.AddEllipse(rect);
NRegion ellipse1Region = NRegion.FromPath(ellipse1Path, ENFillRule.EvenOdd);
// create the second ellipse region
rect.Translate(40, 0);
NGraphicsPath ellipse2Path = new NGraphicsPath();
ellipse2Path.AddEllipse(rect);
NRegion ellipse2Region = NRegion.FromPath(ellipse2Path, ENFillRule.EvenOdd);
// perform XOR on the two regions
NRegion xorRegion = ellipse1Region.ExclusiveOr(ellipse2Region);
// get the path of the resulting region
NGraphicsPath xorPath = xorRegion.GetPath();
|
The graphics path returned by the NRegion-GetPath() method is sealed and you should not attempt to modify it.
The region graphics path represents the area enclosed by the region if filled with the Even-Odd fill rule.