Search code examples
c#triangulation

Failed to find Node for given afront point - Poly2Tri


I am want to cut a hole in a polygon:

|-----------------------|
|                       |
|      |------|         |
|      |      |         |
|      |------|         |
|                       |
|-----------------------|       

The outer has the following coords (double):

-0,76 ; -1,5
1,86  ; -1,5
-0,76 ; 1,5
1,86  ; 1,5

The inner coords (double) are:

0,65 ; -0,66
1,57 ; -0,66
0,65 ; 0,75
1,57 ; 0,75

I want to use for this Poly2Tri an created a Polygon with the first four points.

PolygonPoint[] pts = new PolygonPoint[shape.Length];
for (int i = 0; i < shape.Length; i++)
{
  pts[i] = new PolygonPoint((shape[i].X), (shape[i].Y));
  points.Add(shape[i]);
}
Polygon p = new Polygon(pts);

After that I added a hole with the second four points.

p.AddHole(new Polygon(ptsO));

Now I called:

P2T.Triangulate(p);

I get the following exception: Failed to find Node for given afront point

How do I fix this exception?


Solution

  • I'm not familiar with this library, but the outer 4 points are not in counterclockwise order as you specified them. Instead, they cross, where the grey line connects point3 back to point0:

    enter image description here

    Try adding them in counterclockwise order instead. You may need to create the inner loop in clockwise order, depending on Poly2Tri's conventions.

    Update

    Just found some documentation here:

    enter image description here

    1. Input/Output

    Simple Polygon outer bounary vertices oriented in counter clockwise direction.

    [snip]

    Here are the all vertices for holes oriented in clockwise direction;