-1

I have been asked to store and draw an array of linesegments. The program should print the message "No intersection" or "Found an intersection" (depending) before terminting if negative coordinates are entered or 2x MAXSEGMENTS segments have been entered.

int main(void) {
    lineSeg_t line, allsegments[MAXSEGMENTS];
    point_t a, b;
    int pointssofar=0, i, v, w, x, y; 
    OpenGraphics();

    while (pointssofar<=(2*MAXSEGMENTS)){
        a=GetPoint();
        x=XCoord(a);
        y=YCoord(a);
        if ((x<0)||(y<0))
            break;
        b=GetPoint();
        v=XCoord(b);
        w=YCoord(b);
        if ((v<0)||(w<0))
            break;
        line=LineSeg(a, b);
        DrawLineSeg(line);
        allsegments[((pointssofar+2)/2)]=line;

        for (i=0;i<(pointssofar/2);i++){
            if (intersect(line, allsegments[i])==TRUE){
                printf ("Found an intersection");
                pointssofar=2*MAXSEGMENTS;
            } else if (pointssofar==(2*MAXSEGMENTS)){
                printf("No intersection");
            }
        }
    }
    for(i=0;i<(pointssofar/2);i++){
        if (intersect(allsegments[pointssofar/2], allsegments[i])==FALSE){
            printf("No intersection");
        }
    }
}

I'm having trouble outputting the messages. Think I'm stuck in the while loop and I'm really not sure how to get out! Thank you in advance.

4

2 回答 2

0

您的 while 循环将永远不会终止,因为其中没有任何行会使它的条件不成立。

while (pointssofar<=(2*MAXSEGMENTS)){

你唯一改变这些值的时候就是你得到的是

pointssofar=2*MAXSEGMENTS;

满足while循环条件。

您还有 2 个break语句,但这些语句完全依赖于XCoordandYCoord函数。他们可能永远不会真正返回负数。

于 2013-11-13T19:32:06.333 回答
0

除非你找到一个交叉点,否则你似乎并没有增加点。

于 2013-11-13T19:34:39.177 回答