private void TriangulatePolygon(Otri firstedge, Otri lastedge,
int edgecount, bool doflip, bool triflaws)
{
Otri testtri = default(Otri);
Otri besttri = default(Otri);
Otri tempedge = default(Otri);
Vertex leftbasevertex, rightbasevertex;
Vertex testvertex;
Vertex bestvertex;
int bestnumber = 1;
// Identify the base vertices.
leftbasevertex = lastedge.Apex();
rightbasevertex = firstedge.Dest();
// Find the best vertex to connect the base to.
firstedge.Onext(ref besttri);
bestvertex = besttri.Dest();
besttri.Copy(ref testtri);
for (int i = 2; i <= edgecount - 2; i++)
{
testtri.OnextSelf();
testvertex = testtri.Dest();
// Is this a better vertex?
if (Primitives.InCircle(leftbasevertex, rightbasevertex, bestvertex, testvertex) > 0.0)
{
testtri.Copy(ref besttri);
bestvertex = testvertex;
bestnumber = i;
}
}
if (bestnumber > 1)
{
// Recursively triangulate the smaller polygon on the right.
besttri.Oprev(ref tempedge);
TriangulatePolygon(firstedge, tempedge, bestnumber + 1, true, triflaws);
}
if (bestnumber < edgecount - 2)
{
// Recursively triangulate the smaller polygon on the left.
besttri.Sym(ref tempedge);
TriangulatePolygon(besttri, lastedge, edgecount - bestnumber, true, triflaws);
// Find 'besttri' again; it may have been lost to edge flips.
tempedge.Sym(ref besttri);
}
if (doflip)
{
// Do one final edge flip.
Flip(ref besttri);
if (triflaws)
{
// Check the quality of the newly committed triangle.
besttri.Sym(ref testtri);
quality.TestTriangle(ref testtri);
}
}
// Return the base triangle.
besttri.Copy(ref lastedge);
}