2012-09-14 71 views
0

我已經知道這個程序的輸出是什麼 - 我的問題是我無法讓程序給出正確的輸出或任何輸出。我的問題是:識別並顯示給定旅程中跨過任何其他腿的所有腿。數據如下:旅遊(「城市」)爲{0,4,1,3,2},這些「城市」的點爲{2,0},{4,1},{0,1 },{3,2},{1,2}。下面是我的程序:爲什麼我的線段交叉測試不會返回true?

#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

任何幫助將不勝感激。

回答

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 
+0

這是很有趣,因爲我的教授基本上給我們的主塊,就是這樣沒有工作的一部分。我很高興,這是他而不是我!我會用你的建議。非常感謝你! – mkjo0617

相關問題