/// <summary> /// Performs a brute-force comparison of every segment in each Edge. /// This has n^2 performance, and is about 100 times slower than using /// monotone chains. /// </summary> /// <param name="e0"></param> /// <param name="e1"></param> /// <param name="si"></param> private void ComputeIntersects(Edge e0, Edge e1, SegmentIntersector si) { ICoordinate[] pts0 = e0.Coordinates; ICoordinate[] pts1 = e1.Coordinates; for (int i0 = 0; i0 < pts0.Length - 1; i0++) for (int i1 = 0; i1 < pts1.Length - 1; i1++) si.AddIntersections(e0, i0, e1, i1); }
/// <summary> /// Performs a brute-force comparison of every segment in each Edge. /// This has n^2 performance, and is about 100 times slower than using /// monotone chains. /// </summary> /// <param name="e0"></param> /// <param name="e1"></param> /// <param name="si"></param> private void ComputeIntersects(Edge e0, Edge e1, SegmentIntersector si) { ICoordinate[] pts0 = e0.Coordinates; ICoordinate[] pts1 = e1.Coordinates; for (int i0 = 0; i0 < pts0.Length - 1; i0++) { for (int i1 = 0; i1 < pts1.Length - 1; i1++) { si.AddIntersections(e0, i0, e1, i1); } } }
/// <summary> /// /// </summary> /// <param name="start0"></param> /// <param name="end0"></param> /// <param name="mce"></param> /// <param name="start1"></param> /// <param name="end1"></param> /// <param name="ei"></param> private void ComputeIntersectsForChain(int start0, int end0, MonotoneChainEdge mce, int start1, int end1, SegmentIntersector ei) { ICoordinate p00 = pts[start0]; ICoordinate p01 = pts[end0]; ICoordinate p10 = mce.pts[start1]; ICoordinate p11 = mce.pts[end1]; // terminating condition for the recursion if (end0 - start0 == 1 && end1 - start1 == 1) { ei.AddIntersections(e, start0, mce.e, start1); return; } // nothing to do if the envelopes of these chains don't overlap env1.Init(p00, p01); env2.Init(p10, p11); if (!env1.Intersects(env2)) { return; } // the chains overlap, so split each in half and iterate (binary search) int mid0 = (start0 + end0) / 2; int mid1 = (start1 + end1) / 2; // check terminating conditions before recursing if (start0 < mid0) { if (start1 < mid1) { ComputeIntersectsForChain(start0, mid0, mce, start1, mid1, ei); } if (mid1 < end1) { ComputeIntersectsForChain(start0, mid0, mce, mid1, end1, ei); } } if (mid0 < end0) { if (start1 < mid1) { ComputeIntersectsForChain(mid0, end0, mce, start1, mid1, ei); } if (mid1 < end1) { ComputeIntersectsForChain(mid0, end0, mce, mid1, end1, ei); } } }
/// <summary> /// /// </summary> /// <param name="ss"></param> /// <param name="si"></param> public void ComputeIntersections(SweepLineSegment ss, SegmentIntersector si) { si.AddIntersections(edge, ptIndex, ss.edge, ss.ptIndex); }
/// <summary> /// /// </summary> /// <param name="start0"></param> /// <param name="end0"></param> /// <param name="mce"></param> /// <param name="start1"></param> /// <param name="end1"></param> /// <param name="ei"></param> private void ComputeIntersectsForChain( int start0, int end0, MonotoneChainEdge mce, int start1, int end1, SegmentIntersector ei) { ICoordinate p00 = pts[start0]; ICoordinate p01 = pts[end0]; ICoordinate p10 = mce.pts[start1]; ICoordinate p11 = mce.pts[end1]; // terminating condition for the recursion if (end0 - start0 == 1 && end1 - start1 == 1) { ei.AddIntersections(e, start0, mce.e, start1); return; } // nothing to do if the envelopes of these chains don't overlap env1.Init(p00, p01); env2.Init(p10, p11); if (!env1.Intersects(env2)) return; // the chains overlap, so split each in half and iterate (binary search) int mid0 = (start0 + end0) / 2; int mid1 = (start1 + end1) / 2; // check terminating conditions before recursing if (start0 < mid0) { if (start1 < mid1) ComputeIntersectsForChain(start0, mid0, mce, start1, mid1, ei); if (mid1 < end1) ComputeIntersectsForChain(start0, mid0, mce, mid1, end1, ei); } if (mid0 < end0) { if (start1 < mid1) ComputeIntersectsForChain(mid0, end0, mce, start1, mid1, ei); if (mid1 < end1) ComputeIntersectsForChain(mid0, end0, mce, mid1, end1, ei); } }