0

我已经知道这个程序的输出应该是什么 - 我的问题是我无法让程序给出正确的输出,或者任何输出。我的问题是:识别和显示给定游览的所有跨过任何其他腿的腿。数据如下:旅游(“城市”)是 {0, 4, 1, 3, 2} 并且这些“城市”的点是 {2,0} , {4,1} , {0,1 } , {3,2} , {1,2}。这是我的程序:

    #include <iostream>

    using namespace std;

    const int MAX = 100;

    bool doCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);

    int main()
    {
        int numPts = 5;
        int tourAry[MAX] = {0, 4, 1, 3, 2};
        int pointsAry[MAX][2] = {{2, 0},{4, 1},{0, 1},{3, 2},{1, 2}};


        for(int start = 0; start <= numPts - 3; start++)
        {
            int startPt = tourAry[0];
            int endPt = tourAry[1];
            int testSegmentX1 = pointsAry[startPt][0];
            int testSegmentY1 = pointsAry[startPt][1];
            int testSegmentX2 = pointsAry[endPt][0];
            int testSegmentY2 = pointsAry[endPt][1];

            for(int nextSeg = start + 2; nextSeg <= numPts - 2; nextSeg++)
            {
                startPt = tourAry[2];
                endPt = tourAry[3];
                int startX = pointsAry[startPt][0];
                int startY = pointsAry[startPt][1];
                int endX = pointsAry[endPt][0];
                int endY = pointsAry[endPt][1];

                if(doCross(testSegmentX1, testSegmentY1, testSegmentX2, testSegmentY2, startX, startY, endX, endY))
                {
                    cout << tourAry[start] << " - " << tourAry[start+1] << " crosses " << tourAry[nextSeg] << " - " << tourAry[nextSeg+1] << endl;
                }

            }//for
        }//for

        return 0;

    }//main

    bool doCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
    {
        bool cross = true;
        double denom, numerA, numerB, uA, uB;


        denom = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1));
        numerA = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3));
        numerB = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3));

        if(denom == 0.0)
        {
            if(numerA == 0.0 && numerB == 0.0)
            {
                cross = false;
            }//if

        }//if
        else
        {
            float uA = numerA / denom;
                    float uB = numerB / denom;

            if (uA > 0.0 && uA < 1.0 && uB > 0.0 && uB < 1.0)
            {
                cross = true;
            }//if

            else
            {
                cross = false;

            }//else
        }//else

        return cross;

    }//doCross

我知道输出应该是: 0-4 交叉 3-2 4-1 交叉 3-2

任何帮助将不胜感激。

4

1 回答 1

0

您的索引看起来有点混乱。老实说,一旦我意识到您的交集算法有效,我就没有过多地调试它。

这是一个简单的示例,说明了您可能正在寻找的逻辑。它的可读性当然可以通过一些临时变量来提高:

    for(int i=0; i<numPts-1; i++) {
       for(int j=i; j<numPts-1; j++) {
         if(doCross(pointsAry[tourAry[i]][0],      // x1
                    pointsAry[tourAry[i]][1],      // y1
                    pointsAry[tourAry[i+1]][0],    // x2
                    pointsAry[tourAry[i+1]][1],    // y2
                    pointsAry[tourAry[j]][0],      // x3
                    pointsAry[tourAry[j]][1],      // y3
                    pointsAry[tourAry[j+1]][0],    // x4
                    pointsAry[tourAry[j+1]][1])) { // y4
             cout << tourAry[i] << " - " << tourAry[i+1] << " crosses " << tourAry[j] << " - " << tourAry[j+1] << endl;
         }
       }
    }

这给出了输出:

0 - 4 crosses 3 - 2
4 - 1 crosses 3 - 2
于 2012-09-14T02:41:00.090 回答