/** * Flip TriangleList(I) with TriangleList(J). */ private bool FlipTriangles(int TriangleIndexOne, int TriangleIndexTwo) { BSTriangle A = TriangleList[TriangleIndexOne]; BSTriangle B = TriangleList[TriangleIndexTwo]; // if already optimized, don't have to do any BSPoint TestPt = A.FindNonSharingPoint(B); // If it's not inside, we don't have to do any if (GetCircumcircleState(A, TestPt) != ECircumCircleState.Inside) { return(false); } BSTriangle[] NewTriangles = new BSTriangle[2]; int TrianglesMade = 0; for (int VertexIndexOne = 0; VertexIndexOne < 2; ++VertexIndexOne) { for (int VertexIndexTwo = VertexIndexOne + 1; VertexIndexTwo < 3; ++VertexIndexTwo) { // Check if these vertices form a valid triangle (should be non-colinear) if (IsEligibleForTriangulation(A.Vertices[VertexIndexOne], A.Vertices[VertexIndexTwo], TestPt)) { // Create the new triangle and check if the final (original) vertex falls inside or outside of it's circumcircle BSTriangle NewTriangle = new BSTriangle(A.Vertices[VertexIndexOne], A.Vertices[VertexIndexTwo], TestPt); int VertexIndexThree = 3 - (VertexIndexTwo + VertexIndexOne); if (GetCircumcircleState(NewTriangle, A.Vertices[VertexIndexThree]) == ECircumCircleState.Outside) { // If so store the triangle and increment the number of triangles //checkf(TrianglesMade < 2, TEXT("Incorrect number of triangles created")); NewTriangles[TrianglesMade] = NewTriangle; ++TrianglesMade; } } } } // In case two triangles were generated the flip was successful so we can add them to the list if (TrianglesMade == 2) { AddTriangle(NewTriangles[0], false); AddTriangle(NewTriangles[1], false); } return(TrianglesMade == 2); }