diff --git a/Geometrica/DataStructures/DelaunayTriangulation.cs b/Geometrica/DataStructures/DelaunayTriangulation.cs index ef34699..25ff5c1 100644 --- a/Geometrica/DataStructures/DelaunayTriangulation.cs +++ b/Geometrica/DataStructures/DelaunayTriangulation.cs @@ -93,16 +93,30 @@ public static Triangle[] LegalizeTriangle(Triangle[] triangles, Triangle targetT if (InCircle(targetTriangle[0], targetTriangle[1], targetTriangle[2], oppositeTriangle[oppositePointIndex])) return triangles; - var origNeighborsTarget = new[] { + var origNeighborsTarget = new[] + { targetTriangle.GetNeighbor(neighborIndex), targetTriangle.GetNeighbor((neighborIndex + 1) % 3), targetTriangle.GetNeighbor((neighborIndex + 2) % 3) }; - var origNeighborsOpposite = new[] { + var origNeighborsOpposite = new[] + { oppositeTriangle.GetNeighbor(oppositePointIndex), oppositeTriangle.GetNeighbor((oppositePointIndex + 1) % 3), oppositeTriangle.GetNeighbor((oppositePointIndex + 2) % 3) }; + var origPointsTarget = new[] + { + targetTriangle[neighborIndex], + targetTriangle[(neighborIndex + 1) % 3], + targetTriangle[(neighborIndex + 2) % 3] + }; + var origPointsOpposite = new[] + { + oppositeTriangle[oppositePointIndex], + oppositeTriangle[(oppositePointIndex + 1) % 3], + oppositeTriangle[(oppositePointIndex + 2) % 3] + }; if (origNeighborsTarget[2] != null) { @@ -115,7 +129,15 @@ public static Triangle[] LegalizeTriangle(Triangle[] triangles, Triangle targetT origNeighborsOpposite[2].SetNeighbor(targetIndex, targetTriangle); } - //for each of the original neighbors, set their neighbor references to the newly created triangles + targetTriangle[0] = origPointsTarget[0]; + targetTriangle[1] = origPointsOpposite[0]; + targetTriangle[2] = origPointsTarget[2]; + targetTriangle.SetNeighbors(origNeighborsOpposite[2], origNeighborsTarget[1], oppositeTriangle); + + oppositeTriangle[0] = origPointsTarget[0]; + oppositeTriangle[1] = origPointsOpposite[2]; + oppositeTriangle[2] = origPointsOpposite[0]; + oppositeTriangle.SetNeighbors(origNeighborsOpposite[1], origNeighborsTarget[2], targetTriangle); return triangles; } diff --git a/GeometricaTests/DelaunayTriangulationTests.cs b/GeometricaTests/DelaunayTriangulationTests.cs index 4b10e20..321fbfd 100644 --- a/GeometricaTests/DelaunayTriangulationTests.cs +++ b/GeometricaTests/DelaunayTriangulationTests.cs @@ -386,7 +386,7 @@ public void LegalizeTriangle_SwapsEdge_ForNonDelaunayPair() triangles = DelaunayTriangulation.LegalizeTriangle(triangles, t1, 0); var actual = string.Join(" ", triangles); - Assert.AreEqual("", actual); + Assert.AreEqual("Triangle [0; 0] [0.9; 0.9] [0; 1] Triangle [0; 0] [1; 0] [0.9; 0.9]", actual); } [TestMethod]