I'm trying to implement Delaunay Triangulation with a simple grid as a test input, and it seems to work up to the third row, in which the triangles then start to overlap.
Rows 1 and 2
Row 3
Final
I'm having trouble understanding exactly what it is that causes this behaviour. Here is some pseudo-code:
for each vertex v
edgeBuffer = [];
for each triangle
if v is in the triangle's circumscribed circle
for each edge in the triangle
check if it's already in edgeBuffer
if not, add it
otherwise, remove it
remove triangle
for each edge in edgeBuffer
create a new triangle between edge and v
Actual code:
$("#add").click(function () { // User clicks a button to add the next vertex
currVertIndex++;
var v = vertList[currVertIndex];
var edges = [];
for (var i = 0; i < triangles.length; i++) {
if (triangles[i].circc.containsVertex(v)) {
var nextIndex;
for (var e = 0; e < 3; e++) {
nextIndex = (e + 1 == 3) ? 0 : e + 1;
var newEdge = new Edge(triangles[i].v[e], triangles[i].v[nextIndex]);
edges = addEdgeIfNew(newEdge, edges);
}
triangles.splice(i, 1);
i--;
}
}
for (var i = 0; i < edges.length; i++) {
triangles.push(new Triangle(
edges[i].v[0],
edges[i].v[1],
v
));
}
});
What is going wrong here? Fiddle
Edit: This is not a problem of duplicate edges not being removed...that was my previous question. That's fixed now. You can compare the images here with the images there to see that.