/// <summary>
        /// Create a new "split edge" with the section of points between
        /// (and including) the two intersections.
        /// The label for the new edge is the same as the label for the parent edge.
        /// </summary>
        /// <param name="ei0"></param>
        /// <param name="ei1"></param>
        public Edge CreateSplitEdge(EdgeIntersection ei0, EdgeIntersection ei1)
        {
            int npts           = ei1.SegmentIndex - ei0.SegmentIndex + 2;
            var lastSegStartPt = edge.Points[ei1.SegmentIndex];
            // if the last intersection point is not equal to the its segment start pt,
            // add it to the points list as well.
            // (This check is needed because the distance metric is not totally reliable!)
            // The check for point equality is 2D only - Z values are ignored
            bool useIntPt1 = ei1.Distance > 0.0 || !ei1.Coordinate.Equals2D(lastSegStartPt);

            if (!useIntPt1)
            {
                npts--;
            }

            var pts = new Coordinate[npts];
            int ipt = 0;

            pts[ipt++] = ei0.Coordinate.Copy();
            for (int i = ei0.SegmentIndex + 1; i <= ei1.SegmentIndex; i++)
            {
                pts[ipt++] = edge.Points[i];
            }

            if (useIntPt1)
            {
                pts[ipt] = ei1.Coordinate;
            }
            return(new Edge(pts, new Label(edge.Label)));
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="outstream"></param>
 public void Write(StreamWriter outstream)
 {
     outstream.WriteLine("Intersections:");
     for (IEnumerator <EdgeIntersection> it = GetEnumerator(); it.MoveNext();)
     {
         EdgeIntersection ei = it.Current;
         ei.Write(outstream);
     }
 }
 /// <summary> 
 /// Adds an intersection into the list, if it isn't already there.
 /// The input segmentIndex and dist are expected to be normalized.
 /// </summary>
 /// <param name="intPt"></param>
 /// <param name="segmentIndex"></param>
 /// <param name="dist"></param>
 /// <returns>The EdgeIntersection found or added.</returns>
 public EdgeIntersection Add(Coordinate intPt, int segmentIndex, double dist)
 {
     EdgeIntersection eiNew = new EdgeIntersection(intPt, segmentIndex, dist);
     EdgeIntersection ei;
     if (nodeMap.TryGetValue(eiNew, out ei))
         return ei;
     nodeMap[eiNew] = eiNew;
     return eiNew;
 }
        /// <summary>
        /// Adds an intersection into the list, if it isn't already there.
        /// The input segmentIndex and dist are expected to be normalized.
        /// </summary>
        /// <param name="intPt"></param>
        /// <param name="segmentIndex"></param>
        /// <param name="dist"></param>
        /// <returns>The EdgeIntersection found or added.</returns>
        public EdgeIntersection Add(Coordinate intPt, int segmentIndex, double dist)
        {
            var eiNew = new EdgeIntersection(intPt, segmentIndex, dist);
            EdgeIntersection ei;

            if (nodeMap.TryGetValue(eiNew, out ei))
            {
                return(ei);
            }
            nodeMap[eiNew] = eiNew;
            return(eiNew);
        }
        /// <summary>
        /// Create a StubEdge for the edge after the intersection eiCurr.
        /// The next intersection is provided
        /// in case it is the endpoint for the stub edge.
        /// Otherwise, the next point from the parent edge will be the endpoint.
        /// eiCurr will always be an EdgeIntersection, but eiNext may be null.
        /// </summary>
        /// <param name="edge"></param>
        /// <param name="l"></param>
        /// <param name="eiCurr"></param>
        /// <param name="eiNext"></param>
        public void CreateEdgeEndForNext(Edge edge, IList<EdgeEnd> l, EdgeIntersection eiCurr, EdgeIntersection eiNext)
        {
            int iNext = eiCurr.SegmentIndex + 1;
            // if there is no next edge there is nothing to do
            if (iNext >= edge.NumPoints && eiNext == null)
                return;

            Coordinate pNext = edge.GetCoordinate(iNext);
            // if the next intersection is in the same segment as the current, use it as the endpoint
            if (eiNext != null && eiNext.SegmentIndex == eiCurr.SegmentIndex)
                pNext = eiNext.Coordinate;

            EdgeEnd e = new EdgeEnd(edge, eiCurr.Coordinate, pNext, new Label(edge.Label));
            l.Add(e);
        }
        /// <summary>
        /// Creates new edges for all the edges that the intersections in this
        /// list split the parent edge into.
        /// Adds the edges to the input list (this is so a single list
        /// can be used to accumulate all split edges for a Geometry).
        /// </summary>
        /// <param name="edgeList"></param>
        public void AddSplitEdges(IList <Edge> edgeList)
        {
            // ensure that the list has entries for the first and last point of the edge
            AddEndpoints();

            IEnumerator <EdgeIntersection> it = GetEnumerator();

            it.MoveNext();
            // there should always be at least two entries in the list
            EdgeIntersection eiPrev = it.Current;

            while (it.MoveNext())
            {
                EdgeIntersection ei      = it.Current;
                Edge             newEdge = CreateSplitEdge(eiPrev, ei);
                edgeList.Add(newEdge);

                eiPrev = ei;
            }
        }
        /// <summary>
        /// Create a EdgeStub for the edge before the intersection eiCurr.
        /// The previous intersection is provided
        /// in case it is the endpoint for the stub edge.
        /// Otherwise, the previous point from the parent edge will be the endpoint.
        /// eiCurr will always be an EdgeIntersection, but eiPrev may be null.
        /// </summary>
        /// <param name="edge"></param>
        /// <param name="l"></param>
        /// <param name="eiCurr"></param>
        /// <param name="eiPrev"></param>
        public void CreateEdgeEndForPrev(Edge edge, IList<EdgeEnd> l, EdgeIntersection eiCurr, EdgeIntersection eiPrev)
        {
            int iPrev = eiCurr.SegmentIndex;
            if (eiCurr.Distance == 0.0) 
            {
                // if at the start of the edge there is no previous edge
                if (iPrev == 0)
                    return;
                iPrev--;
            }

            Coordinate pPrev = edge.GetCoordinate(iPrev);
            // if prev intersection is past the previous vertex, use it instead
            if (eiPrev != null && eiPrev.SegmentIndex >= iPrev)
                pPrev = eiPrev.Coordinate;

            Label label = new Label(edge.Label);
            // since edgeStub is oriented opposite to it's parent edge, have to flip sides for edge label
            label.Flip();
            EdgeEnd e = new EdgeEnd(edge, eiCurr.Coordinate, pPrev, label);        
            l.Add(e);
        }
        /// <summary>
        /// Create a new "split edge" with the section of points between
        /// (and including) the two intersections.
        /// The label for the new edge is the same as the label for the parent edge.
        /// </summary>
        /// <param name="ei0"></param>
        /// <param name="ei1"></param>
        public Edge CreateSplitEdge(EdgeIntersection ei0, EdgeIntersection ei1)
        {        
            int npts = ei1.SegmentIndex - ei0.SegmentIndex + 2;
            Coordinate lastSegStartPt = edge.Points[ei1.SegmentIndex];
            // if the last intersection point is not equal to the its segment start pt,
            // add it to the points list as well.
            // (This check is needed because the distance metric is not totally reliable!)
            // The check for point equality is 2D only - Z values are ignored
            bool useIntPt1 = ei1.Distance > 0.0 || ! ei1.Coordinate.Equals2D(lastSegStartPt);
            if (! useIntPt1) 
                npts--;

            Coordinate[] pts = new Coordinate[npts];
            int ipt = 0;
            pts[ipt++] = new Coordinate(ei0.Coordinate);
            for (int i = ei0.SegmentIndex + 1; i <= ei1.SegmentIndex; i++) 
                pts[ipt++] = edge.Points[i];

            if (useIntPt1) 
                pts[ipt] = ei1.Coordinate;
            return new Edge(pts, new Label(edge.Label));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            EdgeIntersection other = (EdgeIntersection)obj;

            return(Compare(other.SegmentIndex, other.Distance));
        }