/**
  * Because Delete Events have a link to their corresponding Insert event, it is possible to
  * compute exactly the range of events which must be compared to a given Insert event object.
  */
 private void prepareEvents() {
   Collections.sort(events);
   for (int i = 0; i < events.size(); i++) {
     SweepLineEvent ev = (SweepLineEvent) events.get(i);
     if (ev.isDelete()) {
       ev.getInsertEvent().setDeleteEventIndex(i);
     }
   }
 }
  private void computeIntersections(SegmentIntersector si) {
    nOverlaps = 0;
    prepareEvents();

    for (int i = 0; i < events.size(); i++) {
      SweepLineEvent ev = (SweepLineEvent) events.get(i);
      if (ev.isInsert()) {
        processOverlaps(i, ev.getDeleteEventIndex(), ev, si);
      }
    }
  }
 private void processOverlaps(int start, int end, SweepLineEvent ev0, SegmentIntersector si) {
   SweepLineSegment ss0 = (SweepLineSegment) ev0.getObject();
   /**
    * Since we might need to test for self-intersections, include current insert event object in
    * list of event objects to test. Last index can be skipped, because it must be a Delete event.
    */
   for (int i = start; i < end; i++) {
     SweepLineEvent ev1 = (SweepLineEvent) events.get(i);
     if (ev1.isInsert()) {
       SweepLineSegment ss1 = (SweepLineSegment) ev1.getObject();
       if (ev0.edgeSet == null || (ev0.edgeSet != ev1.edgeSet)) {
         ss0.computeIntersections(ss1, si);
         nOverlaps++;
       }
     }
   }
 }