double[] p0 = start.last.last(); double[] p1 = new double[]{start.last.P2.getX(), start.last.P2.getY()}; double[] p2 = new double[]{segments[0].P1.getX(), segments[0].P1.getY()}; double[] p3 = segments[0].first(); Point2D p;  double det = (p1[0] - p0[0])*(p3[1] - p2[1]) - (p3[0] - p2[0])*(p1[1] - p0[1]);  if( det > 0 ) {   p = lineIntersection(p0[0],p0[1],p1[0],p1[1],p2[0],p2[1],p3[0],p3[1], false); if( p == null ) {  start.add(new LineSegment(start.last.P2, segments[0].P1)); p = new Point2D.Double((segments[0].P1.getX()+ start.last.P2.getX())/2.0, (segments[0].P1.getY()+ start.last.P2.getY())/2.0); } else segments[0].P1 = start.last.P2 = p;  start.add( segments[0] ); joinSegments(end, segments[1], p); } else {  p0 = end.last.last(); p1 = new double[]{end.last.P2.getX(), end.last.P2.getY()}; p2 = new double[]{segments[1].P1.getX(), segments[1].P1.getY()}; p3 = segments[1].first();  p = lineIntersection(p0[0],p0[1],p1[0],p1[1], p2[0],p2[1],p3[0],p3[1], false); if( p == null ) {  end.add(new LineSegment(end.last.P2, segments[1].P1)); p = new Point2D.Double((segments[1].P1.getX()+ end.last.P2.getX())/2.0, (segments[1].P1.getY()+ end.last.P2.getY())/2.0); } else segments[1].P1 = end.last.P2 = p;  end.add( segments[1] ); joinSegments(start, segments[0], p); }
joinSegments(segments); start.add(segments[0]); end.add(segments[1]);
  private void addSegments(Segment[] segments)  {    double[] p0 = start.last.last();    double[] p1 = new double[]{start.last.P2.getX(), start.last.P2.getY()};    double[] p2 = new double[]{segments[0].P1.getX(), segments[0].P1.getY()};    double[] p3 = segments[0].first();    Point2D p;    double det = (p1[0] - p0[0])*(p3[1] - p2[1]) -       (p3[0] - p2[0])*(p1[1] - p0[1]);    if( det > 0 )      {        // start and segment[0] form the 'inner' part of a join,         // connect the overlapping segments        p = lineIntersection(p0[0],p0[1],p1[0],p1[1],p2[0],p2[1],p3[0],p3[1], false);        if( p == null )           {            // Dodgy.            start.add(new LineSegment(start.last.P2, segments[0].P1));            p = new Point2D.Double((segments[0].P1.getX()+ start.last.P2.getX())/2.0,                                   (segments[0].P1.getY()+ start.last.P2.getY())/2.0);          }        else          segments[0].P1 = start.last.P2 = p;        start.add( segments[0] );        joinSegments(end, segments[1], p);      }    else      {        // end and segment[1] form the 'inner' part         p0 = end.last.last();        p1 = new double[]{end.last.P2.getX(), end.last.P2.getY()};        p2 = new double[]{segments[1].P1.getX(), segments[1].P1.getY()};        p3 = segments[1].first();        p = lineIntersection(p0[0],p0[1],p1[0],p1[1],                             p2[0],p2[1],p3[0],p3[1], false);        if( p == null )          {            // Dodgy.            end.add(new LineSegment(end.last.P2, segments[1].P1));            p = new Point2D.Double((segments[1].P1.getX()+ end.last.P2.getX())/2.0,                                   (segments[1].P1.getY()+ end.last.P2.getY())/2.0);          }        else          segments[1].P1 = end.last.P2 = p;        end.add( segments[1] );        joinSegments(start, segments[0], p);      }  }