Program: 178230
Project: 178230
Buggy source:
/** 
 * Take all the polygons in a list, both open and closed, and reorder them such that  accessible points on any that have a squared distance less than linkUp to accessible points on any others are joined to form single polygons.   For an open polygon the accessible points are just its ends.  For a closed polygon  all its points are accessible. This is a fairly radical remove in-air movement strategy. All the polygons in the list must be plotted with the same physical extruder (otherwise  it would be nonsense to join them).  It is the calling function's responsibility to  make sure this is the case.
 * @param linkUp
 */
public void radicalReOrder(double linkUp){
  if (size() < 2)   return;
  int physicalExtruder=polygon(0).getAttributes().getExtruder().getPhysicalExtruderNumber();
  for (int i=1; i < size(); i++)   if (polygon(i).getAttributes().getExtruder().getPhysicalExtruderNumber() != physicalExtruder) {
    Debug.e("String_Node_Str");
    return;
  }
  for (int i=0; i < size() - 1; i++) {
    RrPolygon myPolygon=polygon(i);
    for (int j=i + 1; j < size(); j++) {
      double d=Double.POSITIVE_INFINITY;
      double d2;
      int myPoint=-1;
      int itsPoint=-1;
      int myTempPoint, itsTempPoint;
      boolean reverseMe, reverseIt;
      RrPolygon itsPolygon=polygon(j);
      if (!myPolygon.isClosed() && itsPolygon.isClosed()) {
        polygons.set(i,itsPolygon);
        polygons.set(j,myPolygon);
        myPolygon=polygon(i);
        itsPolygon=polygon(j);
      }
      if (!myPolygon.isClosed() && !itsPolygon.isClosed()) {
        reverseMe=true;
        reverseIt=false;
        d=Rr2Point.dSquared(myPolygon.point(0),itsPolygon.point(0));
        d2=Rr2Point.dSquared(myPolygon.point(myPolygon.size() - 1),itsPolygon.point(0));
        if (d2 < d) {
          reverseMe=false;
          reverseIt=false;
          d=d2;
        }
        d2=Rr2Point.dSquared(myPolygon.point(0),itsPolygon.point(itsPolygon.size() - 1));
        if (d2 < d) {
          reverseMe=true;
          reverseIt=true;
          d=d2;
        }
        d2=Rr2Point.dSquared(myPolygon.point(myPolygon.size() - 1),itsPolygon.point(itsPolygon.size() - 1));
        if (d2 < d) {
          reverseMe=false;
          reverseIt=true;
          d=d2;
        }
        if (d < linkUp) {
          if (reverseMe)           myPolygon=myPolygon.negate();
          if (reverseIt)           itsPolygon=itsPolygon.negate();
          myPolygon.add(itsPolygon);
          polygons.set(i,myPolygon);
          polygons.remove(j);
        }
      }
 else       if (myPolygon.isClosed() && !itsPolygon.isClosed()) {
        reverseIt=false;
        myPoint=myPolygon.nearestVertex(itsPolygon.point(0));
        d=Rr2Point.dSquared(myPolygon.point(myPoint),itsPolygon.point(0));
        myTempPoint=myPolygon.nearestVertex(itsPolygon.point(itsPolygon.size() - 1));
        d2=Rr2Point.dSquared(myPolygon.point(myTempPoint),itsPolygon.point(itsPolygon.size() - 1));
        if (d2 < d) {
          myPoint=myTempPoint;
          reverseIt=true;
          d=d2;
        }
        if (d < linkUp) {
          myPolygon=myPolygon.newStart(myPoint);
          myPolygon.add(myPolygon.point(0));
          if (reverseIt)           itsPolygon=itsPolygon.negate();
          myPolygon.add(itsPolygon);
          myPolygon.setOpen();
          polygons.set(i,myPolygon);
          polygons.remove(j);
        }
      }
 else       if (myPolygon.isClosed() && itsPolygon.isClosed()) {
        for (int k=0; k < itsPolygon.size(); k++) {
          myTempPoint=myPolygon.nearestVertex(itsPolygon.point(k));
          d2=Rr2Point.dSquared(myPolygon.point(myTempPoint),itsPolygon.point(k));
          if (d2 < d) {
            myPoint=myTempPoint;
            itsPoint=k;
            d=d2;
          }
        }
        if (d < linkUp) {
          myPolygon=myPolygon.newStart(myPoint);
          myPolygon.add(myPolygon.point(0));
          itsPolygon=itsPolygon.newStart(itsPoint);
          myPolygon.add(itsPolygon);
          myPolygon.setOpen();
          polygons.set(i,myPolygon);
          polygons.remove(j);
        }
      }
 else {
        Debug.e("String_Node_Str");
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Take all the polygons in a list, both open and closed, and reorder them such that  accessible points on any that have a squared distance less than linkUp to accessible points on any others are joined to form single polygons.   For an open polygon the accessible points are just its ends.  For a closed polygon  all its points are accessible. This is a fairly radical remove in-air movement strategy. All the polygons in the list must be plotted with the same physical extruder (otherwise  it would be nonsense to join them).  It is the calling function's responsibility to  make sure this is the case.
 * @param linkUp
 */
public void radicalReOrder(double linkUp){
  if (size() < 2)   return;
  int physicalExtruder=polygon(0).getAttributes().getExtruder().getPhysicalExtruderNumber();
  for (int i=1; i < size(); i++)   if (polygon(i).getAttributes().getExtruder().getPhysicalExtruderNumber() != physicalExtruder) {
    Debug.e("String_Node_Str");
    return;
  }
  for (int i=0; i < size() - 1; i++) {
    RrPolygon myPolygon=polygon(i);
    for (int j=i + 1; j < size(); j++) {
      double d=Double.POSITIVE_INFINITY;
      double d2;
      int myPoint=-1;
      int itsPoint=-1;
      int myTempPoint, itsTempPoint;
      boolean reverseMe, reverseIt;
      RrPolygon itsPolygon=polygon(j);
      if (myPolygon.isClosed() && !itsPolygon.isClosed()) {
        polygons.set(i,itsPolygon);
        polygons.set(j,myPolygon);
        myPolygon=polygon(i);
        itsPolygon=polygon(j);
      }
      if (!myPolygon.isClosed() && !itsPolygon.isClosed()) {
        reverseMe=true;
        reverseIt=false;
        d=Rr2Point.dSquared(myPolygon.point(0),itsPolygon.point(0));
        d2=Rr2Point.dSquared(myPolygon.point(myPolygon.size() - 1),itsPolygon.point(0));
        if (d2 < d) {
          reverseMe=false;
          reverseIt=false;
          d=d2;
        }
        d2=Rr2Point.dSquared(myPolygon.point(0),itsPolygon.point(itsPolygon.size() - 1));
        if (d2 < d) {
          reverseMe=true;
          reverseIt=true;
          d=d2;
        }
        d2=Rr2Point.dSquared(myPolygon.point(myPolygon.size() - 1),itsPolygon.point(itsPolygon.size() - 1));
        if (d2 < d) {
          reverseMe=false;
          reverseIt=true;
          d=d2;
        }
        if (d < linkUp) {
          if (reverseMe)           myPolygon=myPolygon.negate();
          if (reverseIt)           itsPolygon=itsPolygon.negate();
          myPolygon.add(itsPolygon);
          polygons.set(i,myPolygon);
          polygons.remove(j);
        }
      }
 else       if (!myPolygon.isClosed() && itsPolygon.isClosed()) {
        reverseMe=true;
        itsPoint=itsPolygon.nearestVertex(myPolygon.point(0));
        d=Rr2Point.dSquared(itsPolygon.point(itsPoint),myPolygon.point(0));
        itsTempPoint=itsPolygon.nearestVertex(myPolygon.point(myPolygon.size() - 1));
        d2=Rr2Point.dSquared(itsPolygon.point(itsTempPoint),myPolygon.point(myPolygon.size() - 1));
        if (d2 < d) {
          itsPoint=itsTempPoint;
          reverseMe=false;
          d=d2;
        }
        if (d < linkUp) {
          itsPolygon=itsPolygon.newStart(itsPoint);
          itsPolygon.add(itsPolygon.point(0));
          if (reverseMe)           myPolygon=myPolygon.negate();
          myPolygon.add(itsPolygon);
          myPolygon.setOpen();
          polygons.set(i,myPolygon);
          polygons.remove(j);
        }
      }
 else       if (myPolygon.isClosed() && itsPolygon.isClosed()) {
        for (int k=0; k < itsPolygon.size(); k++) {
          myTempPoint=myPolygon.nearestVertex(itsPolygon.point(k));
          d2=Rr2Point.dSquared(myPolygon.point(myTempPoint),itsPolygon.point(k));
          if (d2 < d) {
            myPoint=myTempPoint;
            itsPoint=k;
            d=d2;
          }
        }
        if (d < linkUp) {
          myPolygon=myPolygon.newStart(myPoint);
          myPolygon.add(myPolygon.point(0));
          itsPolygon=itsPolygon.newStart(itsPoint);
          itsPolygon.add(itsPolygon.point(0));
          myPolygon.add(itsPolygon);
          polygons.set(i,myPolygon);
          polygons.remove(j);
        }
      }
 else {
        Debug.e("String_Node_Str");
      }
    }
  }
}