// relevant structures // sections exist from "from" to "to" in a Counter-Clockwise manner typedef struct section{ double from; // start angle double to; // end angle } section; // relevant functions // returns angular distance from a1 counter-clockwise to a2 double getDistance(double a1, double a2){ return getAngle(getAngle(a2) - getAngle(a1)); } // ensures angle is between 0 and 360 double getAngle(double a){ while(a < 0.0) a += 360.0; while(a >= 360.0) a -= 360.0; return a; } // problem function void combineOverlaps(section* sections, int *numSections){ int swapped,i,j; // combine sections that overlap swapped = 1; while(swapped){ swapped = 0; for(i = 0 ; (i < *numSections && swapped == 0); i++){ for(j = 0 ; (j < *numSections && swapped == 0); j++){ if(i != j){ if(getDistance(sections[i].from,sections[i].to) >= getDistance(sections[i].from,sections[j].from) ){ // combine the sections if(getDistance(sections[i].from, sections[i].to) < getDistance(sections[i].from, sections[j].to)){ sections[i].to = sections[j].to; } // get rid of j sections[j] = sections[*numSections - 1]; *numSections = *numSections - 1; swapped = 1; } } } } } } // problem function modified for debugging // besides the added fprintf's I also changed it so // that the two operands of the >= that is failing // are being stored in doubles so that I can print // them later without re-evaluating them. void combineOverlaps(section* sections, int *numSections){ int swapped,i,j; double dtmpa, dtmpb,dtmpc; fprintf(stderr,"\n"); for(i = 0 ; i < *numSections; i++){ fprintf(stderr,"Section %2d : %12f %12f\n",i,sections[i].from,sections[i].to); } // combine sections that overlap swapped = 1; while(swapped){ fprintf(stderr,"swapped is true\n"); fprintf(stderr,"Equality matrix...\n"); // horizontal header fprintf(stderr," "); for(i = 0 ; i < *numSections; i++){ fprintf(stderr,"%2d",i); } fprintf(stderr,"\n "); for(i = 0 ; i < *numSections; i++){ fprintf(stderr,"ft"); } fprintf(stderr,"\n"); // body for(i = 0 ; i < *numSections; i++){ fprintf(stderr,"%2df",i); for(j = 0 ; j < *numSections; j++){ fprintf(stderr,"%s",sections[i].from == sections[j].from ? "=" : "."); fprintf(stderr,"%s",sections[i].from == sections[j].to ? "=" : "."); } fprintf(stderr,"\n"); fprintf(stderr," t"); for(j = 0 ; j < *numSections; j++){ fprintf(stderr,"%s",sections[i].to == sections[j].from ? "=" : "."); fprintf(stderr,"%s",sections[i].to == sections[j].to ? "=" : "."); } fprintf(stderr,"\n"); } swapped = 0; for(i = 0 ; (i < *numSections && swapped == 0); i++){ fprintf(stderr,"i : %d\n",i); for(j = 0 ; (j < *numSections && swapped == 0); j++){ fprintf(stderr,"j : %d\n",j); if(i != j){ fprintf(stderr,"%d != %d\n",i,j); dtmpa = getDistance(sections[i].from,sections[i].to); dtmpb = getDistance(sections[i].from,sections[j].from); if(dtmpa >= dtmpb ){ fprintf(stderr,"getDistance(sections[%d].from (%20.15f),sections[%d].to (%20.15f)) ... (%25.20f)\n",i,sections[i].from,i,sections[i].to ,dtmpa); fprintf(stderr,">=\n"); fprintf(stderr,"getDistance(sections[%d].from (%20.15f),sections[%d].from (%20.15f)) ... (%25.20f)\n",i,sections[i].from,j,sections[j].from,dtmpb); // combine the sections if(getDistance(sections[i].from, sections[i].to) < getDistance(sections[i].from, sections[j].to)){ sections[i].to = sections[j].to; } // get rid of j sections[j] = sections[*numSections - 1]; *numSections = *numSections - 1; swapped = 1; }else{ fprintf(stderr,"getDistance(sections[%d].from (%20.15f),sections[%d].to (%20.15f)) ... (%25.20f)\n",i,sections[i].from,i,sections[i].to ,dtmpa); if(dtmpa < dtmpb) fprintf(stderr,"(< ) "); if(dtmpa <= dtmpb) fprintf(stderr,"(<=) "); if(dtmpa > dtmpb) fprintf(stderr,"(> ) "); if(dtmpa >= dtmpb) fprintf(stderr,"(UH OH) "); if(dtmpa == dtmpb) fprintf(stderr,"(==) "); fprintf(stderr,"<\n"); fprintf(stderr,"getDistance(sections[%d].from (%20.15f),sections[%d].from (%20.15f)) ... (%25.20f)\n",i,sections[i].from,j,sections[j].from,dtmpb); } } } } } }