private void SnapPoints(MapPoint point, MapNodeHalfEdge edge) { // Don't snap if the neighboring nodes already have three edges if (edge.node.GetEdges().Count() <= 3 || edge.opposite == null || edge.opposite.node.GetEdges().Count() <= 3) { return; } // There are issues with this when snapping near the edge of the map if (point.GetEdges().Any(x => x.opposite == null) || edge.destination.GetEdges().Any(x => x.opposite == null)) { return; } // Delete the edges edges.Remove(edge); // Delete the other point points.Remove(new Vector3(edge.destination.position.x, 0, edge.destination.position.z)); var otherEdges = edge.destination.GetEdges().ToList(); // Update everything to point to the first point if (point.leavingEdge == edge) { point.leavingEdge = edge.opposite.next; } if (edge.node.startEdge == edge) { edge.node.startEdge = edge.previous; } edge.next.previous = edge.previous; edge.previous.next = edge.next; // Update the opposite edge as well if (edge.opposite != null && edge.opposite.node.GetEdges().Count() > 3) { // Delete edge edges.Remove(edge.opposite); // Update pointers edge.opposite.next.previous = edge.opposite.previous; edge.opposite.previous.next = edge.opposite.next; if (edge.opposite.node.startEdge == edge.opposite) { edge.opposite.node.startEdge = edge.opposite.previous; } } foreach (var otherEdge in otherEdges) { if (otherEdge.opposite != null) { otherEdge.opposite.destination = point; } } }