public static List <Triangle> TriangulatePoints(List <Vertex> points) { //Generate the convex hull - will also remove the points from points list which are not on the hull List <Vertex> pointsOnConvexHull = HullAlgorithms.JarvisMarch(points); //Triangulate the convex hull List <Triangle> triangles = TriangulateHullAlgorithms.TriangulateConvexPolygon(pointsOnConvexHull); //Add the remaining points and split the triangles for (int i = 0; i < points.Count; i++) { Vertex currentPoint = points[i]; //2d space Vector2 p = new Vector2(currentPoint.position.x, currentPoint.position.z); //Which triangle is this point in? for (int j = 0; j < triangles.Count; j++) { Triangle t = triangles[j]; Vector2 p1 = new Vector2(t.v1.position.x, t.v1.position.z); Vector2 p2 = new Vector2(t.v2.position.x, t.v2.position.z); Vector2 p3 = new Vector2(t.v3.position.x, t.v3.position.z); if (Intersections.IsPointInTriangle(p1, p2, p3, p)) { //Create 3 new triangles Triangle t1 = new Triangle(t.v1, t.v2, currentPoint); Triangle t2 = new Triangle(t.v2, t.v3, currentPoint); Triangle t3 = new Triangle(t.v3, t.v1, currentPoint); //Remove the old triangle triangles.Remove(t); //Add the new triangles triangles.Add(t1); triangles.Add(t2); triangles.Add(t3); break; } } } return(triangles); }
//Check if a vertex is an ear private static void IsVertexEar(Vertex v, List <Vertex> vertices, List <Vertex> earVertices) { //A reflex vertex cant be an ear! if (v.isReflex) { return; } //This triangle to check point in triangle Vector2 a = v.prevVertex.GetPos2D_XZ(); Vector2 b = v.GetPos2D_XZ(); Vector2 c = v.nextVertex.GetPos2D_XZ(); bool hasPointInside = false; for (int i = 0; i < vertices.Count; i++) { //We only need to check if a reflex vertex is inside of the triangle if (vertices[i].isReflex) { Vector2 p = vertices[i].GetPos2D_XZ(); //This means inside and not on the hull if (Intersections.IsPointInTriangle(a, b, c, p)) { hasPointInside = true; break; } } } if (!hasPointInside) { earVertices.Add(v); } }