bool ExtendEdges(Point p) { // Point p is on director vector of edge e, and we extend e to include p. List<Edge> extended = new List<Edge>(); foreach (Edge e in edges) if (p.OnLine(e.P1, e.P2) && p != e.P1 && p != e.P2) { int where = p.Where(e.P1, e.P2); if (where == 1 && p.DistanceTo(e.P1) < Epsilon) { e.P1 = p; extended.Add(e); } if (where == 2 && p.DistanceTo(e.P2) < Epsilon) { e.P2 = p; extended.Add(e); } } if (extended.Count == 0) return false; // We try to link 2 extended edges to p to form a single edge. Point P1, P2; for (int i = 0; i < extended.Count; i++) if (extended[i] != null) { if (extended[i].P1 == p) P1 = extended[i].P2; else P1 = extended[i].P1; for (int j = i + 1; j < extended.Count; j++) if (extended[j] != null) { if (extended[j].P1 == p) P2 = extended[j].P2; else P2 = extended[j].P1; if (p.OnLine(P1, P2)) { // This works because extended[i] is a pointer! extended[i].P1 = P1; extended[i].P2 = P2; edges.Remove(extended[j]); extended[j] = null; break; } } } return true; }