Categories

# Polygons and Zones – Example4

This is the right example if you want to triangulate polygons or perform polygon clipping. In Fade, a `Zone2` object defines a specific area of a triangulation. It allows you to combine shapes using the Boolean set operations union, difference, symmetric difference and intersection. The result is again a zone whose area and boundary can be computed and whose triangles can be extracted.

You can define a zone as the area…

• inside or outside a polygon
• grown from a seed point up to defined fences
• of the whole triangulation (global zone)
• of arbitrary triangles

## Preparing two initial polygons

This code snippet creates two polygons in a rectangle as shown in the above image.

```// * 1 *   Insert 4 bounding box points
dt.insert(Point2(0,0));
dt.insert(Point2(+100,0));
dt.insert(Point2(100,50));
dt.insert(Point2(0,50));

// * 2 *   Create points on two circles
std::vector<Point2> vCircle0;
std::vector<Point2> vCircle1;
int numPoints(8);

// * 3 *   Create segments
std::vector<Segment2> vSegments0;
std::vector<Segment2> vSegments1;
for(int i=0;i<numPoints;++i)
{
Segment2 seg0(vCircle0[i],vCircle0[(i+1)%numPoints]);
Segment2 seg1(vCircle1[i],vCircle1[(i+1)%numPoints]);
vSegments0.push_back(seg0);
vSegments1.push_back(seg1);
}

// * 4 *   Insert the segments as constraint graphs
ConstraintGraph2* pCG0=dt.createConstraint(vSegments0,CIS_CONSTRAINED_DELAUNAY);
ConstraintGraph2* pCG1=dt.createConstraint(vSegments1,CIS_CONSTRAINED_DELAUNAY);

// * 5 *  Visualize
dt.show("example4_constraints.ps",true);

```
1. Step 1 inserts 4 points into a new `Fade_2D` object.
2. Step 2 creates points on two circles (`vCircle0,vCircle1`)
3. The third step creates segments (`vSegment0,vSegment1`)
4. After that two polygons (`ConstraintGraph2 pCG0, pCG1`) are made.
5. Finally, Step 5 draws the triangulation.

## Creating zones inside and outside of polygons

The `ConstraintGraph2 pCG1` is the left polygon circle in the above image. Let’s create a `Zone2` inside and another one outside this polygon now.

```// + Zone inside pCG0:
Zone2* pZoneInside(dt.createZone(pCG0,ZL_INSIDE));
pZoneInside->show("example4_zoneInside.ps",true,true);

// + Zone outside pCG0:
Zone2* pZoneOutside(dt.createZone(pCG0,ZL_OUTSIDE));
pZoneOutside->show("example4_zoneOutside.ps",true,true);
```

## Growing a zone from a seed point

In addition to zones inside and outside a polygon, zones can also grow from a specific seed point.

```vector<ConstraintGraph2*> vCG;
vCG.push_back(pCG0);
vCG.push_back(pCG1);
Point2 seedPoint(5.0,5.0); // Point near the lower left corner
Zone2* pZoneGrow(dt.createZone(vCG,ZL_GROW,seedPoint));
pZoneGrow->show("example4_zoneGrow.ps",true,true);
```

This is code uses a `vector<ConstraintGraph2>` and it creates a `seedPoint` near the lower left corner of the below image. Then it grows a zone from this seed point. The edges of the `ConstraintGraph2` objects in `vCG` (in our case the two polygon circles) act as fence which stops the growing process. Zone (yellow area) grown from a seed point. The constraint edges (red) act as a fence that stops the growing process

## The global zone

A global zone consists of all existing triangles.

```Zone2* pZoneGlobal(dt.createZone(NULL,ZL_GLOBAL));
pZoneGlobal->show("example4_zoneGlobal.ps",true,true);```

## Zone creation from arbitrary triangles

This technique creates a zone from an arbitrary set of triangles, connected or not.

```// + Zone defined by specific triangles
vector<Triangle2*> vT;
dt.getTrianglePointers(vT);
vT.resize(vT.size()/2);
Zone2* pZoneRandomTriangles(dt.createZone(vT));
pZoneRandomTriangles->show("example4_zoneFromTriangles.ps",true,true);
```

This code firstly fetches all triangles from the triangulation. Then it chooses only half of them to create a zone.