private void FollowLine(ILine start, Shape s, MyLineQueue q, List<ILine> lines) { // q.Remove(start); if (!s.AddPoint(start.Point1)) return; List<ILine> l = new List<ILine>(); for (int i = 0; i < lines.Count; i++) { if (!lines[i].IsCongruentTo(start)) { if (lines[i].HasPoint(start.Point2)) { if (start.Point2 != lines[i].Point1) { lines[i] = Line.Flip(lines[i]); } l.Add(lines[i]); } } } if (l.Count == 1) { FollowLine(l[0], s, q, lines); } else { int iR = -1; int iL = -1; int iU = -1; for (int i = 0; i < l.Count; i++) { switch (start.RelativeDirection(l[i])) { case Line.Direction.Right: iR = i; break; case Line.Direction.Left: iL = i; break; case Line.Direction.Up: iU = i; break; case Line.Direction.Down: iU = i; break; case Line.Direction.Undef: iU = i; break; default: break; } } if (iR > -1) { if (iU > -1) q.Enqueue(l[iU], shapes); if (iL > -1) q.Enqueue(l[iL], shapes); FollowLine(l[iR], s, q, lines); } else if (iU > -1) { if (iL > -1) q.Enqueue(l[iL], shapes); FollowLine(l[iU], s, q, lines); } else if (iL > -1) { FollowLine(l[iL], s, q, lines); } } return; }
private void MapShape(MyLineQueue q, List<ILine> lines) { Shape s = new Shape(); try { ILine start = q.Dequeue(); int qSize = q.Count; FollowLine(start, s, q, lines); if (ShapeAlreadyExists(s)) { int nRemove = q.Count - qSize; if (nRemove > 0) q.Remove(nRemove); } else if (ResolveDuplicateLines(s, lines)) { int remove = q.Count - qSize; if (remove > 0) q.Remove(remove); } else if (!ShapeIsValid(s)) { int remove = q.Count - qSize; if (remove > 0) { q.Remove(remove); } ILine li = (s.GetLines())[0]; q.Enqueue(Line.Flip(li), shapes); } else { AddShape(s); } } catch { } }
private void BuildShapes(List<ILine> lines) { if (lines.Count == 0) return; MyLineQueue q = new MyLineQueue(); q.Enqueue(lines[0], shapes); while (q.Count > 0) { MapShape(q, lines); if (q.Count == 0) { int iLine = FindUnusedLine(lines); if (iLine > -1) { q.Enqueue(lines[iLine], shapes); } } } }