/// <summary> /// Update a vertex as it can change from reflex to convex and convex vertices may /// become ears when other ears are removed. /// </summary> /// <param name="prev">Previous vertex.</param> /// <param name="curr">Current vertex.</param> /// <param name="next">Next vertex.</param> private void UpdateVertex(Vector3 prev, Vector3 curr, Vector3 next) { // Check if vertex became convex and update if so if (reflex.Contains(curr)) { if (Maths2D.IsConvex(prev, curr, next)) { reflex.Remove(curr); convex.AddLast(curr); } } // Check if the vertex is convex and if so, check if it is an ear. if (convex.Contains(curr)) { if (CheckIfEar(prev, curr, next)) { if (!ears.Contains(curr)) { ears.AddLast(curr); } } else if (ears.Contains(curr)) { ears.Remove(curr); } } }
/// <summary> /// Construct the necessary lists for triangulation with ear clipping. /// </summary> private void PrepareLists() { // Get reflex/convex vertices var j = vertices.Count - 1; var i = j - 1; for (int k = 0; k < vertices.Count; k++) { if (!Maths2D.IsConvex(vertices[i], vertices[j], vertices[k])) { reflex.AddLast(vertices[j]); } else { convex.AddLast(vertices[j]); } i = j; j = k; } // Get ears var curr = convex.First; var prev = verts.Find(curr.Value).Previous ?? verts.Last; var next = verts.Find(curr.Value).Next ?? verts.First; while (curr != verts.First) { if (CheckIfEar(prev.Value, curr.Value, next.Value)) { ears.AddLast(curr.Value); } curr = curr.Next; if (curr == null) { break; } prev = verts.Find(curr.Value).Previous ?? verts.Last; next = verts.Find(curr.Value).Next ?? verts.First; } }